Passed
Push — main ( 6276b7...62bcc5 )
by Gabriel
10:50
created

CastableTraitTest::test_castsDatetime()   A

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
        $date = date('Y-m-d');
31
        $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...
32
33
        $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...
34
        self::assertInstanceOf(\DateTime::class, $dateGet);
35
        self::assertSame($date, $dateGet->format('Y-m-d'));
36
        self::assertSame($date . ' 00:00:00', $book->getPropertyRaw('published'));
37
38
        $book = new Book();
39
        $date = Date::now();
40
        $book->published = $date;
41
42
        $dateGet = $book->published;
43
        $dateFormatted = $dateGet->format('Y-m-d H:i:s');
44
        self::assertInstanceOf(\DateTime::class, $dateGet);
45
        self::assertSame($dateFormatted, $dateGet->format('Y-m-d H:i:s'));
46
        self::assertSame($dateFormatted, $book->getPropertyRaw('published'));
47
    }
48
49
    public function test_castsValueCache()
50
    {
51
        $book = \Mockery::mock(Book::class)->shouldAllowMockingProtectedMethods()->makePartial();
52
        $book->shouldNotReceive('castValue')->twice()->andReturnUsing(
0 ignored issues
show
Bug introduced by
The method andReturnUsing() does not exist on Mockery\ExpectationInterface. Did you maybe mean andReturn()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        $book->shouldNotReceive('castValue')->twice()->/** @scrutinizer ignore-call */ andReturnUsing(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
            function ($key, $value) {
54
                return ValueCaster::as($value, 'datetime');
55
            }
56
        );
57
58
        $date = date('Y-m-d');
59
        $book->published = $date;
0 ignored issues
show
Bug introduced by
The property published does not seem to exist on Mockery\Mock.
Loading history...
60
61
        $book->get('published');
62
        $book->get('published');
63
        $dateGet = $book->published;
64
        self::assertInstanceOf(\DateTime::class, $dateGet);
65
        self::assertSame($date, $dateGet->format('Y-m-d'));
66
        self::assertSame($date.' 00:00:00', $book->getAttribute('published'));
67
68
        $book->published = '2019-01-01';
69
70
        $book->get('published');
71
        $book->get('published');
72
        $dateGet = $book->published;
73
        self::assertInstanceOf(\DateTime::class, $dateGet);
74
        self::assertSame('2019-01-01', $dateGet->format('Y-m-d'));
75
        self::assertSame('2019-01-01 00:00:00', $book->getAttribute('published'));
76
    }
77
}
78