Skip to content

Commit 96391ea

Browse files
authored
Merge pull request #108 from jrushlow/replace/micro-kernel-trait
[tests] replace test kernel abstraction with simpilier ResetPasswordTestKernel
2 parents ac39892 + 2395010 commit 96391ea

File tree

6 files changed

+162
-145
lines changed

6 files changed

+162
-145
lines changed

tests/Fixtures/AbstractResetPasswordTestKernel.php

Lines changed: 0 additions & 114 deletions
This file was deleted.

tests/FunctionalTests/Command/ResetPasswordRemoveExpiredCommandTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
use Symfony\Component\Console\Application;
1717
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
1818
use Symfony\Component\Console\Tester\CommandTester;
19-
use SymfonyCasts\Bundle\ResetPassword\Tests\Fixtures\AbstractResetPasswordTestKernel;
2019
use SymfonyCasts\Bundle\ResetPassword\Tests\Fixtures\Entity\ResetPasswordTestFixtureRequest;
20+
use SymfonyCasts\Bundle\ResetPassword\Tests\ResetPasswordTestKernel;
2121

2222
/**
2323
* @author Jesse Rushlow <[email protected]>
@@ -39,7 +39,7 @@ final class ResetPasswordRemoveExpiredCommandTest extends TestCase
3939

4040
protected function setUp(): void
4141
{
42-
$kernel = new CommandKernel();
42+
$kernel = new ResetPasswordTestKernel();
4343
$kernel->boot();
4444

4545
$container = $kernel->getContainer();
@@ -101,7 +101,3 @@ private function configureDatabase(): void
101101
$tool->createSchema($metaData->getAllMetadata());
102102
}
103103
}
104-
105-
class CommandKernel extends AbstractResetPasswordTestKernel
106-
{
107-
}

tests/FunctionalTests/Persistence/ResetPasswordRequestRepositoryTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
use Doctrine\ORM\Tools\SchemaTool;
1414
use Doctrine\Persistence\ObjectManager;
1515
use PHPUnit\Framework\TestCase;
16-
use SymfonyCasts\Bundle\ResetPassword\Tests\Fixtures\AbstractResetPasswordTestKernel;
1716
use SymfonyCasts\Bundle\ResetPassword\Tests\Fixtures\Entity\ResetPasswordTestFixtureRequest;
1817
use SymfonyCasts\Bundle\ResetPassword\Tests\Fixtures\Entity\ResetPasswordTestFixtureUser;
1918
use SymfonyCasts\Bundle\ResetPassword\Tests\Fixtures\ResetPasswordTestFixtureRequestRepository;
19+
use SymfonyCasts\Bundle\ResetPassword\Tests\ResetPasswordTestKernel;
2020

2121
/**
2222
* @author Jesse Rushlow <[email protected]>
@@ -41,7 +41,7 @@ final class ResetPasswordRequestRepositoryTest extends TestCase
4141
*/
4242
protected function setUp(): void
4343
{
44-
$kernel = new ResetPasswordFunctionalKernel();
44+
$kernel = new ResetPasswordTestKernel();
4545
$kernel->boot();
4646

4747
$container = $kernel->getContainer();
@@ -220,7 +220,3 @@ private function configureDatabase(): void
220220
$tool->createSchema($metaData->getAllMetadata());
221221
}
222222
}
223-
224-
class ResetPasswordFunctionalKernel extends AbstractResetPasswordTestKernel
225-
{
226-
}

tests/IntegrationTests/ResetPasswordInterfaceAutowireTest.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,37 @@
1010
namespace SymfonyCasts\Bundle\ResetPassword\Tests\IntegrationTests;
1111

1212
use PHPUnit\Framework\TestCase;
13-
use Symfony\Component\Config\Loader\LoaderInterface;
1413
use Symfony\Component\DependencyInjection\ContainerBuilder;
1514
use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
16-
use SymfonyCasts\Bundle\ResetPassword\Tests\Fixtures\AbstractResetPasswordTestKernel;
15+
use SymfonyCasts\Bundle\ResetPassword\Tests\ResetPasswordTestKernel;
1716

1817
/**
1918
* @author Jesse Rushlow <[email protected]>
2019
* @author Ryan Weaver <[email protected]>
2120
*/
22-
class ResetPasswordInterfaceAutowireTest extends TestCase
21+
final class ResetPasswordInterfaceAutowireTest extends TestCase
2322
{
2423
public function testResetPasswordInterfaceIsAutowiredByContainer(): void
2524
{
26-
$kernel = new ResetPasswordIntegrationKernel();
25+
$builder = new ContainerBuilder();
26+
$builder->autowire(ResetPasswordAutowireTest::class)
27+
->setPublic(true)
28+
;
29+
30+
$kernel = new ResetPasswordTestKernel($builder);
2731
$kernel->boot();
32+
2833
$container = $kernel->getContainer();
2934
$container->get(ResetPasswordAutowireTest::class);
3035

3136
$this->expectNotToPerformAssertions();
3237
}
3338
}
3439

