Passed
Push — main ( d959bf...c2020d )
by Gabriel
02:07
created

TimestampableTraitTest::test_createTimestamps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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

39
        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...
40
41
        $object->setModelTimeAttribute('created', '');
42
        $created = $object->created;
43
        self::assertInstanceOf(\DateTime::class, $created);
44
        self::assertSame($now->toDateTimeString('minute'), $created->toDateTimeString('minute'));
45
46
        $object->setModelTimeAttribute('created', ' ');
47
        $created = $object->created;
48
        self::assertInstanceOf(\DateTime::class, $created);
49
        self::assertSame($now->toDateTimeString('minute'), $created->toDateTimeString('minute'));
50
51
        $now = $now->add('hour', 3);
52
        $object->setModelTimeAttribute('created', $now->timestamp);
53
        $created = $object->created;
54
        self::assertInstanceOf(\DateTime::class, $created);
55
        self::assertSame($now->toDateTimeString('minute'), $created->toDateTimeString('minute'));
56
57
        $now = $now->add('hour', 3);
58
        $object->setModelTimeAttribute('created', $now->toDateTimeString());
59
        $created = $object->created;
60
        self::assertInstanceOf(\DateTime::class, $created);
61
        self::assertSame($now->toDateTimeString('minute'), $created->toDateTimeString('minute'));
62
63
        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

63
        self::/** @scrutinizer ignore-call */ 
64
              expectException(\Exception::class);
Loading history...
64
        $object->setModelTimeAttribute('created', 'test');
65
    }
66
67
    public function test_hookCastableTrait()
68
    {
69
        $book = new Book();
70
        $book->bootTimestampableTrait();
71
72
        $date1 = Date::now();
73
        $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...
74
75
        $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...
76
        self::assertInstanceOf(\DateTime::class, $dateGet);
77
        self::assertSame($date1->toDateTimeString(), $dateGet->toDateTimeString());
78
    }
79
80
    public function test_usesTimestamps_called_once()
81
    {
82
        /** @var Mock|NoProperties $book */
83
        $book = \Mockery::mock(NoProperties::class)->shouldAllowMockingProtectedMethods()->makePartial();
84
        $book->shouldReceive('usesTimestampsDefault')->once()->andReturn(true);
85
86
        $book->usesTimestamps();
87
        $book->usesTimestamps();
88
        $book->usesTimestamps();
89
        self::assertTrue($book->usesTimestamps());
90
    }
91
92
    public function test_updatedTimestamps()
93
    {
94
        $book = new Book();
95
        $date1 = Date::now();
96
        $book->updatedTimestamps('create');
97
98
        $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...
99
        self::assertInstanceOf(\DateTime::class, $dateGet);
100
        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

100
        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...
101
102
        $dateOld = $date1->sub('days', 5);
103
        $date1 = Date::now();
104
        $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...
105
        $book->updatedTimestamps('update');
106
107
        $dateGet = $book->created_at;
108
        self::assertInstanceOf(\DateTime::class, $dateGet);
109
        self::assertSame($dateOld->toDateTimeString(), $dateGet->toDateTimeString());
110
111
        $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...
112
        self::assertInstanceOf(\DateTime::class, $dateGet);
113
        self::assertSame($date1->toDateTimeString(), $dateGet->toDateTimeString());
114
    }
115
116
    public function test_createTimestamps()
117
    {
118
        $object = new CreateTimestamps();
119
120
        self::assertSame(['created'], $object->getCreateTimestamps());
121
        self::assertSame([], $object->getUpdateTimestamps());
122
        self::assertSame([], $object->getTimestampAttributes(''));
123
        self::assertSame([], $object->getTimestampAttributes('test'));
124
        self::assertSame(['created'], $object->getTimestampAttributes('create'));
125
        self::assertSame([], $object->getTimestampAttributes('update'));
126
127
    }
128
}
129