|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Janisbiz\LightOrm\Tests\Unit\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Janisbiz\LightOrm\Entity\BaseEntity; |
|
6
|
|
|
use Janisbiz\LightOrm\Entity\EntityException; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class BaseEntityTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
const COL_ONE_VALUE = '<b>valOne</b>'; |
|
12
|
|
|
const COL_ONE_VALUE_REPLACEMENT = 'valOneReplacement'; |
|
13
|
|
|
const COL_TWO_VALUE = 2; |
|
14
|
|
|
const COL_TWO_VALUE_REPLACEMENT = 2.2; |
|
15
|
|
|
const COL_THREE_VALUE = 3.3; |
|
16
|
|
|
const COL_THREE_VALUE_REPLACEMENT = 3.33; |
|
17
|
|
|
const COL_THREE_VALUE_REPLACEMENT_EMPTY = ''; |
|
18
|
|
|
const COL_FOUR_VALUE = 'This is column four!'; |
|
19
|
|
|
const COL_FIVE_VALUE = 'This is column five!'; |
|
20
|
|
|
const COL_NON_EXISTENT = 'non_existent'; |
|
21
|
|
|
const COL_NON_EXISTENT_VALUE = null; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var BaseEntity |
|
25
|
|
|
*/ |
|
26
|
|
|
private $baseEntity; |
|
27
|
|
|
|
|
28
|
|
|
public function setUp() |
|
29
|
|
|
{ |
|
30
|
|
|
$baseEntity = new BaseEntity(); |
|
31
|
|
|
|
|
32
|
|
|
$baseEntity->col_one = static::COL_ONE_VALUE; |
|
|
|
|
|
|
33
|
|
|
$baseEntity->col_two = static::COL_TWO_VALUE; |
|
|
|
|
|
|
34
|
|
|
$baseEntity->col_three = static::COL_THREE_VALUE; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
$this->baseEntity = $baseEntity; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testGetters() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->assertEquals(static::COL_ONE_VALUE, $this->baseEntity->getColOne()); |
|
|
|
|
|
|
42
|
|
|
$this->assertEquals( |
|
43
|
|
|
\nl2br(\htmlspecialchars(\trim(static::COL_ONE_VALUE), ENT_QUOTES, 'UTF-8')), |
|
44
|
|
|
$this->baseEntity->getColOne(true) |
|
45
|
|
|
); |
|
46
|
|
|
$this->assertEquals(static::COL_ONE_VALUE, $this->baseEntity->col_one); |
|
|
|
|
|
|
47
|
|
|
$this->assertEquals(static::COL_TWO_VALUE, $this->baseEntity->getColTwo()); |
|
|
|
|
|
|
48
|
|
|
$this->assertEquals(static::COL_TWO_VALUE, $this->baseEntity->getColTwo(true)); |
|
49
|
|
|
$this->assertEquals(static::COL_TWO_VALUE, $this->baseEntity->col_two); |
|
|
|
|
|
|
50
|
|
|
$this->assertEquals(static::COL_THREE_VALUE, $this->baseEntity->getColThree()); |
|
|
|
|
|
|
51
|
|
|
$this->assertEquals(static::COL_THREE_VALUE, $this->baseEntity->getColThree(true)); |
|
52
|
|
|
$this->assertEquals(static::COL_THREE_VALUE, $this->baseEntity->col_three); |
|
|
|
|
|
|
53
|
|
|
$this->assertEquals(static::COL_THREE_VALUE, $this->baseEntity->col_three); |
|
54
|
|
|
$this->assertEquals(static::COL_NON_EXISTENT_VALUE, $this->baseEntity->invalid_col); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testGettersInvalid() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->expectException(EntityException::class); |
|
60
|
|
|
$this->expectExceptionMessage('Call to undefined method Janisbiz\LightOrm\Entity\BaseEntity::getInvalidCol()'); |
|
61
|
|
|
|
|
62
|
|
|
$this->baseEntity->getInvalidCol(); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testSetters() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->baseEntity->setColThree(static::COL_THREE_VALUE_REPLACEMENT); |
|
|
|
|
|
|
68
|
|
|
$this->assertEquals(static::COL_THREE_VALUE_REPLACEMENT, $this->baseEntity->getColThree()); |
|
69
|
|
|
$this->assertEquals(static::COL_THREE_VALUE_REPLACEMENT, $this->baseEntity->col_three); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
$this->baseEntity->setColThree(static::COL_THREE_VALUE_REPLACEMENT_EMPTY); |
|
72
|
|
|
$this->assertEquals(null, $this->baseEntity->getColThree()); |
|
73
|
|
|
$this->assertEquals(null, $this->baseEntity->col_three); |
|
74
|
|
|
|
|
75
|
|
|
$this->baseEntity->setColFour(static::COL_FOUR_VALUE); |
|
|
|
|
|
|
76
|
|
|
$this->assertEquals(static::COL_FOUR_VALUE, $this->baseEntity->getColFour()); |
|
|
|
|
|
|
77
|
|
|
$this->assertEquals(static::COL_FOUR_VALUE, $this->baseEntity->col_four); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
$this->baseEntity->col_one = static::COL_ONE_VALUE_REPLACEMENT; |
|
|
|
|
|
|
80
|
|
|
$this->assertEquals(static::COL_ONE_VALUE_REPLACEMENT, $this->baseEntity->getColOne()); |
|
81
|
|
|
$this->assertEquals(static::COL_ONE_VALUE_REPLACEMENT, $this->baseEntity->col_one); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
$this->baseEntity->col_five = static::COL_FIVE_VALUE; |
|
|
|
|
|
|
84
|
|
|
$this->assertEquals(static::COL_FIVE_VALUE, $this->baseEntity->getColFive()); |
|
|
|
|
|
|
85
|
|
|
$this->assertEquals(static::COL_FIVE_VALUE, $this->baseEntity->col_five); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testData() |
|
89
|
|
|
{ |
|
90
|
|
|
$data = &$this->baseEntity->data(); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertCount(3, $data); |
|
93
|
|
|
$this->baseEntity->setColFour(static::COL_FOUR_VALUE); |
|
94
|
|
|
$this->assertCount(4, $data); |
|
95
|
|
|
$data['col_five'] = static::COL_FIVE_VALUE; |
|
96
|
|
|
$this->assertCount(5, $data); |
|
97
|
|
|
$this->assertEquals(static::COL_FIVE_VALUE, $this->baseEntity->getColFive()); |
|
98
|
|
|
$this->assertEquals(static::COL_FIVE_VALUE, $this->baseEntity->col_five); |
|
|
|
|
|
|
99
|
|
|
$this->assertEquals(static::COL_FIVE_VALUE, $this->baseEntity->data('col_five')); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function testDataWithInvalidKey() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->expectException(EntityException::class); |
|
105
|
|
|
$this->expectExceptionMessage('There is no key "ivalid_key" present in data!'); |
|
106
|
|
|
|
|
107
|
|
|
$this->baseEntity->data('ivalid_key'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function testDataOriginal() |
|
111
|
|
|
{ |
|
112
|
|
|
$data = &$this->baseEntity->data(); |
|
113
|
|
|
$dataOriginal = &$this->baseEntity->dataOriginal(); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertCount(3, $dataOriginal); |
|
116
|
|
|
|
|
117
|
|
|
$this->baseEntity->setColFour(static::COL_FOUR_VALUE); |
|
118
|
|
|
$this->assertCount(3, $dataOriginal); |
|
119
|
|
|
|
|
120
|
|
|
$data['col_five'] = static::COL_FIVE_VALUE; |
|
121
|
|
|
$this->assertCount(3, $dataOriginal); |
|
122
|
|
|
|
|
123
|
|
|
$this->assertEquals(static::COL_FIVE_VALUE, $this->baseEntity->getColFive()); |
|
124
|
|
|
$this->assertEquals(static::COL_FIVE_VALUE, $this->baseEntity->dataOriginal('col_five')); |
|
125
|
|
|
$this->assertEquals(static::COL_FIVE_VALUE, $this->baseEntity->col_five); |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
$this->baseEntity->col_five = static::COL_FIVE_VALUE; |
|
|
|
|
|
|
128
|
|
|
$this->assertCount(4, $dataOriginal); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function testDataOriginalWithInvalidKey() |
|
132
|
|
|
{ |
|
133
|
|
|
$this->expectException(EntityException::class); |
|
134
|
|
|
$this->expectExceptionMessage('There is no key "ivalid_key" present in data original!'); |
|
135
|
|
|
|
|
136
|
|
|
$this->baseEntity->dataOriginal('ivalid_key'); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function testIsNew() |
|
140
|
|
|
{ |
|
141
|
|
|
$this->assertTrue($this->baseEntity->isNew()); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function testIsSaved() |
|
145
|
|
|
{ |
|
146
|
|
|
$this->assertFalse($this->baseEntity->isSaved()); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function testPrimaryKeys() |
|
150
|
|
|
{ |
|
151
|
|
|
$this->assertCount(0, $this->baseEntity->primaryKeys()); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function testPrimaryKeysAutoIncrement() |
|
155
|
|
|
{ |
|
156
|
|
|
$this->assertCount(0, $this->baseEntity->primaryKeysAutoIncrement()); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function testColumns() |
|
160
|
|
|
{ |
|
161
|
|
|
$this->assertCount(0, $this->baseEntity->columns()); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|