7
7
use Prophecy \Prophecy \ObjectProphecy ;
8
8
use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
9
9
use Symfony \Component \DependencyInjection \ContainerBuilder ;
10
+ use Symfony \Component \DependencyInjection \ContainerInterface ;
10
11
use Symfony \Component \DependencyInjection \Reference ;
11
12
use Symfony \Component \DependencyInjection \ServiceLocator ;
12
13
use Zalas \Injector \PHPUnit \Symfony \Compiler \Discovery \PropertyDiscovery ;
13
14
use Zalas \Injector \PHPUnit \Symfony \Compiler \ExposeServicesForTestsPass ;
14
15
use Zalas \Injector \PHPUnit \Tests \Symfony \Compiler \Fixtures \Service1 ;
15
16
use Zalas \Injector \PHPUnit \Tests \Symfony \Compiler \Fixtures \Service2 ;
16
17
use Zalas \Injector \PHPUnit \Tests \Symfony \Compiler \Fixtures \TestCase1 ;
18
+ use Zalas \Injector \PHPUnit \Tests \Symfony \Compiler \Fixtures \TestCase2 ;
17
19
use Zalas \Injector \Service \Property ;
18
20
19
21
class ExposeServicesForTestsPassTest extends TestCase
20
22
{
21
- const SERVICE_LOCATOR_ID = 'app.test.service_locator ' ;
22
-
23
23
/**
24
24
* @var ExposeServicesForTestsPass
25
25
*/
@@ -33,42 +33,49 @@ class ExposeServicesForTestsPassTest extends TestCase
33
33
protected function setUp ()
34
34
{
35
35
$ this ->discovery = $ this ->prophesize (PropertyDiscovery::class);
36
- $ this ->pass = new ExposeServicesForTestsPass (self :: SERVICE_LOCATOR_ID , $ this ->discovery ->reveal ());
36
+ $ this ->pass = new ExposeServicesForTestsPass ($ this ->discovery ->reveal ());
37
37
}
38
38
39
39
public function test_it_is_a_compiler_pass ()
40
40
{
41
41
$ this ->assertInstanceOf (CompilerPassInterface::class, $ this ->pass );
42
42
}
43
43
44
- public function test_it_registers_a_service_locator_for_services_used_in_tests ()
44
+ public function test_it_registers_a_service_locator_for_each_test_case_requiring_service_injection ()
45
45
{
46
46
$ this ->discovery ->run ()->willReturn ([
47
47
new Property (TestCase1::class, 'service1 ' , Service1::class),
48
48
new Property (TestCase1::class, 'service2 ' , Service2::class),
49
+ new Property (TestCase2::class, 'service2 ' , Service2::class),
49
50
]);
50
51
51
52
$ container = new ContainerBuilder ();
52
53
53
54
$ this ->pass ->process ($ container );
54
55
55
- $ this ->assertTrue ($ container ->hasDefinition (self ::SERVICE_LOCATOR_ID ), 'The service locator is registered as a service. ' );
56
- $ this ->assertSame (ServiceLocator::class, $ container ->getDefinition (self ::SERVICE_LOCATOR_ID )->getClass ());
57
- $ this ->assertFalse ($ container ->getDefinition (self ::SERVICE_LOCATOR_ID )->isPrivate (), 'The service locator is registered as a public service. ' );
58
- $ this ->assertTrue ($ container ->getDefinition (self ::SERVICE_LOCATOR_ID )->isPublic (), 'The service locator is registered as a public service. ' );
59
- $ this ->assertTrue ($ container ->getDefinition (self ::SERVICE_LOCATOR_ID )->hasTag ('container.service_locator ' ), 'The service locator is tagged. ' );
60
- $ this ->assertEquals ([Service1::class => new Reference (Service1::class), Service2::class => new Reference (Service2::class)], $ container ->getDefinition (self ::SERVICE_LOCATOR_ID )->getArgument (0 ));
56
+ $ this ->assertTrue ($ container ->hasDefinition (TestCase1::class), 'The first test case service locator is registered as a service. ' );
57
+ $ this ->assertSame (ServiceLocator::class, $ container ->getDefinition (TestCase1::class)->getClass ());
58
+ $ this ->assertSame (ServiceLocator::class, $ container ->getDefinition (TestCase2::class)->getClass ());
59
+ $ this ->assertFalse ($ container ->getDefinition (TestCase1::class)->isPrivate (), 'The first test case service locator is registered as a public service. ' );
60
+ $ this ->assertTrue ($ container ->getDefinition (TestCase1::class)->isPublic (), 'The first test case service locator is registered as a public service. ' );
61
+ $ this ->assertTrue ($ container ->getDefinition (TestCase1::class)->hasTag ('container.service_locator ' ), 'The first case service locator is tagged. ' );
62
+ $ this ->assertEquals ([Service1::class => new Reference (Service1::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE ), Service2::class => new Reference (Service2::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE )], $ container ->getDefinition (TestCase1::class)->getArgument (0 ));
63
+ $ this ->assertTrue ($ container ->hasDefinition (TestCase2::class), 'The second test case service locator is registered as a service. ' );
64
+ $ this ->assertFalse ($ container ->getDefinition (TestCase2::class)->isPrivate (), 'The second test case service locator is registered as a public service. ' );
65
+ $ this ->assertTrue ($ container ->getDefinition (TestCase2::class)->isPublic (), 'The second test case service locator is registered as a public service. ' );
66
+ $ this ->assertTrue ($ container ->getDefinition (TestCase2::class)->hasTag ('container.service_locator ' ), 'The second test case service locator is tagged. ' );
67
+ $ this ->assertEquals ([Service2::class => new Reference (Service2::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE )], $ container ->getDefinition (TestCase2::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE )->getArgument (0 ));
61
68
}
62
69
63
- public function test_it_registers_an_empty_service_locator_if_no_services_were_discovered ()
70
+ public function test_it_only_registers_a_service_locator_if_any_services_were_discovered ()
64
71
{
65
72
$ this ->discovery ->run ()->willReturn ([]);
66
73
67
74
$ container = new ContainerBuilder ();
68
75
69
76
$ this ->pass ->process ($ container );
70
77
71
- $ this ->assertTrue ($ container ->hasDefinition (self :: SERVICE_LOCATOR_ID ), 'The service locator is registered as a service. ' );
72
- $ this ->assertEquals ([], $ container ->getDefinition ( self :: SERVICE_LOCATOR_ID )-> getArgument ( 0 ) , 'No services were registered on the service locator . ' );
78
+ $ this ->assertfalse ($ container ->hasDefinition (TestCase1::class ), 'The first test case service locator is not registered as a service. ' );
79
+ $ this ->assertfalse ( $ container ->hasDefinition (TestCase2::class) , 'The second test case service locator is not registered as a service. ' );
73
80
}
74
81
}
0 commit comments