From b0925750011273e48f33bed5d60730f752166b52 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Tue, 20 Oct 2020 11:02:11 +0200 Subject: [PATCH] Replace deprecated PHPUnit code The annotations expectedException and expectedExceptionMessage has been replace with the corresponding expecting exceptions, ref: https://github.com/sebastianbergmann/phpunit/issues/3332 Also a call to TestCase::assertInternalType has been replaced with TestCase::assertIsInt. --- .../AbstractMongoDBExtensionTest.php | 2 +- .../DependencyInjection/ConfigurationTest.php | 9 ++--- Tests/FixtureIntegrationTest.php | 21 ++++++++--- Tests/Form/Type/DocumentTypeTest.php | 7 ++-- Tests/Mapping/Driver/AbstractDriverTest.php | 11 +++--- .../ContainerRepositoryFactoryTest.php | 36 ++++++++++++------- Tests/ServiceRepositoryTest.php | 12 ++++--- 7 files changed, 62 insertions(+), 36 deletions(-) diff --git a/Tests/DependencyInjection/AbstractMongoDBExtensionTest.php b/Tests/DependencyInjection/AbstractMongoDBExtensionTest.php index c8f8bcf4..b5daeac1 100644 --- a/Tests/DependencyInjection/AbstractMongoDBExtensionTest.php +++ b/Tests/DependencyInjection/AbstractMongoDBExtensionTest.php @@ -162,7 +162,7 @@ public function testLoadSimpleSingleConnection() $methodNames = array_map(static function ($call) { return $call[0]; }, $methodCalls); - $this->assertInternalType('integer', $pos = array_search('setDefaultDB', $methodNames)); + $this->assertIsInt($pos = array_search('setDefaultDB', $methodNames)); $this->assertEquals('mydb', $methodCalls[$pos][1][0]); $definition = $container->getDefinition('doctrine_mongodb.odm.default_document_manager'); diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index 93274efe..aeb89cd1 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -16,6 +16,7 @@ use LogicException; use PHPUnit\Framework\TestCase; use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader; +use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; use Symfony\Component\Config\Definition\Processor; use Symfony\Component\Config\Util\XmlUtils; use Symfony\Component\Yaml\Yaml; @@ -454,10 +455,6 @@ public function testPasswordAndUsernameShouldBeUnsetIfNull() $this->assertEquals([], $options['connections']['conn3']['options']); } - /** - * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException - * @expectedExceptionMessage The replicaSet option must be a string - */ public function testInvalidReplicaSetValue() { $config = [ @@ -471,6 +468,10 @@ public function testInvalidReplicaSetValue() $processor = new Processor(); $configuration = new Configuration(false); + + $this->expectException(InvalidConfigurationException::class); + $this->expectExceptionMessage('The replicaSet option must be a string'); + $processor->processConfiguration($configuration, [$config]); } diff --git a/Tests/FixtureIntegrationTest.php b/Tests/FixtureIntegrationTest.php index 2d65bbce..d74e2904 100644 --- a/Tests/FixtureIntegrationTest.php +++ b/Tests/FixtureIntegrationTest.php @@ -8,8 +8,10 @@ use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle; use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\FooBundle\DataFixtures\DependentOnRequiredConstructorArgsFixtures; use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\FooBundle\DataFixtures\OtherFixtures; +use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\FooBundle\DataFixtures\RequiredConstructorArgsFixtures; use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\FooBundle\DataFixtures\WithDependenciesFixtures; use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\FooBundle\FooBundle; +use LogicException; use RuntimeException; use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader; use Symfony\Bundle\FrameworkBundle\FrameworkBundle; @@ -22,6 +24,7 @@ use function array_map; use function get_class; use function rand; +use function sprintf; use function sys_get_temp_dir; class FixtureIntegrationTest extends TestCase @@ -90,10 +93,6 @@ public function testFixturesLoaderWhenFixtureHasDependencyThatIsNotYetLoaded() : $this->assertInstanceOf(WithDependenciesFixtures::class, $actualFixtures[1]); } - /** - * @expectedException \LogicException - * @expectedExceptionMessage The "Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\FooBundle\DataFixtures\RequiredConstructorArgsFixtures" fixture class is trying to be loaded, but is not available. Make sure this class is defined as a service and tagged with "doctrine.fixture.odm.mongodb". - */ public function testExceptionIfDependentFixtureNotWired() : void { $kernel = new IntegrationTestKernel('dev', false); @@ -106,6 +105,13 @@ public function testExceptionIfDependentFixtureNotWired() : void $kernel->boot(); $container = $kernel->getContainer(); + $this->expectException(LogicException::class); + $this->expectExceptionMessage(sprintf( + 'The "%s" fixture class is trying to be loaded, but is not available.' + . ' Make sure this class is defined as a service and tagged with "doctrine.fixture.odm.mongodb".', + RequiredConstructorArgsFixtures::class + )); + /** @var ContainerAwareLoader $loader */ $loader = $container->get('test.doctrine_mongodb.odm.symfony.fixtures.loader'); @@ -192,7 +198,12 @@ public function testLoadFixturesViaGroupWithMissingDependency() : void $loader = $container->get('test.doctrine_mongodb.odm.symfony.fixtures.loader'); $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('Fixture "Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\FooBundle\DataFixtures\OtherFixtures" was declared as a dependency for fixture "Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\FooBundle\DataFixtures\WithDependenciesFixtures", but it was not included in any of the loaded fixture groups.'); + $this->expectExceptionMessage(sprintf( + 'Fixture "%s" was declared as a dependency for fixture "%s",' + . ' but it was not included in any of the loaded fixture groups.', + OtherFixtures::class, + WithDependenciesFixtures::class + )); $loader->getFixtures(['missingDependencyGroup']); } diff --git a/Tests/Form/Type/DocumentTypeTest.php b/Tests/Form/Type/DocumentTypeTest.php index 35e01390..0cd91689 100644 --- a/Tests/Form/Type/DocumentTypeTest.php +++ b/Tests/Form/Type/DocumentTypeTest.php @@ -11,6 +11,7 @@ use Doctrine\Bundle\MongoDBBundle\Tests\TestCase; use Doctrine\ODM\MongoDB\DocumentManager; use Doctrine\Persistence\ManagerRegistry; +use InvalidArgumentException; use MongoDB\BSON\ObjectId; use PHPUnit\Framework\MockObject\MockObject; use Symfony\Component\Form\AbstractType; @@ -76,12 +77,10 @@ public function testDocumentManagerInstancePassedAsOption() $this->assertSame($this->dm, $field->getConfig()->getOption('em')); } - /** - * @expectedException \InvalidArgumentException - */ public function testSettingDocumentManagerAndEmOptionShouldThrowException() { - $field = $this->factory->createNamed('name', $this->typeFQCN ? DocumentType::class : 'document', null, [ + $this->expectException(InvalidArgumentException::class); + $this->factory->createNamed('name', $this->typeFQCN ? DocumentType::class : 'document', null, [ 'document_manager' => 'default', 'em' => 'default', ]); diff --git a/Tests/Mapping/Driver/AbstractDriverTest.php b/Tests/Mapping/Driver/AbstractDriverTest.php index 03441bfe..8e875c50 100644 --- a/Tests/Mapping/Driver/AbstractDriverTest.php +++ b/Tests/Mapping/Driver/AbstractDriverTest.php @@ -5,6 +5,7 @@ namespace Doctrine\Bundle\MongoDBBundle\Tests\Mapping\Driver; use Doctrine\Persistence\Mapping\Driver\FileDriver; +use Doctrine\Persistence\Mapping\MappingException; use PHPUnit\Framework\TestCase; use ReflectionProperty; @@ -37,25 +38,23 @@ public function testFindMappingFileInSubnamespace() ); } - /** - * @expectedException Doctrine\Persistence\Mapping\MappingException - */ public function testFindMappingFileNamespacedFoundFileNotFound() { $driver = $this->getDriver([$this->getFixtureDir() => 'MyNamespace\MyBundle\Document']); + $this->expectException(MappingException::class); $locator = $this->getDriverLocator($driver); $locator->findMappingFile('MyNamespace\MyBundle\Document\Missing'); } - /** - * @expectedException Doctrine\Persistence\Mapping\MappingException - */ public function testFindMappingNamespaceNotFound() { $driver = $this->getDriver([$this->getFixtureDir() => 'MyNamespace\MyBundle\Document']); $locator = $this->getDriverLocator($driver); + + $this->expectException(MappingException::class); + $locator->findMappingFile('MyOtherNamespace\MyBundle\Document\Foo'); } diff --git a/Tests/Repository/ContainerRepositoryFactoryTest.php b/Tests/Repository/ContainerRepositoryFactoryTest.php index a69823c1..d90e9af8 100644 --- a/Tests/Repository/ContainerRepositoryFactoryTest.php +++ b/Tests/Repository/ContainerRepositoryFactoryTest.php @@ -16,7 +16,9 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; +use RuntimeException; use stdClass; +use function sprintf; use function sys_get_temp_dir; class ContainerRepositoryFactoryTest extends TestCase @@ -57,10 +59,6 @@ public function testCustomRepositoryIsReturned() $this->assertSame($actualRepo, $factory->getRepository($dm, CustomNormalRepoDocument::class)); } - /** - * @expectedException \RuntimeException - * @expectedExceptionMessage The service "my_repo" must extend DocumentRepository (or a base class, like ServiceDocumentRepository). - */ public function testServiceRepositoriesMustExtendDocumentRepository() { $repo = new stdClass(); @@ -70,13 +68,14 @@ public function testServiceRepositoriesMustExtendDocumentRepository() $dm = $this->createDocumentManager([CoolDocument::class => 'my_repo']); $factory = new ContainerRepositoryFactory($container); + + $this->expectExceptionMessage( + 'The service "my_repo" must extend DocumentRepository (or a base class, like ServiceDocumentRepository).' + ); + $this->expectException(RuntimeException::class); $factory->getRepository($dm, CoolDocument::class); } - /** - * @expectedException \RuntimeException - * @expectedExceptionMessage The "Doctrine\Bundle\MongoDBBundle\Tests\Repository\StubServiceRepository" document repository implements "Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepositoryInterface", but its service could not be found. Make sure the service exists and is tagged with "doctrine_mongodb.odm.repository_service". - */ public function testRepositoryMatchesServiceInterfaceButServiceNotFound() { $container = $this->createContainer([]); @@ -86,13 +85,18 @@ public function testRepositoryMatchesServiceInterfaceButServiceNotFound() ]); $factory = new ContainerRepositoryFactory($container); + + $this->expectExceptionMessage(sprintf( + 'The "%s" document repository implements "%s", but its service could not be found.' + . ' Make sure the service exists and is tagged with "doctrine_mongodb.odm.repository_service".', + StubServiceRepository::class, + ServiceDocumentRepositoryInterface::class + )); + $this->expectException(RuntimeException::class); + $factory->getRepository($dm, CoolDocument::class); } - /** - * @expectedException \RuntimeException - * @expectedExceptionMessage The "Doctrine\Bundle\MongoDBBundle\Tests\Repository\CoolDocument" document has a repositoryClass set to "not_a_real_class", but this is not a valid class. Check your class naming. If this is meant to be a service id, make sure this service exists and is tagged with "doctrine_mongodb.odm.repository_service". - */ public function testCustomRepositoryIsNotAValidClass() { $container = $this->createContainer([]); @@ -100,6 +104,14 @@ public function testCustomRepositoryIsNotAValidClass() $dm = $this->createDocumentManager([CoolDocument::class => 'not_a_real_class']); $factory = new ContainerRepositoryFactory($container); + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage(sprintf( + 'The "%s" document has a repositoryClass set to "not_a_real_class", but this is not a valid class.' + . ' Check your class naming. If this is meant to be a service id, make sure this service exists and' + . ' is tagged with "doctrine_mongodb.odm.repository_service".', + CoolDocument::class + )); $factory->getRepository($dm, CoolDocument::class); } diff --git a/Tests/ServiceRepositoryTest.php b/Tests/ServiceRepositoryTest.php index 14aaf73f..7340f9ba 100644 --- a/Tests/ServiceRepositoryTest.php +++ b/Tests/ServiceRepositoryTest.php @@ -15,6 +15,7 @@ use Fixtures\Bundles\RepositoryServiceBundle\Document\TestCustomServiceRepoFile; use Fixtures\Bundles\RepositoryServiceBundle\Document\TestDefaultRepoDocument; use Fixtures\Bundles\RepositoryServiceBundle\Document\TestDefaultRepoFile; +use Fixtures\Bundles\RepositoryServiceBundle\Document\TestUnmappedDocument; use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomClassRepoRepository; use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomServiceRepoDocumentRepository; use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomServiceRepoGridFSRepository; @@ -26,6 +27,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; +use function sprintf; use function sys_get_temp_dir; class ServiceRepositoryTest extends TestCase @@ -133,12 +135,14 @@ public function testRepositoryServiceWiring() $this->assertInstanceOf(Builder::class, $customServiceGridFSRepo->createQueryBuilder()); } - /** - * @expectedException LogicException - * @expectedExceptionMessage Could not find the document manager for class "Fixtures\Bundles\RepositoryServiceBundle\Document\TestUnmappedDocument". Check your Doctrine configuration to make sure it is configured to load this document’s metadata. - */ public function testInstantiatingServiceRepositoryForUnmappedClass() { + $this->expectExceptionMessage(sprintf( + 'Could not find the document manager for class "%s".' + . ' Check your Doctrine configuration to make sure it is configured to load this document’s metadata.', + TestUnmappedDocument::class + )); + $this->expectException(LogicException::class); new TestUnmappedDocumentRepository($this->container->get('doctrine_mongodb')); } }