Passed
Push — main ( c84275...1d7014 )
by Gabriel
03:41
created

TimestampableTraitTest::test_updatedTimestamps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 16
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 22
rs 9.7333
1
<?php
2
3
namespace ByTIC\DataObjects\Tests\Behaviors\Timestampable;
4
5
use ByTIC\DataObjects\Tests\AbstractTest;
6
use ByTIC\DataObjects\Tests\Fixtures\Dto\Timestampable;
7
use ByTIC\DataObjects\Tests\Fixtures\Models\Books\Book;
8
use Nip\Utility\Date;
9
10
/**
11
 * Class TimestampableTraitTest
12
 * @package ByTIC\DataObjects\Tests\Behaviors\Timestampable
13
 */
14
class TimestampableTraitTest extends AbstractTest
15
{
16
    public function test_getTimestampsAttributes()
17
    {
18
        $object = new Timestampable();
19
20
        self::assertSame(['created'], $object->getCreateTimestamps());
21
        self::assertSame(['modified'], $object->getUpdateTimestamps());
22
        self::assertSame([], $object->getTimestampAttributes(''));
23
        self::assertSame([], $object->getTimestampAttributes('test'));
24
        self::assertSame(['created', 'modified'], $object->getTimestampAttributes('create'));
25
        self::assertSame(['modified'], $object->getTimestampAttributes('update'));
26
    }
27
28
    public function test_setModelTimeAttribute()
29
    {
30
        $object = new Timestampable();
31
32
        $now = Date::now();
33
        $object->setModelTimeAttribute('created', null);
34
        $created = $object->created;
0 ignored issues
show
Bug Best Practice introduced by
The property created does not exist on ByTIC\DataObjects\Tests\Fixtures\Dto\Timestampable. Since you implemented __get, consider adding a @property annotation.
Loading history...
35
        self::assertInstanceOf(\DateTime::class, $created);
36
        self::assertSame($now->toDateTimeString('minute'), $created->toDateTimeString('minute'));
0 ignored issues
show
Bug introduced by
The method toDateTimeString() does not exist on null. ( Ignorable by Annotation )

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

36
        self::assertSame($now->toDateTimeString('minute'), $created->/** @scrutinizer ignore-call */ toDateTimeString('minute'));

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...
37
38
        $object->setModelTimeAttribute('created', '');
39
        $created = $object->created;
40
        self::assertInstanceOf(\DateTime::class, $created);
41
        self::assertSame($now->toDateTimeString('minute'), $created->toDateTimeString('minute'));
42
43
        $object->setModelTimeAttribute('created', ' ');
44
        $created = $object->created;
45
        self::assertInstanceOf(\DateTime::class, $created);
46
        self::assertSame($now->toDateTimeString('minute'), $created->toDateTimeString('minute'));
47
48
        $now = $now->add('hour', 3);
49
        $object->setModelTimeAttribute('created', $now->timestamp);
50
        $created = $object->created;
51
        self::assertInstanceOf(\DateTime::class, $created);
52
        self::assertSame($now->toDateTimeString('minute'), $created->toDateTimeString('minute'));
53
54
        $now = $now->add('hour', 3);
55
        $object->setModelTimeAttribute('created', $now->toDateTimeString());
56
        $created = $object->created;
57
        self::assertInstanceOf(\DateTime::class, $created);
58
        self::assertSame($now->toDateTimeString('minute'), $created->toDateTimeString('minute'));
59
60
        self::expectException(\Exception::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

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

60
        self::/** @scrutinizer ignore-call */ 
61
              expectException(\Exception::class);
Loading history...
61
        $object->setModelTimeAttribute('created', 'test');
62
    }
63
64
    public function test_hookCastableTrait()
65
    {
66
        $book = new Book();
67
        $book->bootTimestampableTrait();
68
69
        $date1 = Date::now();
70
        $book->created_at = $date1->toDateTimeString();
0 ignored issues
show
Bug Best Practice introduced by
The property created_at does not exist on ByTIC\DataObjects\Tests\Fixtures\Models\Books\Book. Since you implemented __set, consider adding a @property annotation.
Loading history...
71
72
        $dateGet = $book->created_at;
0 ignored issues
show
Bug Best Practice introduced by
The property created_at does not exist on ByTIC\DataObjects\Tests\Fixtures\Models\Books\Book. Since you implemented __get, consider adding a @property annotation.
Loading history...
73
        self::assertInstanceOf(\DateTime::class, $dateGet);
74
        self::assertSame($date1->toDateTimeString(), $dateGet->toDateTimeString());
75
    }
76
77
    public function test_updatedTimestamps()
78
    {
79
        $book = new Book();
80
        $date1 = Date::now();
81
        $book->updatedTimestamps('create');
82
83
        $dateGet = $book->created_at;
0 ignored issues
show
Bug Best Practice introduced by
The property created_at does not exist on ByTIC\DataObjects\Tests\Fixtures\Models\Books\Book. Since you implemented __get, consider adding a @property annotation.
Loading history...
84
        self::assertInstanceOf(\DateTime::class, $dateGet);
85
        self::assertSame($date1->toDateTimeString(), $dateGet->toDateTimeString());
0 ignored issues
show
Bug introduced by
The method toDateTimeString() does not exist on null. ( Ignorable by Annotation )

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

85
        self::assertSame($date1->toDateTimeString(), $dateGet->/** @scrutinizer ignore-call */ toDateTimeString());

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...
86
87
        $dateOld = $date1->sub('days', 5);
88
        $date1 = Date::now();
89
        $book->created_at = $dateOld;
0 ignored issues
show
Bug Best Practice introduced by
The property created_at does not exist on ByTIC\DataObjects\Tests\Fixtures\Models\Books\Book. Since you implemented __set, consider adding a @property annotation.
Loading history...
90
        $book->updatedTimestamps('update');
91
92
        $dateGet = $book->created_at;
93
        self::assertInstanceOf(\DateTime::class, $dateGet);
94
        self::assertSame($dateOld->toDateTimeString(), $dateGet->toDateTimeString());
95
96
        $dateGet = $book->updated_at;
0 ignored issues
show
Bug Best Practice introduced by
The property updated_at does not exist on ByTIC\DataObjects\Tests\Fixtures\Models\Books\Book. Since you implemented __get, consider adding a @property annotation.
Loading history...
97
        self::assertInstanceOf(\DateTime::class, $dateGet);
98
        self::assertSame($date1->toDateTimeString(), $dateGet->toDateTimeString());
99
    }
100
}
101