Skip to content

Commit 7bc90cd

Browse files
authored
Merge pull request #12 from JMLamodiere/bad_tests
Fix hexagonal mistakes, but tests are still too much coupled with implem
2 parents 2c2de6e + 854dddb commit 7bc90cd

16 files changed

+112
-126
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ branches:
2424
only:
2525
- main
2626
- bad_implementation
27+
- bad_tests
2728

2829
before_install:
2930
- make preinstall

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22

33
[![Build Status](https://travis-ci.com/JMLamodiere/tdd-demo-forumphp2020.svg?branch=main)](https://travis-ci.com/JMLamodiere/tdd-demo-forumphp2020)
44

5-
Live coding examples used during Forum PHP 2020 talk (in :fr: French) : [Trop de mock tue le test : ce que l'archi hexagonale a changé](https://event.afup.org/forum-php-2020/programme-forum-php-2020/#3414)
5+
Live coding examples used during Forum PHP 2020 talk (:fr: french) : [Trop de mock tue le test : ce que l'archi hexagonale a changé](https://event.afup.org/forum-php-2020/programme-forum-php-2020/#3414)
6+
(video not yet published).
67

7-
:warning: **WARNING:** The [bad_implementation](https://github.com/JMLamodiere/tdd-demo-forumphp2020/tree/bad_implementation)
8-
branch contains example of bad practices ! See [main](https://github.com/JMLamodiere/tdd-demo-forumphp2020) branch
9-
for correct implementation.
8+
For a bit of theory, see [De CRUD à DDD, comment Meetic a sauvé son legacy](https://afup.org/talks/3037-de-crud-a-ddd-comment-meetic-a-sauve-son-legacy) (:fr: french)
9+
10+
## Steps by step refactoring
11+
12+
1. [bad_implementation](https://github.com/JMLamodiere/tdd-demo-forumphp2020/tree/bad_implementation) branch
13+
contains :
14+
- Architecture mistakes according to [Hexagonal architecture](https://alistair.cockburn.us/hexagonal-architecture/) (aka Port & Adapters)
15+
- Tests too much coupled with implementation details, and an incorrect usage of mocks
16+
1. [bad_tests](https://github.com/JMLamodiere/tdd-demo-forumphp2020/tree/bad_tests) branch
17+
[(see Pull Request)](https://github.com/JMLamodiere/tdd-demo-forumphp2020/pull/12) only fixes (some) hexagonal mistakes.
18+
Many obscure changes are required in the tests, proving they do not help much during refactoring
19+
1. (todo...)
1020

1121
## API documentation
1222

src/Application/Command/RegisterRunningSessionHandler.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,30 @@
44

55
namespace App\Application\Command;
66

7-
use App\Domain\RestWeatherProviderInterface;
87
use App\Domain\RunningSession;
98
use App\Domain\RunningSessionRepository;
10-
use Webmozart\Assert\Assert;
9+
use App\Domain\WeatherProvider;
1110

1211
class RegisterRunningSessionHandler
1312
{
14-
private RestWeatherProviderInterface $weatherProvider;
13+
private WeatherProvider $weatherProvider;
1514
private RunningSessionRepository $repository;
1615

17-
public function __construct(RestWeatherProviderInterface $weatherProvider, RunningSessionRepository $repository)
16+
public function __construct(WeatherProvider $weatherProvider, RunningSessionRepository $repository)
1817
{
1918
$this->weatherProvider = $weatherProvider;
2019
$this->repository = $repository;
2120
}
2221

2322
public function handle(RegisterRunningSession $command): RunningSession
2423
{
25-
$currentCondition = $this->weatherProvider->callGetCurrentCondition();
26-
$observations = $currentCondition->getObservations();
27-
Assert::notEmpty($observations, 'observations should not be empty');
28-
$observation = reset($observations);
24+
$currentTemperature = $this->weatherProvider->getCurrentCelciusTemperature();
2925

3026
$session = new RunningSession(
3127
$command->getId(),
3228
$command->getDistance(),
3329
$command->getShoes(),
34-
$observation->getMetricTemperature()
30+
$currentTemperature
3531
);
3632

3733
$this->repository->add($session);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Domain;
6+
7+
use RuntimeException;
8+
9+
class CannotGetCurrentTemperature extends RuntimeException
10+
{
11+
}

src/Domain/RestWeatherProviderInterface.php

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

src/Domain/RunningSessionRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
interface RunningSessionRepository
88
{
9-
public function add(RunningSession $session): int;
9+
public function add(RunningSession $session): void;
1010
}

src/Domain/WeatherProvider.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Domain;
6+
7+
interface WeatherProvider
8+
{
9+
/**
10+
* @throws CannotGetCurrentTemperature
11+
*/
12+
public function getCurrentCelciusTemperature(): float;
13+
}

src/Infrastructure/Database/PostgresRunningSessionRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct(Connection $dbal)
1919
$this->dbal = $dbal;
2020
}
2121

22-
public function add(RunningSession $session): int
22+
public function add(RunningSession $session): void
2323
{
2424
$queryBuilder = $this->dbal->createQueryBuilder();
2525

@@ -31,6 +31,6 @@ public function add(RunningSession $session): int
3131
->setValue('TEMPERATURE_CELCIUS', ':celcius')->setParameter(':celcius', $session->getMetricTemperature())
3232
;
3333

34-
return $queryBuilder->execute();
34+
$queryBuilder->execute();
3535
}
3636
}

src/Infrastructure/Http/CurrentCondition.php

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

src/Infrastructure/Http/CurrentConditionDeserializer.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,24 @@
99

1010
class CurrentConditionDeserializer
1111
{
12-
public function deserialize(ResponseInterface $response): CurrentCondition
12+
public function deserialize(ResponseInterface $response): float
1313
{
1414
$content = $response->getBody()->getContents();
1515
$data = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
1616

1717
Assert::isArray($data, 'Data root should be array');
18+
Assert::notEmpty($data, 'Data root should not be empty');
19+
$firstObservation = reset($data);
1820

19-
return new CurrentCondition(array_map([$this, 'denormalizeObservation'], $data));
21+
return $this->denormalizeObservation($firstObservation);
2022
}
2123

22-
private function denormalizeObservation(array $data): Observation
24+
private function denormalizeObservation(array $data): float
2325
{
2426
Assert::keyExists($data, 'Temperature', 'missing Temperature key');
2527
Assert::keyExists($data['Temperature'], 'Metric', 'missing Temperature.Metric key');
2628
Assert::keyExists($data['Temperature']['Metric'], 'Value', 'missing Temperature.Metric.Value key');
2729

28-
return new Observation($data['Temperature']['Metric']['Value']);
30+
return $data['Temperature']['Metric']['Value'];
2931
}
3032
}

0 commit comments

Comments
 (0)