Skip to content

Commit 345c82e

Browse files
authoredMar 5, 2018
Merge pull request #2 from jakzal/feature/refresh-container-on-test-case-change
Refresh the container when a test case changes
2 parents 0428c04 + 02c0a51 commit 345c82e

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed
 

‎depfile.yml

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ layers:
3030
collectors:
3131
- type: className
3232
regex: ^Psr\\Container\\.*
33+
- name: Symfony Config
34+
collectors:
35+
- type: className
36+
regex: ^Symfony\\Component\\Config\\.*
3337
- name: Symfony DependencyInjection
3438
collectors:
3539
- type: className
@@ -61,6 +65,8 @@ layers:
6165
regex: ^Psr\\Container\\.*
6266
- type: className
6367
regex: ^PHPUnit\\Framework\\.*
68+
- type: className
69+
regex: ^Symfony\\Component\\Config\\.*
6470
- type: className
6571
regex: ^Symfony\\Component\\DependencyInjection\\.*
6672
- type: className
@@ -70,6 +76,7 @@ ruleset:
7076
- Injector Factory
7177
- Injector Service
7278
- TestCase
79+
- Symfony Config
7380
- Symfony DependencyInjection
7481
Symfony TestCase:
7582
- Psr Container

‎src/Symfony/Compiler/ExposeServicesForTestsPass.php

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Zalas\Injector\PHPUnit\Symfony\Compiler;
55

6+
use Symfony\Component\Config\Resource\ReflectionClassResource;
67
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
78
use Symfony\Component\DependencyInjection\ContainerBuilder;
89
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -30,6 +31,8 @@ public function process(ContainerBuilder $container): void
3031
->setPublic(true)
3132
->addTag('container.service_locator')
3233
->addArgument($references);
34+
35+
$container->addResource(new ReflectionClassResource(new \ReflectionClass($testClass)));
3336
}
3437
}
3538

‎tests/Symfony/Compiler/ExposeServicesForTestsPassTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use PHPUnit\Framework\TestCase;
77
use Prophecy\Prophecy\ObjectProphecy;
8+
use Symfony\Component\Config\Resource\ReflectionClassResource;
89
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
910
use Symfony\Component\DependencyInjection\ContainerBuilder;
1011
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -78,4 +79,23 @@ public function test_it_only_registers_a_service_locator_if_any_services_were_di
7879
$this->assertfalse($container->hasDefinition(TestCase1::class), 'The first test case service locator is not registered as a service.');
7980
$this->assertfalse($container->hasDefinition(TestCase2::class), 'The second test case service locator is not registered as a service.');
8081
}
82+
83+
public function test_it_registers_test_cases_as_container_resources()
84+
{
85+
$this->discovery->run()->willReturn([
86+
new Property(TestCase1::class, 'service1', Service1::class),
87+
new Property(TestCase1::class, 'service2', Service2::class),
88+
new Property(TestCase2::class, 'service2', Service2::class),
89+
]);
90+
91+
$container = new ContainerBuilder();
92+
$this->pass->process($container);
93+
94+
$resources = $container->getResources();
95+
96+
$this->assertCount(2, $resources);
97+
$this->assertContainsOnlyInstancesOf(ReflectionClassResource::class, $resources);
98+
$this->assertRegExp('#'.\preg_quote(TestCase1::class, '#').'#', (string) $resources[0]);
99+
$this->assertRegExp('#'.\preg_quote(TestCase2::class, '#').'#', (string) $resources[1]);
100+
}
81101
}

0 commit comments

Comments
 (0)
Please sign in to comment.