Passed
Push — master ( d25e4d...dba989 )
by Mr
06:54
created

EntityTest::testDiff()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.9666
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/entity project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Tests\Entity;
10
11
use Assert\AssertionFailedException;
12
use Daikon\Entity\EntityDiff;
13
use Daikon\Tests\Entity\Fixture\Article;
14
use Daikon\Tests\Entity\Fixture\Location;
15
use Daikon\Tests\Entity\Fixture\Paragraph;
16
use PHPUnit\Framework\TestCase;
17
18
class EntityTest extends TestCase
19
{
20
    private const FIXED_UUID = '941b4e51-e524-4e5d-8c17-1ef96585abc3';
21
22
    private const FIXTURE = [
23
        '@type' => Article::class,
24
        'id' => '525b4e51-e524-4e5d-8c17-1ef96585cbd3',
25
        'created' => '2017-04-02T23:42:05.000000+00:00',
26
        'title' => 'hello world!',
27
        'url' => 'http://www.example.com/',
28
        'feedbackMail' => '[email protected]',
29
        'averageVoting' => 23.42,
30
        'workshopDate' => '2017-05-23',
31
        'workshopCancelled' => true,
32
        'workshopLocation' => [
33
            '@type' => Location::class,
34
            'id' => 42,
35
            'coords' => [ 'lat' => 52.5119, 'lon' => 13.3084 ]
36
        ],
37
        'paragraphs' => [[
38
            '@type' => Paragraph::class,
39
            'id' => 23,
40
            'kicker' => 'this is the kicker baby!',
41
            'content' => 'hell yeah!'
42
        ]]
43
    ];
44
45
    /** @var Article */
46
    private $entity;
47
48 1
    public function testGet(): void
49
    {
50 1
        $this->assertEquals(self::FIXTURE['id'], $this->entity->getIdentity()->toNative());
51 1
        $this->assertEquals(self::FIXTURE['title'], $this->entity->getTitle()->toNative());
52 1
    }
53
54 1
    public function testHas(): void
55
    {
56 1
        $this->assertTrue($this->entity->has('id'));
57 1
        $this->assertTrue($this->entity->has('title'));
58 1
        $this->assertTrue($this->entity->has('paragraphs'));
59 1
        $article = $this->entity::fromNative(['id' => '941b4e51-e524-4e5d-8c17-1ef96585abc3']);
60 1
        $this->assertFalse($article->has('title'));
61 1
    }
62
63 1
    public function testWith(): void
64
    {
65 1
        $article = $this->entity->withValue('id', self::FIXED_UUID);
66 1
        $this->assertEquals(self::FIXTURE['id'], $this->entity->get('id')->toNative());
67 1
        $this->assertEquals(self::FIXED_UUID, $article->get('id')->toNative());
68 1
    }
69
70 1
    public function testDiff(): void
71
    {
72 1
        $article = Article::fromNative([
73 1
            'id' => self::FIXED_UUID,
74 1
            'title' => 'Hello world!',
75 1
            'url' => 'http://metallica.com',
76
        ]);
77
        $diffData = [
78 1
            'title' => 'This is different',
79
            'url' => 'http://tv.lol',
80
        ];
81 1
        $calculatedDiff = (new EntityDiff)($article->withValues($diffData), $article);
82 1
        $this->assertEquals($diffData, $calculatedDiff->toNative());
83 1
    }
84
85 1
    public function testIsSameAs(): void
86
    {
87 1
        $articleTwo = Article::fromNative(['id' => self::FIXTURE['id'], 'title' => 'Hello world!']);
88
        // considered same, due to identifier
89 1
        $this->assertTrue($this->entity->isSameAs($articleTwo));
90 1
    }
91
92 1
    public function testGetPath(): void
93
    {
94 1
        $this->assertEquals(
95 1
            self::FIXTURE['paragraphs'][0]['kicker'],
96 1
            $this->entity->get('paragraphs.0-kicker')->toNative()
97
        );
98 1
    }
99
100 1
    public function testToNative(): void
101
    {
102 1
        $this->assertEquals(self::FIXTURE, $this->entity->toNative());
103 1
    }
104
105 1
    public function testInvalidValue(): void
106
    {
107 1
        $this->expectException(AssertionFailedException::class);
108 1
        Article::fromNative(['id' => self::FIXED_UUID, 'title' => [123]]);
109
    } // @codeCoverageIgnore
110
111 1
    public function testInvalidHas(): void
112
    {
113 1
        $this->expectException(\InvalidArgumentException::class);
114 1
        $article = Article::fromNative(['id' => self::FIXED_UUID]);
115 1
        $article->has('foobar');
116
    } // @codeCoverageIgnore
117
118 1
    public function testInvalidPath(): void
119
    {
120 1
        $this->expectException(\InvalidArgumentException::class);
121 1
        $article = Article::fromNative(['id' => self::FIXED_UUID]);
122 1
        $article->get('foo.0');
123
    } // @codeCoverageIgnore
124
125 10
    protected function setUp(): void
126
    {
127 10
        $this->entity = Article::fromNative(self::FIXTURE);
128 10
    }
129
}
130