Skip to content

Commit 6a47e43

Browse files
committed
Add ObjectDriver
fixes #46 Add an ObjectDriver that relies on Symfony Serializer and will snapshot public properties and the return of public getters into readable JSON. This was motivated by offering a more convenient alternative to the current VarDriver.
1 parent 0252c54 commit 6a47e43

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"php": "^7.4",
2424
"ext-dom": "*",
2525
"phpunit/phpunit": "^8.3",
26+
"symfony/property-access": "^4.0|^5.0",
27+
"symfony/serializer": "^4.0|^5.0",
2628
"symfony/yaml": "^4.0|^5.0"
2729
},
2830
"autoload": {

src/Drivers/ObjectDriver.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Spatie\Snapshots\Drivers;
4+
5+
use PHPUnit\Framework\Assert;
6+
use Spatie\Snapshots\Driver;
7+
use Symfony\Component\Serializer\Encoder\JsonEncode;
8+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
9+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
10+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
11+
use Symfony\Component\Serializer\Serializer;
12+
13+
class ObjectDriver implements Driver
14+
{
15+
public function serialize($data): string
16+
{
17+
$normalizers = [
18+
new DateTimeNormalizer(),
19+
new ObjectNormalizer(),
20+
];
21+
22+
$encoders = [
23+
new JsonEncoder(),
24+
];
25+
26+
$serializer = new Serializer($normalizers, $encoders);
27+
28+
return $serializer->serialize($data, 'json', [
29+
JsonEncode::OPTIONS => JSON_PRETTY_PRINT,
30+
]);
31+
}
32+
33+
public function extension(): string
34+
{
35+
return 'json';
36+
}
37+
38+
public function match($expected, $actual)
39+
{
40+
Assert::assertJsonStringEqualsJsonString($expected, $this->serialize($actual));
41+
}
42+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Spatie\Snapshots\Test\Unit\Drivers;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Spatie\Snapshots\Drivers\ObjectDriver;
7+
8+
class ObjectDriverTest extends TestCase
9+
{
10+
/** @test */
11+
public function it_can_serialize_an_object()
12+
{
13+
$driver = new ObjectDriver();
14+
15+
$expected = <<<JSON
16+
{
17+
"name": "My name",
18+
"valid": true,
19+
"dateTime": "2020-01-01T15:00:00+01:00",
20+
"public": "public"
21+
}
22+
JSON;
23+
24+
$this->assertEquals($expected, $driver->serialize(new Obj()));
25+
}
26+
}
27+
28+
class Obj
29+
{
30+
private $private = 'private';
31+
public $public = 'public';
32+
33+
public function getName()
34+
{
35+
return 'My name';
36+
}
37+
38+
public function isValid()
39+
{
40+
return true;
41+
}
42+
43+
public function getDateTime()
44+
{
45+
return new \DateTimeImmutable('2020-01-01 15:00:00+01:00');
46+
}
47+
}

0 commit comments

Comments
 (0)