1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests; |
4
|
|
|
|
5
|
|
|
class ModelTest extends \PHPUnit_Framework_TestCase |
6
|
|
|
{ |
7
|
|
|
public function __construct() |
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
|
|
|
|
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: