Passed
Push — main ( 2123f6...cb81bd )
by Gabriel
02:05
created

TimestampableTraitTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 63
c 2
b 0
f 2
dl 0
loc 96
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A test_getTimestampsAttributes() 0 10 1
A test_setModelTimeAttribute() 0 34 1
A test_usesTimestamps_called_once() 0 9 1
A test_updatedTimestamps() 0 22 1
A test_hookCastableTrait() 0 11 1
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\Dto\TimestampableNoProperties;
8
use ByTIC\DataObjects\Tests\Fixtures\Models\Books\Book;
9
use Nip\Utility\Date;
10
11
/**
12
 * Class TimestampableTraitTest
13
 * @package ByTIC\DataObjects\Tests\Behaviors\Timestampable
14
 */
15
class TimestampableTraitTest extends AbstractTest
16
{
17
    public function test_getTimestampsAttributes()
18
    {
19
        $object = new Timestampable();
20
21
        self::assertSame(['created'], $object->getCreateTimestamps());
22
        self::assertSame(['modified'], $object->getUpdateTimestamps());
23
        self::assertSame([], $object->getTimestampAttributes(''));
24
        self::assertSame([], $object->getTimestampAttributes('test'));
25
        self::assertSame(['created', 'modified'], $object->getTimestampAttributes('create'));
26
        self::assertSame(['modified'], $object->getTimestampAttributes('update'));
27
    }
28
29
    public function test_setModelTimeAttribute()
30
    {
31
        $object = new Timestampable();
32
33
        $now = Date::now();
34
        $object->setModelTimeAttribute('created', null);
35
        $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...
36
        self::assertInstanceOf(\DateTime::class, $created);
37
        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

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

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

97
        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...
98
99
        $dateOld = $date1->sub('days', 5);
100
        $date1 = Date::now();
101
        $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...
102
        $book->updatedTimestamps('update');
103
104
        $dateGet = $book->created_at;
105
        self::assertInstanceOf(\DateTime::class, $dateGet);
106
        self::assertSame($dateOld->toDateTimeString(), $dateGet->toDateTimeString());
107
108
        $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...
109
        self::assertInstanceOf(\DateTime::class, $dateGet);
110
        self::assertSame($date1->toDateTimeString(), $dateGet->toDateTimeString());
111
    }
112
}
113