|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests; |
|
4
|
|
|
|
|
5
|
|
|
class ModelTest extends \PHPUnit\Framework\TestCase |
|
6
|
|
|
{ |
|
7
|
|
|
public function setUp() |
|
8
|
|
|
{ |
|
9
|
|
|
$this->model = new Model(); |
|
|
|
|
|
|
10
|
|
|
$this->date = date('Y-m-d H:i:s'); |
|
|
|
|
|
|
11
|
|
|
$this->datetime = new \DateTime($this->date); |
|
|
|
|
|
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
public function testNewEntity() |
|
15
|
|
|
{ |
|
16
|
|
|
$this->assertEquals(null, $this->model->getId()); |
|
17
|
|
|
$this->assertEquals(null, $this->model->getCreatedAt()); |
|
18
|
|
|
$this->assertEquals(null, $this->model->getUpdatedAt()); |
|
19
|
|
|
$this->assertEquals(null, $this->model->getDeletedAt()); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function testPopulatedEntity() |
|
23
|
|
|
{ |
|
24
|
|
|
// Vérification des Getter sur des objects |
|
25
|
|
|
$this->model->populate($this->datetime); |
|
26
|
|
|
// Avec format |
|
27
|
|
|
$this->assertEquals($this->date, $this->model->getCreatedAt('Y-m-d H:i:s')); |
|
28
|
|
|
$this->assertEquals($this->date, $this->model->getUpdatedAt('Y-m-d H:i:s')); |
|
29
|
|
|
$this->assertEquals($this->date, $this->model->getDeletedAt('Y-m-d H:i:s')); |
|
30
|
|
|
$this->assertEquals(substr($this->date, 0, 10), $this->model->getCreatedAt('Y-m-d')); |
|
31
|
|
|
$this->assertEquals(substr($this->date, 0, 10), $this->model->getUpdatedAt('Y-m-d')); |
|
32
|
|
|
$this->assertEquals(substr($this->date, 0, 10), $this->model->getDeletedAt('Y-m-d')); |
|
33
|
|
|
|
|
34
|
|
|
// Sans format |
|
35
|
|
|
$this->assertEquals($this->datetime, $this->model->getUpdatedAt()); |
|
36
|
|
|
$this->assertEquals($this->datetime, $this->model->getCreatedAt()); |
|
37
|
|
|
$this->assertEquals($this->datetime, $this->model->getDeletedAt()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @expectedException Exception |
|
42
|
|
|
* @expectedExceptionMessage ETNA\Doctrine\Extensions\AutoIncrementID::setId : method not implemented |
|
43
|
|
|
*/ |
|
44
|
|
|
public function testExceptionOnSetId() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->model->setId(2); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @expectedException Exception |
|
51
|
|
|
* @expectedExceptionMessage created_at is not a datetime |
|
52
|
|
|
*/ |
|
53
|
|
|
public function testExceptionOnGetCreated_at() |
|
54
|
|
|
{ |
|
55
|
|
|
// Remplis les dates du model avec le model au lieu d'un Datetime |
|
56
|
|
|
$this->model->populate($this->model); |
|
57
|
|
|
$this->model->getCreatedAt(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @expectedException Exception |
|
62
|
|
|
* @expectedExceptionMessage updated_at is not a datetime |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testExceptionOnGetUpdated_at() |
|
65
|
|
|
{ |
|
66
|
|
|
// Remplis les dates du model avec le model au lieu d'un Datetime |
|
67
|
|
|
$this->model->populate($this->model); |
|
68
|
|
|
$this->model->getUpdatedAt(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @expectedException Exception |
|
73
|
|
|
* @expectedExceptionMessage deleted_at is not a datetime |
|
74
|
|
|
*/ |
|
75
|
|
|
public function testExceptionOnGetDeleted_at() |
|
76
|
|
|
{ |
|
77
|
|
|
// Remplis les dates du model avec le model au lieu d'un Datetime |
|
78
|
|
|
$this->model->populate($this->model); |
|
79
|
|
|
$this->model->getDeletedAt(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @expectedException Exception |
|
84
|
|
|
* @expectedExceptionMessage bad deleted_at provided |
|
85
|
|
|
*/ |
|
86
|
|
|
public function testExceptionOnSetDeleted_at() |
|
87
|
|
|
{ |
|
88
|
|
|
$this->model->setDeletedAt(date('Y-m-d')); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @expectedException Exception |
|
93
|
|
|
* @expectedExceptionMessage Cannot delete Tests\Model |
|
94
|
|
|
*/ |
|
95
|
|
|
public function testExceptionOncanNotDeleted() |
|
96
|
|
|
{ |
|
97
|
|
|
$this->model->canNotDelete(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testSetCreatedAt() |
|
101
|
|
|
{ |
|
102
|
|
|
$this->model->setCreatedAtBeforePersist(); |
|
103
|
|
|
$this->assertEquals(date('Y-m-d H:i:s'), $this->model->getCreatedAt('Y-m-d H:i:s')); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testSetUpdatedAt() |
|
107
|
|
|
{ |
|
108
|
|
|
$this->model->setUpdatedAt(); |
|
109
|
|
|
$this->assertEquals(date('Y-m-d H:i:s'), $this->model->getUpdatedAt('Y-m-d H:i:s')); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function testSetDeletedAt() |
|
113
|
|
|
{ |
|
114
|
|
|
$this->model->setDeletedAt($this->date); |
|
115
|
|
|
// On récupère une string |
|
116
|
|
|
$this->assertEquals($this->date, $this->model->getDeletedAt('Y-m-d H:i:s')); |
|
117
|
|
|
// On récupère un objet |
|
118
|
|
|
$this->assertEquals($this->datetime, $this->model->getDeletedAt()); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function testSetProperties() |
|
122
|
|
|
{ |
|
123
|
|
|
$this->assertEquals($this->model->getModelValue(), ''); |
|
124
|
|
|
$this->model->setProperties(["model_value" => "super value"]); |
|
125
|
|
|
$this->assertEquals($this->model->getModelValue(), "super value"); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function testModelService() |
|
129
|
|
|
{ |
|
130
|
|
|
$doctrine_fake_metas = new \stdClass(); |
|
131
|
|
|
$validation_fake_metas = new \stdClass(); |
|
132
|
|
|
$fake_constraints = new \stdClass(); |
|
133
|
|
|
$fake_constraints->constraintsByGroup = [ |
|
134
|
|
|
"create" => [] |
|
135
|
|
|
]; |
|
136
|
|
|
|
|
137
|
|
|
$validation_fake_metas->members = [ |
|
138
|
|
|
"model_value" => [$fake_constraints], |
|
139
|
|
|
"created_at" => [$fake_constraints] |
|
140
|
|
|
]; |
|
141
|
|
|
|
|
142
|
|
|
$doctrine_fake_metas->fieldMappings = [ |
|
143
|
|
|
"model_value" => ["fieldName" => "model_value", "type" => "string"], |
|
144
|
|
|
"created_at" => ["fieldName" => "created_at", "type" => "datetime"], |
|
145
|
|
|
"updated_at" => ["fieldName" => "updated_at", "type" => "datetime"], |
|
146
|
|
|
"deleted_at" => ["fieldName" => "deleted_at", "type" => "datetime"], |
|
147
|
|
|
]; |
|
148
|
|
|
|
|
149
|
|
|
$em = $this->createMock(\Doctrine\ORM\EntityManager::class); |
|
150
|
|
|
$em->method('getClassMetadata')->willReturn($doctrine_fake_metas); |
|
151
|
|
|
$validator = $this->createMock(\Symfony\Component\Validator\Validator\RecursiveValidator::class); |
|
152
|
|
|
$validator->method('getMetadataFor')->willReturn($validation_fake_metas); |
|
153
|
|
|
$model_service = new ModelService($em, $validator); |
|
154
|
|
|
|
|
155
|
|
|
$model = $model_service->create([ |
|
156
|
|
|
'model_value' => 'je suis une super valeur', |
|
157
|
|
|
'useless_value' => 'mais moi je sers a rien', |
|
158
|
|
|
'created_at' => '2012-12-21 12:12:12', |
|
159
|
|
|
'updated_at' => 'j\'suis trop un hacker' |
|
160
|
|
|
]); |
|
161
|
|
|
|
|
162
|
|
|
$this->assertEquals($model->getModelValue(), 'je suis une super valeur'); |
|
163
|
|
|
$this->assertEquals($model->getCreatedAt('Y-m-d H:i:s'), '2012-12-21 12:12:12'); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: