Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ install:

script:
- make test

after_script:
- make prune
4 changes: 3 additions & 1 deletion docs/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ components:
allOf:
# Inherit PutRunningSession
- $ref: '#/components/schemas/PutRunningSession'
required: [ temperatureCelcius ]
required: [ temperatureCelcius, is_freezing ]
properties:
temperatureCelcius:
type: number
format: float
example: 37.2
is_freezing:
type: boolean

RunningSessionId:
type: integer
Expand Down
3 changes: 2 additions & 1 deletion src/Application/Command/RegisterRunningSessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Domain\RunningSession;
use App\Domain\RunningSessionRepository;
use App\Domain\Temperature;
use App\Domain\WeatherProvider;

class RegisterRunningSessionHandler
Expand All @@ -27,7 +28,7 @@ public function handle(RegisterRunningSession $command): RunningSession
$command->getId(),
$command->getDistance(),
$command->getShoes(),
$currentTemperature
new Temperature($currentTemperature)
);

$this->repository->add($session);
Expand Down
13 changes: 9 additions & 4 deletions src/Domain/RunningSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ class RunningSession
private int $id;
private float $distance;
private string $shoes;
private float $metricTemperature;
private Temperature $temperature;

public function __construct(int $id, float $distance, string $shoes, float $metricTemperature)
public function __construct(int $id, float $distance, string $shoes, Temperature $temperature)
{
$this->id = $id;
$this->distance = $distance;
$this->shoes = $shoes;
$this->metricTemperature = $metricTemperature;
$this->temperature = $temperature;
}

public function getId(): int
Expand All @@ -36,6 +36,11 @@ public function getShoes(): string

public function getMetricTemperature(): float
{
return $this->metricTemperature;
return $this->temperature->getMetricTemperature();
}

public function isFreezing(): bool
{
return $this->temperature->isFreezing();
}
}
35 changes: 35 additions & 0 deletions src/Domain/Temperature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Domain;

use Webmozart\Assert\Assert;

class Temperature
{
private const ABSOLUTE_ZERO = -273.15;

private float $metricTemperature;

public function __construct(float $metricTemperature)
{
Assert::greaterThanEq(
$metricTemperature,
self::ABSOLUTE_ZERO,
"Cannot create temperature below absolute zero: $metricTemperature"
);

$this->metricTemperature = $metricTemperature;
}

public function getMetricTemperature(): float
{
return $this->metricTemperature;
}

public function isFreezing(): bool
{
return $this->metricTemperature < 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function serialize(RunningSession $session): string
'distance' => $session->getDistance(),
'shoes' => $session->getShoes(),
'temperatureCelcius' => $session->getMetricTemperature(),
'is_freezing' => $session->isFreezing(),
];

return json_encode($data, JSON_THROW_ON_ERROR);
Expand Down
2 changes: 1 addition & 1 deletion tests/Domain/RunningSessionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public static function create(
string $shoes = 'shoes_value_not_used',
float $metricTemperature = 98.76
): RunningSession {
return new RunningSession($id, $distance, $shoes, $metricTemperature);
return new RunningSession($id, $distance, $shoes, new Temperature($metricTemperature));
}
}
34 changes: 34 additions & 0 deletions tests/Domain/TemperatureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Domain;

use PHPUnit\Framework\TestCase;

class TemperatureTest extends TestCase
{
public function testIsNotFreezingAboveZero()
{
//Given
$temperature = new Temperature(25.7);

//When
$result = $temperature->isFreezing();

//Then
self::assertFalse($result, 'temperature should not be freezing');
}

public function testIsFreezingBelowZero()
{
//Given
$temperature = new Temperature(-12);

//When
$result = $temperature->isFreezing();

//Then
self::assertTrue($result, 'temperature should be freezing');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function testRunningSessionIsConvertedToJson()
"id": 42,
"distance": 5.5,
"shoes": "Adadis Turbo2",
"temperatureCelcius": 37.2
"temperatureCelcius": 37.2,
"is_freezing": false
}
EOD;
self::assertJsonStringEqualsJsonString($expectedJson, $result);
Expand Down