From 704db61c32a80e2c4a4ce0d3bece3330efc82e7f Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Sat, 17 Feb 2024 18:38:04 +0100 Subject: [PATCH] get rid of mocks --- composer.json | 24 +++---- tests/Fixtures/App/config/config.yml | 3 +- tests/Unit/Doctrine/Orm/RouteProviderTest.php | 25 +------ .../Doctrine/Phpcr/ContentRepositoryTest.php | 47 ++++++------- .../Doctrine/Phpcr/PrefixCandidatesTest.php | 36 +++++----- .../Unit/Doctrine/Phpcr/RouteProviderTest.php | 68 +++---------------- .../RouteDefaultsTemplatingValidatorTest.php | 3 +- .../RouteDefaultsTwigValidatorTest.php | 5 +- .../RouteDefaultsValidatorTest.php | 19 +++--- 9 files changed, 78 insertions(+), 152 deletions(-) diff --git a/composer.json b/composer.json index 56b99435..ef2590ca 100644 --- a/composer.json +++ b/composer.json @@ -20,23 +20,23 @@ "symfony/framework-bundle": "^6.0 || ^7.0" }, "require-dev": { - "jackalope/jackalope-doctrine-dbal": "^1.3", + "doctrine/data-fixtures": "^1.0.0", + "doctrine/doctrine-bundle": "^2.0", + "doctrine/orm": "^2.9 || ^3.0", + "doctrine/phpcr-bundle": "^2.3 || ^3.0", "doctrine/phpcr-odm": "^1.4 || ^2.0", + "jackalope/jackalope-doctrine-dbal": "^1.3 || ^2.0", + "matthiasnoback/symfony-dependency-injection-test": "^4.1.0 || ^5.1.0", + "matthiasnoback/symfony-config-test": "^4.1.0 || ^5.1.0", "symfony/phpunit-bridge": "^7.0.3", - "matthiasnoback/symfony-dependency-injection-test": "^4.1.0", - "matthiasnoback/symfony-config-test": "^4.1.0", - "doctrine/orm": "^2.9", - "symfony-cmf/testing": "dev-sf7 as 4.2.0", - "doctrine/data-fixtures": "^1.0.0", "symfony/form": "^6.0 || ^7.0", + "symfony/monolog-bundle": "^3.5", + "symfony/security-bundle": "^6.0 || ^7.0", + "symfony/serializer": "^6.0 || ^7.0", "symfony/translation": "^6.0 || ^7.0", "symfony/validator": "^6.0 || ^7.0", - "symfony/security-bundle": "^6.0 || ^7.0", - "doctrine/doctrine-bundle": "^2.0", "symfony/twig-bundle": "^6.0 || ^7.0", - "symfony/monolog-bundle": "^3.5", - "doctrine/phpcr-bundle": "^2.3", - "symfony/serializer": "^6.0 || ^7.0", + "symfony-cmf/testing": "dev-sf7 as 4.2.0", "twig/twig": "^2.4.4 || ^3.0" }, "suggest": { @@ -70,5 +70,5 @@ "composer/package-versions-deprecated": true } }, - "minimum-stability": "beta" + "minimum-stability": "dev" } diff --git a/tests/Fixtures/App/config/config.yml b/tests/Fixtures/App/config/config.yml index 9c4e1301..a71f65a7 100644 --- a/tests/Fixtures/App/config/config.yml +++ b/tests/Fixtures/App/config/config.yml @@ -1,5 +1,4 @@ framework: - serializer: - enable_annotations: false + serializer: ~ property_access: ~ annotations: ~ diff --git a/tests/Unit/Doctrine/Orm/RouteProviderTest.php b/tests/Unit/Doctrine/Orm/RouteProviderTest.php index a4a8f737..138f2331 100644 --- a/tests/Unit/Doctrine/Orm/RouteProviderTest.php +++ b/tests/Unit/Doctrine/Orm/RouteProviderTest.php @@ -201,15 +201,7 @@ public function testGetRoutesByNames(): void ]; $this->objectRepositoryMock - ->expects($this->at(0)) ->method('findOneBy') - ->with(['name' => $paths[0]]) - ->willReturn($this->routeMock) - ; - $this->objectRepositoryMock - ->expects($this->at(1)) - ->method('findOneBy') - ->with(['name' => $paths[1]]) ->willReturn($this->routeMock) ; @@ -217,22 +209,9 @@ public function testGetRoutesByNames(): void $candidatesMock = $this->createMock(CandidatesInterface::class); $candidatesMock - ->expects($this->at(0)) ->method('isCandidate') - ->with($paths[0]) - ->willReturn(true) - ; - $candidatesMock - ->expects($this->at(1)) - ->method('isCandidate') - ->with($paths[1]) - ->willReturn(true) - ; - $candidatesMock - ->expects($this->at(2)) - ->method('isCandidate') - ->with($paths[2]) - ->willReturn(false) + ->withConsecutive([$paths[0]], [$paths[1]], [$paths[2]]) + ->willReturnOnConsecutiveCalls(true, true, false) ; $routeProvider = new RouteProvider($this->managerRegistryMock, $candidatesMock, 'Route'); diff --git a/tests/Unit/Doctrine/Phpcr/ContentRepositoryTest.php b/tests/Unit/Doctrine/Phpcr/ContentRepositoryTest.php index d6995937..f4104fa3 100644 --- a/tests/Unit/Doctrine/Phpcr/ContentRepositoryTest.php +++ b/tests/Unit/Doctrine/Phpcr/ContentRepositoryTest.php @@ -12,9 +12,9 @@ namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Unit\Doctrine\Phpcr; use Doctrine\ODM\PHPCR\DocumentManager; +use Doctrine\ODM\PHPCR\DocumentManagerInterface; use Doctrine\ODM\PHPCR\UnitOfWork; use Doctrine\Persistence\ManagerRegistry; -use Doctrine\Persistence\ObjectManager; use PHPUnit\Framework\TestCase; use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\ContentRepository; @@ -34,24 +34,22 @@ public function setUp(): void { $this->document = new \stdClass(); $this->document2 = new \stdClass(); - $this->objectManager = $this->createMock(ObjectManager::class); - $this->objectManager2 = $this->createMock(ObjectManager::class); + $this->objectManager = $this->createMock(DocumentManagerInterface::class); + $this->objectManager2 = $this->createMock(DocumentManagerInterface::class); $this->managerRegistry = $this->createMock(ManagerRegistry::class); } - public function testFindById() + public function testFindById(): void { $this->objectManager - ->expects($this->any()) ->method('find') ->with(null, 'id-123') - ->will($this->returnValue($this->document)) + ->willReturn($this->document) ; $this->managerRegistry - ->expects($this->any()) ->method('getManager') - ->will($this->returnValue($this->objectManager)) + ->willReturn($this->objectManager) ; $contentRepository = new ContentRepository($this->managerRegistry); @@ -60,25 +58,25 @@ public function testFindById() $this->assertSame($this->document, $contentRepository->findById('id-123')); } - public function testGetContentId() + public function testGetContentId(): void { $uow = $this->createMock(UnitOfWork::class); $uow->expects($this->once()) ->method('getDocumentId') ->with($this->document) - ->will($this->returnValue('id-123')) + ->willReturn('id-123') ; $dm = $this->createMock(DocumentManager::class); $dm ->expects($this->once()) ->method('getUnitOfWork') - ->will($this->returnValue($uow)) + ->willReturn($uow) ; $this->managerRegistry ->expects($this->once()) ->method('getManager') - ->will($this->returnValue($dm)) + ->willReturn($dm) ; $contentRepository = new ContentRepository($this->managerRegistry); @@ -87,18 +85,18 @@ public function testGetContentId() $this->assertEquals('id-123', $contentRepository->getContentId($this->document)); } - public function testGetContentIdNoObject() + public function testGetContentIdNoObject(): void { $contentRepository = new ContentRepository($this->managerRegistry); $this->assertNull($contentRepository->getContentId('hello')); } - public function testGetContentIdException() + public function testGetContentIdException(): void { $this->managerRegistry ->expects($this->once()) ->method('getManager') - ->will($this->throwException(new \Exception())) + ->willThrowException(new \Exception()) ; $contentRepository = new ContentRepository($this->managerRegistry); @@ -111,20 +109,18 @@ public function testGetContentIdException() * Use findById() with two different document managers. * The two document managers will return different documents when searching for the same id. */ - public function testChangingDocumentManager() + public function testChangingDocumentManager(): void { $this->objectManager - ->expects($this->any()) ->method('find') ->with(null, 'id-123') - ->will($this->returnValue($this->document)) + ->willReturn($this->document) ; $this->objectManager2 - ->expects($this->any()) ->method('find') ->with(null, 'id-123') - ->will($this->returnValue($this->document2)) + ->willReturn($this->document2) ; $objectManagers = [ @@ -132,14 +128,11 @@ public function testChangingDocumentManager() 'new_manager' => $this->objectManager2, ]; $this->managerRegistry - ->expects($this->any()) ->method('getManager') - ->will( - $this->returnCallback( - function ($name) use ($objectManagers) { - return $objectManagers[$name]; - } - ) + ->willReturnCallback( + function ($name) use ($objectManagers) { + return $objectManagers[$name]; + } ); $contentRepository = new ContentRepository($this->managerRegistry); diff --git a/tests/Unit/Doctrine/Phpcr/PrefixCandidatesTest.php b/tests/Unit/Doctrine/Phpcr/PrefixCandidatesTest.php index a283fd07..0a4c9188 100644 --- a/tests/Unit/Doctrine/Phpcr/PrefixCandidatesTest.php +++ b/tests/Unit/Doctrine/Phpcr/PrefixCandidatesTest.php @@ -12,8 +12,9 @@ namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Unit\Doctrine\Phpcr; use Doctrine\ODM\PHPCR\DocumentManager; -use Doctrine\ODM\PHPCR\Query\Builder\ConstraintFactory; +use Doctrine\ODM\PHPCR\Query\Builder\ConstraintOrx; use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder; +use Doctrine\ODM\PHPCR\Query\Builder\WhereAnd; use Doctrine\ODM\PHPCR\Translation\LocaleChooser\LocaleChooserInterface; use Doctrine\Persistence\ManagerRegistry; use PHPUnit\Framework\TestCase; @@ -22,7 +23,7 @@ class PrefixCandidatesTest extends TestCase { - public function testAddPrefix() + public function testAddPrefix(): void { $candidates = new PrefixCandidates(['/routes']); $this->assertEquals(['/routes'], $candidates->getPrefixes()); @@ -32,7 +33,7 @@ public function testAddPrefix() $this->assertEquals(['/other'], $candidates->getPrefixes()); } - public function testGetCandidates() + public function testGetCandidates(): void { $request = Request::create('/my/path.html'); @@ -54,7 +55,7 @@ public function testGetCandidates() ); } - public function testGetCandidatesPercentEncoded() + public function testGetCandidatesPercentEncoded(): void { $request = Request::create('/my/path%20percent%20encoded.html'); @@ -76,7 +77,7 @@ public function testGetCandidatesPercentEncoded() ); } - public function testGetCandidatesLocales() + public function testGetCandidatesLocales(): void { $request = Request::create('/de/path.html'); @@ -102,16 +103,15 @@ public function testGetCandidatesLocales() ); } - public function testGetCandidatesLocalesDm() + public function testGetCandidatesLocalesDm(): void { $request = Request::create('/de/path.html'); $dmMock = $this->createMock(DocumentManager::class); $managerRegistryMock = $this->createMock(ManagerRegistry::class); $managerRegistryMock - ->expects($this->any()) ->method('getManager') - ->will($this->returnValue($dmMock)) + ->willReturn($dmMock) ; $localeMock = $this->createMock(LocaleChooserInterface::class); $localeMock @@ -122,14 +122,14 @@ public function testGetCandidatesLocalesDm() $dmMock ->expects($this->once()) ->method('getLocaleChooserStrategy') - ->will($this->returnValue($localeMock)) + ->willReturn($localeMock) ; $candidates = new PrefixCandidates(['/simple'], ['de', 'fr'], $managerRegistryMock); $candidates->getCandidates($request); } - public function testGetCandidatesLocalesDmNoLocale() + public function testGetCandidatesLocalesDmNoLocale(): void { $request = Request::create('/it/path.html'); $managerRegistryMock = $this->createMock(ManagerRegistry::class); @@ -142,7 +142,7 @@ public function testGetCandidatesLocalesDmNoLocale() $candidates->getCandidates($request); } - public function testIsCandidate() + public function testIsCandidate(): void { $candidates = new PrefixCandidates(['/routes']); $this->assertTrue($candidates->isCandidate('/routes')); @@ -152,33 +152,33 @@ public function testIsCandidate() $this->assertFalse($candidates->isCandidate('/routesnotsame')); } - public function testRestrictQuery() + public function testRestrictQuery(): void { - $orX = $this->createMock(ConstraintFactory::class); + $orX = $this->createMock(ConstraintOrx::class); $orX->expects($this->once()) ->method('descendant') ->with('/routes', 'd') ; - $andWhere = $this->createMock(ConstraintFactory::class); + $andWhere = $this->createMock(WhereAnd::class); $andWhere->expects($this->once()) ->method('orX') - ->will($this->returnValue($orX)) + ->willReturn($orX) ; $qb = $this->createMock(QueryBuilder::class); $qb->expects($this->once()) ->method('andWhere') - ->will($this->returnValue($andWhere)) + ->willReturn($andWhere) ; $qb->expects($this->once()) ->method('getPrimaryAlias') - ->will($this->returnValue('d')) + ->willReturn('d') ; $candidates = new PrefixCandidates(['/routes']); $candidates->restrictQuery($qb); } - public function testRestrictQueryGlobal() + public function testRestrictQueryGlobal(): void { $qb = $this->createMock(QueryBuilder::class); $qb->expects($this->never()) diff --git a/tests/Unit/Doctrine/Phpcr/RouteProviderTest.php b/tests/Unit/Doctrine/Phpcr/RouteProviderTest.php index b5a21e1c..8eb3ef4e 100644 --- a/tests/Unit/Doctrine/Phpcr/RouteProviderTest.php +++ b/tests/Unit/Doctrine/Phpcr/RouteProviderTest.php @@ -13,8 +13,8 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ODM\PHPCR\DocumentManager; +use Doctrine\ODM\PHPCR\Query\Builder\From; use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder; -use Doctrine\ODM\PHPCR\Query\Builder\SourceFactory; use Doctrine\ODM\PHPCR\Query\Query; use Doctrine\ODM\PHPCR\UnitOfWork; use Doctrine\Persistence\ManagerRegistry; @@ -87,10 +87,10 @@ public function testGetRouteCollectionForRequest(): void ->willReturn($candidates) ; - $objects = [ + $objects = new ArrayCollection([ new Route('/my'), $this, - ]; + ]); $this->dmMock ->expects($this->once()) @@ -327,28 +327,9 @@ public function testGetRoutesByNames(): void ; $this->candidatesMock - ->expects($this->at(0)) ->method('isCandidate') - ->with('/cms/routes/test-route') - ->willReturn(true) - ; - $this->candidatesMock - ->expects($this->at(1)) - ->method('isCandidate') - ->with('/cms/simple/other-route') - ->willReturn(true) - ; - $this->candidatesMock - ->expects($this->at(2)) - ->method('isCandidate') - ->with('/cms/routes/not-a-route') - ->willReturn(true) - ; - $this->candidatesMock - ->expects($this->at(3)) - ->method('isCandidate') - ->with('/outside/prefix') - ->willReturn(false) + ->withConsecutive(['/cms/routes/test-route'], ['/cms/simple/other-route'], ['/cms/routes/not-a-route'], ['/outside/prefix']) + ->willReturnOnConsecutiveCalls(true, true, true, false) ; $paths[] = '/outside/prefix'; @@ -374,21 +355,8 @@ public function testGetRoutesByNamesNotCandidates(): void ; $this->candidatesMock - ->expects($this->at(0)) - ->method('isCandidate') - ->with('/cms/routes/test-route') - ->willReturn(false) - ; - $this->candidatesMock - ->expects($this->at(1)) ->method('isCandidate') - ->with('/cms/simple/other-route') - ->willReturn(false) - ; - $this->candidatesMock - ->expects($this->at(2)) - ->method('isCandidate') - ->with('/cms/routes/not-a-route') + ->withConsecutive(['/cms/routes/test-route'], ['/cms/simple/other-route'], ['/cms/routes/not-a-route']) ->willReturn(false) ; @@ -429,29 +397,15 @@ public function testGetRoutesByNamesUuid(): void ->willReturn($uow) ; $uow - ->expects($this->at(0)) ->method('getDocumentId') - ->with($route1) - ->willReturn('/cms/routes/test-route') - ; - $uow - ->expects($this->at(1)) - ->method('getDocumentId') - ->with($route2) - ->willReturn('/cms/routes/other-route') + ->withConsecutive([$route1], [$route2]) + ->willReturnOnConsecutiveCalls('/cms/routes/test-route', '/cms/routes/other-route') ; $this->candidatesMock - ->expects($this->at(0)) - ->method('isCandidate') - ->with('/cms/routes/test-route') - ->willReturn(true) - ; - $this->candidatesMock - ->expects($this->at(1)) ->method('isCandidate') - ->with('/cms/routes/other-route') - ->willReturn(false) + ->withConsecutive(['/cms/routes/test-route'], ['/cms/routes/other-route']) + ->willReturnOnConsecutiveCalls(true, false) ; $routeProvider = new RouteProvider($this->managerRegistryMock, $this->candidatesMock); @@ -463,7 +417,7 @@ public function testGetRoutesByNamesUuid(): void private function doRouteDump($limit): void { - $from = $this->createMock(SourceFactory::class); + $from = $this->createMock(From::class); $from->expects($this->once()) ->method('document') ->with(Route::class, 'd') diff --git a/tests/Unit/Validator/Constraints/RouteDefaultsTemplatingValidatorTest.php b/tests/Unit/Validator/Constraints/RouteDefaultsTemplatingValidatorTest.php index e6e74ae1..55c3e17f 100644 --- a/tests/Unit/Validator/Constraints/RouteDefaultsTemplatingValidatorTest.php +++ b/tests/Unit/Validator/Constraints/RouteDefaultsTemplatingValidatorTest.php @@ -13,6 +13,7 @@ use Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints\RouteDefaultsTemplatingValidator; use Symfony\Component\Templating\EngineInterface; +use Symfony\Component\Validator\ConstraintValidatorInterface; class RouteDefaultsTemplatingValidatorTest extends RouteDefaultsValidatorTest { @@ -30,7 +31,7 @@ protected function setUp(): void parent::setUp(); } - protected function createValidator() + protected function createValidator(): ConstraintValidatorInterface { return new RouteDefaultsTemplatingValidator($this->controllerResolver, $this->engine); } diff --git a/tests/Unit/Validator/Constraints/RouteDefaultsTwigValidatorTest.php b/tests/Unit/Validator/Constraints/RouteDefaultsTwigValidatorTest.php index 32f4dbf2..5894ff98 100644 --- a/tests/Unit/Validator/Constraints/RouteDefaultsTwigValidatorTest.php +++ b/tests/Unit/Validator/Constraints/RouteDefaultsTwigValidatorTest.php @@ -13,6 +13,7 @@ use Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints\RouteDefaults; use Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints\RouteDefaultsTwigValidator; +use Symfony\Component\Validator\ConstraintValidatorInterface; use Twig\Loader\LoaderInterface; class RouteDefaultsTwigValidatorTest extends RouteDefaultsValidatorTest @@ -22,12 +23,12 @@ protected function mockEngine() return $this->createMock(LoaderInterface::class); } - protected function createValidator() + protected function createValidator(): ConstraintValidatorInterface { return new RouteDefaultsTwigValidator($this->controllerResolver, $this->engine); } - public function testNoTemplateViolationWithoutTwig() + public function testNoTemplateViolationWithoutTwig(): void { $this->validator = new RouteDefaultsTwigValidator($this->controllerResolver, null); $this->validator->validate( diff --git a/tests/Unit/Validator/Constraints/RouteDefaultsValidatorTest.php b/tests/Unit/Validator/Constraints/RouteDefaultsValidatorTest.php index 18157c8c..c7ec58b6 100644 --- a/tests/Unit/Validator/Constraints/RouteDefaultsValidatorTest.php +++ b/tests/Unit/Validator/Constraints/RouteDefaultsValidatorTest.php @@ -43,18 +43,18 @@ protected function setUp(): void */ abstract protected function mockEngine(); - public function testCorrectControllerPath() + public function testCorrectControllerPath(): void { $this->validator->validate(['_controller' => 'FrameworkBundle:Redirect:redirect'], new RouteDefaults()); $this->assertNoViolation(); } - public function testControllerPathViolation() + public function testControllerPathViolation(): void { - $this->controllerResolver->expects($this->any()) + $this->controllerResolver ->method('getController') - ->will($this->throwException(new \LogicException('Invalid controller'))) + ->willThrowException(new \LogicException('Invalid controller')) ; $this->validator->validate(['_controller' => 'NotExistingBundle:Foo:bar'], new RouteDefaults()); @@ -62,11 +62,11 @@ public function testControllerPathViolation() $this->buildViolation('Invalid controller')->assertRaised(); } - public function testCorrectTemplate() + public function testCorrectTemplate(): void { - $this->engine->expects($this->any()) + $this->engine ->method('exists') - ->will($this->returnValue(true)) + ->willReturn(true) ; $this->validator->validate(['_template' => 'TwigBundle::layout.html.twig'], $this->constraint); @@ -74,13 +74,12 @@ public function testCorrectTemplate() $this->assertNoViolation(); } - public function testTemplateViolation() + public function testTemplateViolation(): void { $this ->engine - ->expects($this->any()) ->method('exists') - ->will($this->returnValue(false)) + ->willReturn(false) ; $this->validator->validate(