CastableTraitTest::test_castsDatetime()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 6
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace ByTIC\DataObjects\Tests\Behaviors\Castable;
4
5
use ByTIC\DataObjects\Tests\AbstractTest;
6
use ByTIC\DataObjects\Tests\Fixtures\Models\Books\Book;
7
use ByTIC\DataObjects\ValueCaster;
8
use Nip\Utility\Date;
9
10
/**
11
 * Class CastableTraitTest
12
 * @package ByTIC\DataObjects\Tests\Behaviors\Castable
13
 */
14
class CastableTraitTest extends AbstractTest
15
{
16
    public function test_castsDatetime()
17
    {
18
        $book = new Book();
19
        $date = date('Y-m-d');
20
        $book->published = $date;
0 ignored issues
show
Bug Best Practice introduced by
The property published does not exist on ByTIC\DataObjects\Tests\Fixtures\Models\Books\Book. Since you implemented __set, consider adding a @property annotation.
Loading history...
21
22
        $dateGet = $book->published;
0 ignored issues
show
Bug Best Practice introduced by
The property published does not exist on ByTIC\DataObjects\Tests\Fixtures\Models\Books\Book. Since you implemented __get, consider adding a @property annotation.
Loading history...
23
        self::assertInstanceOf(\DateTime::class, $dateGet);
24
        self::assertSame($date, $dateGet->format('Y-m-d'));
25
    }
26
27
    public function test_transformInboundValue_Datetime()
28
    {
29
        $book = new Book();
30
31
        $book->published = null;
0 ignored issues
show
Bug Best Practice introduced by
The property published does not exist on ByTIC\DataObjects\Tests\Fixtures\Models\Books\Book. Since you implemented __set, consider adding a @property annotation.
Loading history...
32
        self::assertNull($book->published);
0 ignored issues
show
Bug Best Practice introduced by
The property published does not exist on ByTIC\DataObjects\Tests\Fixtures\Models\Books\Book. Since you implemented __get, consider adding a @property annotation.
Loading history...
33
34
        $book->published = '0000-00-00 00:00:00';
35
        self::assertNull($book->published);
36
37
        $date = date('Y-m-d');
38
        $book->published = $date;
39
40
        $dateGet = $book->published;
41
        self::assertInstanceOf(\DateTime::class, $dateGet);
42
        self::assertSame($date, $dateGet->format('Y-m-d'));
43
        self::assertSame($date . ' 00:00:00', $book->getPropertyRaw('published'));
44
45
        $book = new Book();
46
        $date = Date::now();
47
        $book->published = $date;
48
49
        $dateGet = $book->published;
50
        $dateFormatted = $dateGet->format('Y-m-d H:i:s');
51
        self::assertInstanceOf(\DateTime::class, $dateGet);
52
        self::assertSame($dateFormatted, $dateGet->format('Y-m-d H:i:s'));
53
        self::assertSame($dateFormatted, $book->getPropertyRaw('published'));
54
    }
55
56
    public function test_castsValueCache()
57
    {
58
        $book = \Mockery::mock(Book::class)->shouldAllowMockingProtectedMethods()->makePartial();
0 ignored issues
show
Bug introduced by
The type Mockery was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
59
        $book->shouldNotReceive('castValue')->twice()->andReturnUsing(
60
            function ($key, $value) {
61
                return ValueCaster::as($value, 'datetime');
62
            }
63
        );
64
65
        $date = date('Y-m-d');
66
        $book->published = $date;
67
68
        $book->get('published');
69
        $book->get('published');
70
        $dateGet = $book->published;
71
        self::assertInstanceOf(\DateTime::class, $dateGet);
72
        self::assertSame($date, $dateGet->format('Y-m-d'));
73
        self::assertSame($date.' 00:00:00', $book->getAttribute('published'));
74
75
        $book->published = '2019-01-01';
76
77
        $book->get('published');
78
        $book->get('published');
79
        $dateGet = $book->published;
80
        self::assertInstanceOf(\DateTime::class, $dateGet);
81
        self::assertSame('2019-01-01', $dateGet->format('Y-m-d'));
82
        self::assertSame('2019-01-01 00:00:00', $book->getAttribute('published'));
83
    }
84
}
85