Skip to content

Commit cec0e48

Browse files
Updated example
1 parent bc16653 commit cec0e48

11 files changed

+168
-38
lines changed

example/composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
"require": {
33
"spatie/phpunit-snapshot-assertions": "^0.4.0",
44
"phpunit/phpunit": "^6.0"
5+
},
6+
"autoload": {
7+
"psr-4": {
8+
"App\\": "src/"
9+
}
510
}
611
}

example/src/Order.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace App;
4+
5+
class Order
6+
{
7+
/** @var int */
8+
private $id;
9+
10+
/** @var string */
11+
private $email;
12+
13+
/** @var bool */
14+
private $paid;
15+
16+
/** @var \App\OrderLine[] */
17+
private $orderLines;
18+
19+
public function __construct(int $id, string $email, bool $paid, array $orderLines) {
20+
$this->id = $id;
21+
$this->email = $email;
22+
$this->paid = $paid;
23+
$this->orderLines = $orderLines;
24+
}
25+
26+
public function id(): int
27+
{
28+
return $this->id;
29+
}
30+
31+
public function email(): string
32+
{
33+
return $this->email;
34+
}
35+
36+
public function paid(): bool
37+
{
38+
return $this->paid;
39+
}
40+
41+
public function orderLines(): array
42+
{
43+
return $this->orderLines;
44+
}
45+
}

example/src/OrderLine.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App;
4+
5+
class OrderLine
6+
{
7+
/** @var int */
8+
private $id;
9+
10+
/** @var string */
11+
private $description;
12+
13+
/** @var int */
14+
private $unitPrice;
15+
16+
/** @var int */
17+
private $quantity;
18+
19+
public function __construct(int $id, string $description, int $unitPrice, int $quantity) {
20+
$this->id = $id;
21+
$this->description = $description;
22+
$this->unitPrice = $unitPrice;
23+
$this->quantity = $quantity;
24+
}
25+
26+
public function id(): int
27+
{
28+
return $this->id;
29+
}
30+
31+
public function description(): string
32+
{
33+
return $this->description;
34+
}
35+
36+
public function unitPrice(): int
37+
{
38+
return $this->unitPrice;
39+
}
40+
41+
public function quantity(): int
42+
{
43+
return $this->quantity;
44+
}
45+
46+
public function totalPrice(): int
47+
{
48+
return $this->unitPrice * $this->quantity;
49+
}
50+
}

example/src/OrderSerializer.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App;
4+
5+
class OrderSerializer
6+
{
7+
public function serialize(Order $order): string
8+
{
9+
$data = [
10+
'id' => $order->id(),
11+
'email' => $order->email(),
12+
'paid' => $order->paid() ? 1 : 0,
13+
'items' => [],
14+
];
15+
16+
foreach ($order->orderLines() as $orderLine) {
17+
$data['items'][] = [
18+
'id' => $orderLine->id(),
19+
'description' => $orderLine->description(),
20+
'price' => $orderLine->totalPrice(),
21+
];
22+
}
23+
24+
return json_encode($data);
25+
}
26+
}

example/tests/ExampleTest.php

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use App\Order;
4+
use App\OrderLine;
5+
use App\OrderSerializer;
6+
use PHPUnit\Framework\TestCase;
7+
use Spatie\Snapshots\MatchesSnapshots;
8+
9+
class OrderSerializerTest extends TestCase
10+
{
11+
use MatchesSnapshots;
12+
13+
/** @test */
14+
public function it_can_serialize_an_order()
15+
{
16+
$orderSerializer = new OrderSerializer();
17+
18+
$order = new Order(1, 'sebastian@spatie.be', true, [
19+
new OrderLine(1, 'Sublime Text License', 70, 3),
20+
new OrderLine(2, 'PHPStorm License', 199, 2),
21+
]);
22+
23+
$this->assertMatchesJsonSnapshot($orderSerializer->serialize($order));
24+
}
25+
}

example/tests/__snapshots__/ExampleTest__test_it_matches_a_string__1.php

Lines changed: 0 additions & 1 deletion
This file was deleted.

example/tests/__snapshots__/ExampleTest__test_it_matches_an_array__1.php

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

example/tests/__snapshots__/ExampleTest__test_it_matches_json__1.json

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

example/tests/__snapshots__/ExampleTest__test_it_matches_xml__1.xml

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

0 commit comments

Comments
 (0)