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 aaa17e8
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 105 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"
}
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
68 changes: 11 additions & 57 deletions tests/Unit/Doctrine/Phpcr/RouteProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,10 +87,10 @@ public function testGetRouteCollectionForRequest(): void
->willReturn($candidates)
;

$objects = [
$objects = new ArrayCollection([
new Route('/my'),
$this,
];
]);

$this->dmMock
->expects($this->once())
Expand Down Expand Up @@ -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';
Expand All @@ -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)
;

Expand Down Expand Up @@ -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);
Expand All @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -30,7 +31,7 @@ protected function setUp(): void
parent::setUp();
}

protected function createValidator()
protected function createValidator(): ConstraintValidatorInterface
{
return new RouteDefaultsTemplatingValidator($this->controllerResolver, $this->engine);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
19 changes: 9 additions & 10 deletions tests/Unit/Validator/Constraints/RouteDefaultsValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,44 +43,43 @@ 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());

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

$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(
Expand Down

0 comments on commit aaa17e8

Please sign in to comment.