Skip to content

Commit

Permalink
get rid of mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Feb 17, 2024
1 parent 2e8d45d commit 704db61
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 152 deletions.
24 changes: 12 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -70,5 +70,5 @@
"composer/package-versions-deprecated": true
}
},
"minimum-stability": "beta"
"minimum-stability": "dev"
}
3 changes: 1 addition & 2 deletions tests/Fixtures/App/config/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
framework:
serializer:
enable_annotations: false
serializer: ~
property_access: ~
annotations: ~
25 changes: 2 additions & 23 deletions tests/Unit/Doctrine/Orm/RouteProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,38 +201,17 @@ 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)
;

$paths[] = '/no-candidate';

$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');
Expand Down
47 changes: 20 additions & 27 deletions tests/Unit/Doctrine/Phpcr/ContentRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -111,35 +109,30 @@ 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 = [
'default' => $this->objectManager,
'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);
Expand Down
36 changes: 18 additions & 18 deletions tests/Unit/Doctrine/Phpcr/PrefixCandidatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +23,7 @@

class PrefixCandidatesTest extends TestCase
{
public function testAddPrefix()
public function testAddPrefix(): void
{
$candidates = new PrefixCandidates(['/routes']);
$this->assertEquals(['/routes'], $candidates->getPrefixes());
Expand All @@ -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');

Expand All @@ -54,7 +55,7 @@ public function testGetCandidates()
);
}

public function testGetCandidatesPercentEncoded()
public function testGetCandidatesPercentEncoded(): void
{
$request = Request::create('/my/path%20percent%20encoded.html');

Expand All @@ -76,7 +77,7 @@ public function testGetCandidatesPercentEncoded()
);
}

public function testGetCandidatesLocales()
public function testGetCandidatesLocales(): void
{
$request = Request::create('/de/path.html');

Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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'));
Expand All @@ -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())
Expand Down
Loading

0 comments on commit 704db61

Please sign in to comment.