35-
class ResetPasswordIntegrationKernel extends AbstractResetPasswordTestKernel
36-
{
37-
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
38-
{
39-
parent::configureContainer($container, $loader);
40-
41-
$container->autowire(ResetPasswordAutowireTest::class)
42-
->setPublic(true)
43-
;
44-
}
45-
}
46-
47-
class ResetPasswordAutowireTest
40+
/**
41+
* @internal
42+
*/
43+
final class ResetPasswordAutowireTest
4844
{
4945
public function __construct(ResetPasswordHelperInterface $helper)
5046
{

tests/IntegrationTests/ResetPasswordServiceDefinitionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use PHPUnit\Framework\TestCase;
1313
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
15-
use SymfonyCasts\Bundle\ResetPassword\Tests\Fixtures\AbstractResetPasswordTestKernel;
15+
use SymfonyCasts\Bundle\ResetPassword\Tests\ResetPasswordTestKernel;
1616

1717
/**
1818
* @author Jesse Rushlow <[email protected]>
@@ -71,7 +71,7 @@ public function process(ContainerBuilder $container)
7171
/**
7272
* @internal
7373
*/
74-
final class ResetPasswordDefinitionTestKernel extends AbstractResetPasswordTestKernel
74+
final class ResetPasswordDefinitionTestKernel extends ResetPasswordTestKernel
7575
{
7676
public $compilerPass;
7777

tests/ResetPasswordTestKernel.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the SymfonyCasts ResetPasswordBundle package.
5+
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace SymfonyCasts\Bundle\ResetPassword\Tests;
11+
12+
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
13+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
14+
use Symfony\Component\Config\Loader\LoaderInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
17+
use Symfony\Component\HttpKernel\Kernel;
18+
use Symfony\Component\Routing\Route;
19+
use Symfony\Component\Routing\RouteCollection;
20+
use SymfonyCasts\Bundle\ResetPassword\SymfonyCastsResetPasswordBundle;
21+
use SymfonyCasts\Bundle\ResetPassword\Tests\Fixtures\ResetPasswordTestFixtureRequestRepository;
22+
23+
/**
24+
* @author Jesse Rushlow <[email protected]>
25+
* @author Ryan Weaver <[email protected]>
26+
*
27+
* @internal
28+
*/
29+
class ResetPasswordTestKernel extends Kernel
30+
{
31+
private $builder;
32+
private $routes;
33+
private $extraBundles;
34+
35+
/**
36+
* @param array $routes Routes to be added to the container e.g. ['name' => 'path']
37+
* @param BundleInterface[] $bundles Additional bundles to be registered e.g. [new Bundle()]
38+
*/
39+
public function __construct(ContainerBuilder $builder = null, array $routes = [], array $bundles = [])
40+
{
41+
$this->builder = $builder;
42+
$this->routes = $routes;
43+
$this->extraBundles = $bundles;
44+
45+
parent::__construct('test', true);
46+
}
47+
48+
public function registerBundles(): iterable
49+
{
50+
return \array_merge(
51+
$this->extraBundles,
52+
[
53+
new FrameworkBundle(),
54+
new SymfonyCastsResetPasswordBundle(),
55+
new DoctrineBundle(),
56+
]
57+
);
58+
}
59+
60+
public function registerContainerConfiguration(LoaderInterface $loader): void
61+
{
62+
if (null === $this->builder) {
63+
$this->builder = new ContainerBuilder();
64+
}
65+
66+
$builder = $this->builder;
67+
68+
$loader->load(function (ContainerBuilder $container) use ($builder) {
69+
$container->merge($builder);
70+
$container->loadFromExtension(
71+
'framework',
72+
[
73+
'secret' => 'foo',
74+
'router' => [
75+
'resource' => 'kernel::loadRoutes',
76+
'type' => 'service',
77+
'utf8' => true,
78+
],
79+
]
80+
);
81+
82+
$container->loadFromExtension('doctrine', [
83+
'dbal' => [
84+
'driver' => 'pdo_sqlite',
85+
'url' => 'sqlite:///'.$this->getCacheDir().'/app.db',
86+
],
87+
'orm' => [
88+
'auto_generate_proxy_classes' => true,
89+
'auto_mapping' => true,
90+
'mappings' => [
91+
'App' => [
92+
'is_bundle' => false,
93+
'type' => 'annotation',
94+
'dir' => 'tests/Fixtures/Entity/',
95+
'prefix' => 'SymfonyCasts\Bundle\ResetPassword\Tests\Fixtures\Entity',
96+
'alias' => 'App',
97+
],
98+
],
99+
],
100+
]);
101+
102+
$container->register(ResetPasswordTestFixtureRequestRepository::class)
103+
->setAutoconfigured(true)
104+
->setAutowired(true)
105+
;
106+
107+
$container->loadFromExtension('symfonycasts_reset_password', [
108+
'request_password_repository' => ResetPasswordTestFixtureRequestRepository::class,
109+
]);
110+
111+
$container->register('kernel', static::class)
112+
->setPublic(true)
113+
;
114+
115+
$kernelDefinition = $container->getDefinition('kernel');
116+
$kernelDefinition->addTag('routing.route_loader');
117+
});
118+
}
119+
120+
/**
121+
* @internal
122+
*/
123+
public function loadRoutes(LoaderInterface $loader): RouteCollection
124+
{
125+
$routes = new RouteCollection();
126+
127+
foreach ($this->routes as $name => $path) {
128+
$routes->add($name, new Route($path));
129+
}
130+
131+
return $routes;
132+
}
133+
134+
public function getCacheDir()
135+
{
136+
return \sys_get_temp_dir().'/cache'.\spl_object_hash($this);
137+
}
138+
139+
public function getLogDir()
140+
{
141+
return \sys_get_temp_dir().'/logs'.\spl_object_hash($this);
142+
}
143+
}

0 commit comments

Comments
 (0)