Skip to content

Commit e915fbf

Browse files
committed
remove Dependency Injection Container usage in Integration Tests
1 parent e9ea7a4 commit e915fbf

File tree

5 files changed

+50
-21
lines changed

5 files changed

+50
-21
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ branches:
2828
- bad_tests
2929
- integration_infra_medium_domain
3030
- integration_infra_medium_domain_wiremock
31+
- integration_infra_medium_domain_no_di
3132

3233
before_install:
3334
- make preinstall

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ For a bit of theory, see [De CRUD à DDD, comment Meetic a sauvé son legacy](ht
99

1010
## Steps by step refactoring
1111

12+
:warning: **Warning** : Only steps 1 & 2 are really considered *bad*. Next steps just show different testing styles.
13+
1214
1. [bad_implementation](https://github.com/JMLamodiere/tdd-demo-forumphp2020/tree/bad_implementation) branch
1315
contains :
1416
- Architecture mistakes according to [Hexagonal architecture](https://alistair.cockburn.us/hexagonal-architecture/) (aka Port & Adapters)
@@ -24,6 +26,10 @@ Many obscure changes are required in the tests, proving they do not help much du
2426
[(see Pull Request)](https://github.com/JMLamodiere/tdd-demo-forumphp2020/pull/14)
2527
only replaces [Guzzle MockHandler](https://docs.guzzlephp.org/en/stable/testing.html) with [Wiremock](#wiremock),
2628
decoupling HTTP tests with the library being used for HTTP calls.
29+
1. [integration_infra_medium_domain_no_di](https://github.com/JMLamodiere/tdd-demo-forumphp2020/tree/integration_infra_medium_domain_no_di) branch
30+
[(see Pull Request)](https://github.com/JMLamodiere/tdd-demo-forumphp2020/pull/15)
31+
removes [Dependency Injection Container](https://www.loosecouplings.com/2011/01/dependency-injection-using-di-container.html)
32+
usage and manually build tested classes instead.
2733
1. (todo...)
2834

2935
## API documentation

tests/Infrastructure/Database/PostgresRunningSessionRepositoryTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,27 @@
66

77
use App\Domain\RunningSession;
88
use Doctrine\DBAL\Connection;
9-
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
9+
use Doctrine\DBAL\DriverManager;
10+
use PHPUnit\Framework\TestCase;
1011

1112
/**
1213
* @group integration
1314
*/
14-
class PostgresRunningSessionRepositoryTest extends KernelTestCase
15+
class PostgresRunningSessionRepositoryTest extends TestCase
1516
{
1617
private Connection $dbal;
1718
private PostgresRunningSessionRepository $repository;
1819

1920
protected function setUp(): void
2021
{
21-
self::bootKernel();
22-
$this->dbal = self::$container->get('doctrine.dbal.default_connection');
23-
$this->repository = self::$container->get(PostgresRunningSessionRepository::class);
22+
//see https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#getting-a-connection
23+
$connectionParams = [
24+
// Same as .env.test
25+
'url' => 'postgres://forumphp:forumphp@database:5432/forumphp?sslmode=disable&charset=utf8',
26+
];
27+
$this->dbal = DriverManager::getConnection($connectionParams);
28+
29+
$this->repository = new PostgresRunningSessionRepository($this->dbal);
2430
$this->resetState();
2531
}
2632

tests/Infrastructure/Http/RestWeatherProviderTest.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,34 @@
55
namespace App\Infrastructure\Http;
66

77
use App\Domain\CannotGetCurrentTemperature;
8-
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
8+
use GuzzleHttp\Client;
9+
use PHPUnit\Framework\TestCase;
910
use WireMock\Client\WireMock;
1011

11-
class RestWeatherProviderTest extends KernelTestCase
12+
class RestWeatherProviderTest extends TestCase
1213
{
1314
private RestWeatherProvider $provider;
1415
private WireMock $wireMock;
1516
private string $currentConditionUri;
1617

1718
protected function setUp(): void
1819
{
19-
self::bootKernel();
20-
21-
$this->provider = self::$container->get(RestWeatherProvider::class);
22-
23-
$this->wireMock = self::$container->get(WireMock::class);
20+
$accuweatherApiKey = 'myApiKey';
21+
// See docker-compose.yml
22+
$host = 'wiremock';
23+
$port = '8080';
24+
25+
$this->provider = new RestWeatherProvider(
26+
// See https://docs.guzzlephp.org/en/stable/quickstart.html#creating-a-client
27+
new Client(['base_uri' => "http://$host:$port/"]),
28+
$accuweatherApiKey,
29+
new CurrentConditionDeserializer()
30+
);
31+
32+
// See docker-compose.yml
33+
$this->wireMock = WireMock::create($host, $port);
2434
self::assertTrue($this->wireMock->isAlive(), 'Wiremock should be alive');
2535

26-
$accuweatherApiKey = self::$container->getParameter('accuweather.apikey');
2736
$this->currentConditionUri = '/currentconditions/v1/623?apikey='.$accuweatherApiKey;
2837
}
2938

tests/Infrastructure/Symfony/Controller/RunningSessionControllerTest.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,33 @@
77
use App\Application\Command\RegisterRunningSession;
88
use App\Application\Command\RegisterRunningSessionHandler;
99
use App\Domain\RunningSession;
10+
use App\Infrastructure\Symfony\Serializer\RegisterRunningSessionDeserializer;
11+
use App\Infrastructure\Symfony\Serializer\RunningSessionNormalizer;
12+
use PHPUnit\Framework\TestCase;
1013
use Prophecy\Argument;
1114
use Prophecy\PhpUnit\ProphecyTrait;
1215
use Prophecy\Prophecy\ObjectProphecy;
13-
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1416
use Symfony\Component\HttpFoundation\Request;
1517
use Symfony\Component\HttpFoundation\Response;
1618

17-
class RunningSessionControllerTest extends KernelTestCase
19+
class RunningSessionControllerTest extends TestCase
1820
{
1921
use ProphecyTrait;
2022

2123
/** @var ObjectProphecy|RegisterRunningSessionHandler */
2224
private $registerRunningSessionHandler;
2325

26+
private RunningSessionController $controller;
27+
2428
protected function setUp(): void
2529
{
26-
self::bootKernel();
27-
2830
$this->registerRunningSessionHandler = $this->prophesize(RegisterRunningSessionHandler::class);
29-
self::$container->set(RegisterRunningSessionHandler::class, $this->registerRunningSessionHandler->reveal());
31+
32+
$this->controller = new RunningSessionController(
33+
new RegisterRunningSessionDeserializer(),
34+
new RunningSessionNormalizer(),
35+
$this->registerRunningSessionHandler->reveal()
36+
);
3037
}
3138

3239
public function testPutRouteSendsCommandToHandlerAndDisplayItsResult()
@@ -35,7 +42,7 @@ public function testPutRouteSendsCommandToHandlerAndDisplayItsResult()
3542
$this->givenHandlerResponseIs(new RunningSession(42, 5.5, 'Adadis Turbo2', 37.2));
3643

3744
//When (Act)
38-
$response = $this->whenISendThisRequest(Request::create('/runningsessions/42', 'PUT', [], [], [], [], <<<EOD
45+
$response = $this->whenISendThisRequest('42', Request::create('uri_not_used', 'method_not_used', [], [], [], [], <<<EOD
3946
{
4047
"id": 42,
4148
"distance": 5.5,
@@ -63,9 +70,9 @@ private function givenHandlerResponseIs(RunningSession $handlerResponse)
6370
->willReturn($handlerResponse);
6471
}
6572

66-
private function whenISendThisRequest(Request $request): Response
73+
private function whenISendThisRequest(string $id, Request $request): Response
6774
{
68-
return self::$kernel->handle($request);
75+
return $this->controller->put($id, $request);
6976
}
7077

7178
private function thenThisCommandHasBeenSentToHandler(RegisterRunningSession $expectedCommand)

0 commit comments

Comments
 (0)