Skip to content

Commit 9d0652d

Browse files
authored
Merge pull request #447 from jordisala1991/refactor/tests
Refactor tests to not rely on external mock classes
2 parents 4476d88 + 3dfab41 commit 9d0652d

File tree

3 files changed

+92
-86
lines changed

3 files changed

+92
-86
lines changed

composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"symfony/framework-bundle": "^3.4 | ^4.4 | ^5.0"
2525
},
2626
"require-dev": {
27-
"phpspec/prophecy": "^1.8",
2827
"phpunit/phpunit": "^8.5 | ^9.5",
2928
"symfony/expression-language": "^3.4 | ^4.4 | ^5.0",
3029
"symfony/phpunit-bridge": "^5.2",

tests/DependencyInjection/Compiler/MenuBuilderPassTest.php

+38-35
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
use Knp\Bundle\MenuBundle\DependencyInjection\Compiler\MenuBuilderPass;
66
use PHPUnit\Framework\TestCase;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
78

89
class MenuBuilderPassTest extends TestCase
910
{
11+
/**
12+
* @var ContainerBuilder
13+
*/
1014
private $containerBuilder;
11-
private $definition;
12-
private $builderDefinition;
1315

1416
/**
1517
* @var MenuBuilderPass
@@ -18,84 +20,85 @@ class MenuBuilderPassTest extends TestCase
1820

1921
protected function setUp(): void
2022
{
21-
$this->containerBuilder = $this->prophesize('Symfony\Component\DependencyInjection\ContainerBuilder');
22-
$this->definition = $this->prophesize('Symfony\Component\DependencyInjection\Definition');
23-
$this->builderDefinition = $this->prophesize('Symfony\Component\DependencyInjection\Definition');
23+
$this->containerBuilder = new ContainerBuilder();
2424
$this->pass = new MenuBuilderPass();
2525

26-
$this->containerBuilder->hasDefinition('knp_menu.menu_provider.builder_service')->willReturn(true);
27-
$this->containerBuilder->getDefinition('knp_menu.menu_provider.builder_service')->willReturn($this->definition);
28-
$this->containerBuilder->getDefinition('id')->willReturn($this->builderDefinition);
29-
30-
$this->builderDefinition->isPublic()->willReturn(true);
31-
$this->builderDefinition->isAbstract()->willReturn(false);
26+
$this->containerBuilder->register('knp_menu.menu_provider.builder_service', \stdClass::class)
27+
->setArgument(0, null)
28+
->setArgument(1, null);
29+
$this->containerBuilder->register('id', \stdClass::class)
30+
->addTag('knp_menu.menu_builder', ['alias' => 'foo', 'method' => 'fooMenu'])
31+
->addTag('knp_menu.menu_builder', ['alias' => 'bar', 'method' => 'bar'])
32+
->setPublic(true);
3233
}
3334

3435
public function testNoopWithoutProvider(): void
3536
{
36-
$this->containerBuilder->hasDefinition('knp_menu.menu_provider.builder_service')->willReturn(false);
37+
$this->containerBuilder->removeDefinition('knp_menu.menu_provider.builder_service');
38+
$previousDefinitions = $this->containerBuilder->getDefinitions();
3739

38-
$this->containerBuilder->findTaggedServiceIds('knp_menu.menu_builder')->shouldNotBeCalled();
40+
$this->pass->process($this->containerBuilder);
3941

40-
$this->pass->process($this->containerBuilder->reveal());
42+
$this->assertSame($previousDefinitions, $this->containerBuilder->getDefinitions());
4143
}
4244

4345
public function testFailsWhenServiceIsAbstract(): void
4446
{
45-
$this->builderDefinition->isAbstract()->willReturn(true);
46-
$this->containerBuilder->findTaggedServiceIds('knp_menu.menu_builder')->willReturn(['id' => [['alias' => 'foo']]]);
47+
$this->containerBuilder->getDefinition('id')->setAbstract(true);
4748

4849
$this->expectException(\InvalidArgumentException::class);
4950
$this->expectExceptionMessage('Abstract services cannot be registered as menu builders but "id" is.');
50-
$this->pass->process($this->containerBuilder->reveal());
51+
52+
$this->pass->process($this->containerBuilder);
5153
}
5254

5355
public function testFailsWhenServiceIsPrivate(): void
5456
{
55-
$this->builderDefinition->isPublic()->willReturn(false);
56-
$this->containerBuilder->findTaggedServiceIds('knp_menu.menu_builder')->willReturn(['id' => [['alias' => 'foo']]]);
57+
$this->containerBuilder->getDefinition('id')->setPublic(false);
5758

5859
$this->expectException(\InvalidArgumentException::class);
5960
$this->expectExceptionMessage('Menu builder services must be public but "id" is a private service.');
60-
$this->pass->process($this->containerBuilder->reveal());
61+
62+
$this->pass->process($this->containerBuilder);
6163
}
6264

6365
public function testFailsWhenAliasIsMissing(): void
6466
{
65-
$this->containerBuilder->findTaggedServiceIds('knp_menu.menu_builder')->willReturn(['id' => [['alias' => '']]]);
67+
$this->containerBuilder->getDefinition('id')
68+
->setTags(['knp_menu.menu_builder' => [['alias' => '']]]);
6669

6770
$this->expectException(\InvalidArgumentException::class);
6871
$this->expectExceptionMessage('The alias is not defined in the "knp_menu.menu_builder" tag for the service "id"');
69-
$this->pass->process($this->containerBuilder->reveal());
72+
73+
$this->pass->process($this->containerBuilder);
7074
}
7175

7276
public function testFailsWhenMethodIsMissing(): void
7377
{
74-
$this->containerBuilder->findTaggedServiceIds('knp_menu.menu_builder')->willReturn(['id' => [['alias' => 'foo']]]);
78+
$this->containerBuilder->getDefinition('id')
79+
->setTags(['knp_menu.menu_builder' => [['alias' => 'foo']]]);
7580

7681
$this->expectException(\InvalidArgumentException::class);
7782
$this->expectExceptionMessage('The method is not defined in the "knp_menu.menu_builder" tag for the service "id"');
78-
$this->pass->process($this->containerBuilder->reveal());
83+
84+
$this->pass->process($this->containerBuilder);
7985
}
8086

8187
public function testReplaceArgument(): void
8288
{
83-
$this->containerBuilder->getDefinition('id1')->willReturn($this->builderDefinition);
84-
$this->containerBuilder->getDefinition('id2')->willReturn($this->builderDefinition);
85-
86-
$taggedServiceIds = [
87-
'id1' => [['alias' => 'foo', 'method' => 'fooMenu'], ['alias' => 'bar', 'method' => 'bar']],
88-
'id2' => [['alias' => 'foo', 'method' => 'fooBar'], ['alias' => 'baz', 'method' => 'bar']],
89-
];
90-
$this->containerBuilder->findTaggedServiceIds('knp_menu.menu_builder')->willReturn($taggedServiceIds);
89+
$this->containerBuilder->register('id2', \stdClass::class)
90+
->addTag('knp_menu.menu_builder', ['alias' => 'foo', 'method' => 'fooBar'])
91+
->addTag('knp_menu.menu_builder', ['alias' => 'baz', 'method' => 'bar'])
92+
->setPublic(true);
9193

9294
$menuBuilders = [
9395
'foo' => ['id2', 'fooBar'],
94-
'bar' => ['id1', 'bar'],
96+
'bar' => ['id', 'bar'],
9597
'baz' => ['id2', 'bar'],
9698
];
97-
$this->definition->replaceArgument(1, $menuBuilders)->shouldBeCalled();
9899

99-
$this->pass->process($this->containerBuilder->reveal());
100+
$this->pass->process($this->containerBuilder);
101+
102+
$this->assertSame($menuBuilders, $this->containerBuilder->getDefinition('knp_menu.menu_provider.builder_service')->getArgument(1));
100103
}
101104
}

tests/DependencyInjection/Compiler/RegisterMenusPassTest.php

+54-50
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
namespace Knp\Bundle\MenuBundle\Tests\DependencyInjection\Compiler;
44

55
use Knp\Bundle\MenuBundle\DependencyInjection\Compiler\RegisterMenusPass;
6+
use Knp\Menu\MenuItem;
7+
use Knp\Menu\Provider\LazyProvider;
68
use PHPUnit\Framework\TestCase;
79
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
810
use Symfony\Component\DependencyInjection\ContainerBuilder;
9-
use Symfony\Component\DependencyInjection\Definition;
1011
use Symfony\Component\DependencyInjection\Reference;
1112

1213
class RegisterMenusPassTest extends TestCase
1314
{
15+
/**
16+
* @var ContainerBuilder
17+
*/
1418
private $containerBuilder;
15-
private $definition;
1619

1720
/**
1821
* @var RegisterMenusPass
@@ -21,93 +24,94 @@ class RegisterMenusPassTest extends TestCase
2124

2225
protected function setUp(): void
2326
{
24-
if (!\class_exists(ServiceClosureArgument::class)) {
25-
$this->markTestSkipped('The RegisterMenuPass requires Symfony DI 3.3+.');
26-
}
27+
$this->containerBuilder = new ContainerBuilder();
28+
29+
$this->containerBuilder->register('knp_menu.menu_provider.lazy', LazyProvider::class)
30+
->setArgument(0, null);
31+
$this->containerBuilder->register('knp_menu.menu_provider.builder_service', \stdClass::class);
32+
$this->containerBuilder->register('id', \stdClass::class)
33+
->addTag('knp_menu.menu_builder', ['alias' => 'foo', 'method' => 'fooMenu'])
34+
->addTag('knp_menu.menu_builder', ['alias' => 'bar', 'method' => 'bar'])
35+
->setPublic(true);
36+
$this->containerBuilder->register('menu_id', MenuItem::class)
37+
->addTag('knp_menu.menu', ['alias' => 'baz'])
38+
->setPublic(true);
2739

28-
$this->containerBuilder = $this->prophesize(ContainerBuilder::class);
29-
$this->definition = $this->prophesize(Definition::class);
3040
$this->pass = new RegisterMenusPass();
31-
32-
$this->containerBuilder->hasDefinition('knp_menu.menu_provider.lazy')->willReturn(true);
33-
$this->containerBuilder->getDefinition('knp_menu.menu_provider.lazy')->willReturn($this->definition);
34-
35-
$this->containerBuilder->removeDefinition('knp_menu.menu_provider.builder_service')->shouldBeCalled();
36-
37-
$this->containerBuilder->findTaggedServiceIds('knp_menu.menu_builder', true)->willReturn([]);
38-
$this->containerBuilder->findTaggedServiceIds('knp_menu.menu', true)->willReturn([]);
3941
}
4042

4143
public function testNoopWithoutProvider(): void
4244
{
43-
$this->containerBuilder->hasDefinition('knp_menu.menu_provider.lazy')->willReturn(false);
45+
$this->containerBuilder->removeDefinition('knp_menu.menu_provider.lazy');
4446

45-
$this->containerBuilder->findTaggedServiceIds('knp_menu.menu_builder', true)->shouldNotBeCalled();
46-
$this->containerBuilder->findTaggedServiceIds('knp_menu.menu', true)->shouldNotBeCalled();
47-
$this->containerBuilder->removeDefinition('knp_menu.menu_provider.builder_service')->shouldNotBeCalled();
47+
$this->pass->process($this->containerBuilder);
4848

49-
$this->pass->process($this->containerBuilder->reveal());
49+
$this->assertTrue($this->containerBuilder->hasDefinition('knp_menu.menu_provider.builder_service'));
5050
}
5151

5252
public function testFailsWhenBuilderAliasIsMissing(): void
5353
{
54-
$this->containerBuilder->findTaggedServiceIds('knp_menu.menu_builder', true)->willReturn(['id' => [['alias' => '']]]);
54+
$this->containerBuilder->getDefinition('id')
55+
->setTags(['knp_menu.menu_builder' => [['alias' => '']]]);
5556

5657
$this->expectException(\InvalidArgumentException::class);
5758
$this->expectExceptionMessage('The alias is not defined in the "knp_menu.menu_builder" tag for the service "id"');
58-
$this->pass->process($this->containerBuilder->reveal());
59+
60+
$this->pass->process($this->containerBuilder);
5961
}
6062

6163
public function testFailsWhenBuilderMethodIsMissing(): void
6264
{
63-
$this->containerBuilder->findTaggedServiceIds('knp_menu.menu_builder', true)->willReturn(['id' => [['alias' => 'foo']]]);
65+
$this->containerBuilder->getDefinition('id')
66+
->setTags(['knp_menu.menu_builder' => [['alias' => 'foo']]]);
6467

6568
$this->expectException(\InvalidArgumentException::class);
6669
$this->expectExceptionMessage('The method is not defined in the "knp_menu.menu_builder" tag for the service "id"');
67-
$this->pass->process($this->containerBuilder->reveal());
70+
71+
$this->pass->process($this->containerBuilder);
6872
}
6973

7074
public function testFailsWhenMenuAliasIsMissing(): void
7175
{
72-
$this->containerBuilder->findTaggedServiceIds('knp_menu.menu', true)->willReturn(['id' => [['alias' => '']]]);
76+
$this->containerBuilder->getDefinition('menu_id')
77+
->setTags(['knp_menu.menu' => [['alias' => '']]]);
7378

7479
$this->expectException(\InvalidArgumentException::class);
75-
$this->expectExceptionMessage('The alias is not defined in the "knp_menu.menu" tag for the service "id"');
76-
$this->pass->process($this->containerBuilder->reveal());
80+
$this->expectExceptionMessage('The alias is not defined in the "knp_menu.menu" tag for the service "menu_id"');
81+
82+
$this->pass->process($this->containerBuilder);
7783
}
7884

7985
public function testRegisterMenuBuilderAndMenu(): void
8086
{
81-
$this->containerBuilder->findTaggedServiceIds('knp_menu.menu_builder', true)->willReturn([
82-
'id1' => [['alias' => 'foo', 'method' => 'fooMenu'], ['alias' => 'bar', 'method' => 'bar']],
83-
'id2' => [['alias' => 'foo', 'method' => 'fooBar'], ['alias' => 'baz', 'method' => 'bar']],
84-
]);
85-
$this->containerBuilder->findTaggedServiceIds('knp_menu.menu', true)->willReturn(['id3' => [['alias' => 'test']]]);
86-
87-
$menus = [
88-
'foo' => [new ServiceClosureArgument(new Reference('id2')), 'fooBar'],
89-
'bar' => [new ServiceClosureArgument(new Reference('id1')), 'bar'],
90-
'baz' => [new ServiceClosureArgument(new Reference('id2')), 'bar'],
91-
'test' => new ServiceClosureArgument(new Reference('id3')),
87+
$expectedMenuBuilders = [
88+
'foo' => [new ServiceClosureArgument(new Reference('id')), 'fooMenu'],
89+
'bar' => [new ServiceClosureArgument(new Reference('id')), 'bar'],
90+
'baz' => new ServiceClosureArgument(new Reference('menu_id')),
9291
];
93-
$this->definition->replaceArgument(0, $menus)->shouldBeCalled();
9492

95-
$this->pass->process($this->containerBuilder->reveal());
93+
$this->pass->process($this->containerBuilder);
94+
95+
$menuBuilders = $this->containerBuilder->getDefinition('knp_menu.menu_provider.lazy')->getArgument(0);
96+
97+
$this->assertEquals($expectedMenuBuilders, $menuBuilders);
98+
$this->assertFalse($this->containerBuilder->hasDefinition('knp_menu.menu_provider.builder_service'));
9699
}
97100

98101
public function testMenuWinsOverBuilder(): void
99102
{
100-
$this->containerBuilder->findTaggedServiceIds('knp_menu.menu_builder', true)->willReturn([
101-
'id1' => [['alias' => 'foo', 'method' => 'fooMenu'], ['alias' => 'bar', 'method' => 'bar']],
102-
]);
103-
$this->containerBuilder->findTaggedServiceIds('knp_menu.menu', true)->willReturn(['id3' => [['alias' => 'foo']]]);
104-
105-
$menus = [
106-
'foo' => new ServiceClosureArgument(new Reference('id3')),
107-
'bar' => [new ServiceClosureArgument(new Reference('id1')), 'bar'],
103+
$this->containerBuilder->getDefinition('menu_id')
104+
->setTags(['knp_menu.menu' => [['alias' => 'foo']]]);
105+
106+
$expectedMenuBuilders = [
107+
'foo' => new ServiceClosureArgument(new Reference('menu_id')),
108+
'bar' => [new ServiceClosureArgument(new Reference('id')), 'bar'],
108109
];
109-
$this->definition->replaceArgument(0, $menus)->shouldBeCalled();
110110

111-
$this->pass->process($this->containerBuilder->reveal());
111+
$this->pass->process($this->containerBuilder);
112+
113+
$menuBuilders = $this->containerBuilder->getDefinition('knp_menu.menu_provider.lazy')->getArgument(0);
114+
115+
$this->assertEquals($expectedMenuBuilders, $menuBuilders);
112116
}
113117
}

0 commit comments

Comments
 (0)