Skip to content

Commit

Permalink
Replace deprecated PHPUnit code
Browse files Browse the repository at this point in the history
The annotations expectedException and expectedExceptionMessage
has been replace with the corresponding expecting exceptions, ref:
sebastianbergmann/phpunit#3332

Also a call to TestCase::assertInternalType has been replaced
with TestCase::assertIsInt.
  • Loading branch information
franmomu committed Dec 3, 2020
1 parent dfaa257 commit 83d7c47
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Tests/DependencyInjection/AbstractMongoDBExtensionTest.php
Expand Up @@ -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');
Expand Down
9 changes: 5 additions & 4 deletions Tests/DependencyInjection/ConfigurationTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [
Expand All @@ -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]);
}

Expand Down
21 changes: 16 additions & 5 deletions Tests/FixtureIntegrationTest.php
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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');

Expand Down Expand Up @@ -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']);
}
Expand Down
7 changes: 3 additions & 4 deletions Tests/Form/Type/DocumentTypeTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
]);
Expand Down
13 changes: 7 additions & 6 deletions Tests/Mapping/Driver/AbstractDriverTest.php
Expand Up @@ -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;

Expand Down Expand Up @@ -37,25 +38,25 @@ public function testFindMappingFileInSubnamespace()
);
}

/**
* @expectedException Doctrine\Persistence\Mapping\MappingException
*/
public function testFindMappingFileNamespacedFoundFileNotFound()
{
$driver = $this->getDriver([$this->getFixtureDir() => 'MyNamespace\MyBundle\Document']);

$locator = $this->getDriverLocator($driver);

$this->expectException(MappingException::class);

$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');
}

Expand Down
36 changes: 24 additions & 12 deletions Tests/Repository/ContainerRepositoryFactoryTest.php
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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([]);
Expand All @@ -86,20 +85,33 @@ 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([]);

$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);
}

Expand Down
12 changes: 8 additions & 4 deletions Tests/ServiceRepositoryTest.php
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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'));
}
}

0 comments on commit 83d7c47

Please sign in to comment.