@@ -28,19 +28,22 @@ public function setUp(): void
28
28
'created_by ' => 'new ' ,
29
29
'modified_by ' => 'always ' ,
30
30
'company_id ' => 'always ' ,
31
+ 'manager_id ' => function ($ entity ): bool {
32
+ return $ entity ->company_id == 1 ;
33
+ },
34
+ ],
35
+ 'Model.beforeFind ' => [
36
+ 'created_by ' ,
37
+ 'company_id ' ,
31
38
],
32
- 'Model.beforeFind ' => 'created_by ' ,
33
39
],
34
40
'propertiesMap ' => [
35
41
'company_id ' => '_footprint.company.id ' ,
42
+ 'manager_id ' => '_footprint.manager.id ' ,
36
43
],
37
44
]);
38
45
39
46
$ this ->Table = $ table ;
40
- $ this ->footprint = new Entity ([
41
- 'id ' => 2 ,
42
- 'company ' => new Entity (['id ' => 5 ]),
43
- ]);
44
47
}
45
48
46
49
public function tearDown (): void
@@ -51,58 +54,151 @@ public function tearDown(): void
51
54
52
55
public function testSave ()
53
56
{
54
- $ entity = new Entity (['title ' => 'new article ' ]);
55
- $ entity = $ this ->Table ->save ($ entity , ['_footprint ' => $ this ->footprint ]);
57
+ // Properties may still be assigned even if
58
+ // closure would be satisfied.
59
+ $ entity = new Entity (['title ' => 'new article ' , 'manager_id ' => 7 ]);
60
+ $ footprint = new Entity ([
61
+ 'id ' => 2 ,
62
+ 'company ' => new Entity (['id ' => 1 ]),
63
+ 'manager ' => new Entity (['id ' => 10 ]),
64
+ ]);
65
+
66
+ $ entity = $ this ->Table ->save ($ entity , ['_footprint ' => $ footprint ]);
56
67
$ expected = [
57
68
'id ' => $ entity ->id ,
58
69
'title ' => 'new article ' ,
59
70
'created_by ' => 2 ,
60
71
'modified_by ' => 2 ,
61
- 'company_id ' => 5 ,
72
+ 'company_id ' => 1 ,
73
+ 'manager_id ' => 7 ,
62
74
];
75
+
63
76
$ this ->assertSame (
64
77
$ expected ,
65
- $ entity ->extract (['id ' , 'title ' , 'created_by ' , 'modified_by ' , 'company_id ' ])
78
+ $ entity ->extract (['id ' , 'title ' , 'created_by ' , 'modified_by ' , 'company_id ' , ' manager_id ' ])
66
79
);
67
80
81
+ // Closure fields won't set if disallowed
82
+ // even if provided.
83
+ $ entity = new Entity ();
84
+ $ entity ->title = 'new title ' ;
68
85
$ footprint = new Entity ([
69
86
'id ' => 3 ,
87
+ 'company ' => new Entity (['id ' => 5 ]),
88
+ 'manager ' => new Entity (['id ' => 4 ]),
70
89
]);
71
- $ entity -> title = ' new title ' ;
90
+
72
91
$ entity = $ this ->Table ->save ($ entity , ['_footprint ' => $ footprint ]);
73
- $ expected = ['id ' => $ entity ->id , 'title ' => 'new title ' , 'created_by ' => 2 , 'modified_by ' => 3 ];
74
- $ this ->assertSame ($ expected , $ entity ->extract (['id ' , 'title ' , 'created_by ' , 'modified_by ' ]));
92
+ $ expected = [
93
+ 'id ' => $ entity ->id ,
94
+ 'title ' => 'new title ' ,
95
+ 'created_by ' => 3 ,
96
+ 'modified_by ' => 3 ,
97
+ 'company_id ' => 5 ,
98
+ 'manager_id ' => null ,
99
+ ];
75
100
101
+ $ this ->assertSame ($ expected , $ entity ->extract (['id ' , 'title ' , 'created_by ' , 'modified_by ' , 'company_id ' , 'manager_id ' ]));
102
+
103
+ // Fields won't set if a footprint isn't provided
76
104
$ entity = new Entity (['title ' => 'without footprint ' ]);
105
+
77
106
$ entity = $ this ->Table ->save ($ entity );
78
- $ expected = ['id ' => $ entity ->id , 'title ' => 'without footprint ' , 'created_by ' => null , 'modified_by ' => null ];
79
- $ this ->assertSame ($ expected , $ entity ->extract (['id ' , 'title ' , 'created_by ' , 'modified_by ' ]));
107
+ $ expected = [
108
+ 'id ' => $ entity ->id ,
109
+ 'title ' => 'without footprint ' ,
110
+ 'created_by ' => null ,
111
+ 'modified_by ' => null ,
112
+ 'manager_id ' => null ,
113
+ ];
114
+
115
+ $ this ->assertSame ($ expected , $ entity ->extract (['id ' , 'title ' , 'created_by ' , 'modified_by ' , 'manager_id ' ]));
116
+
117
+ // Satisfying closure manually still permits
118
+ // explicit field assignments
119
+ $ entity = new Entity (['title ' => 'different manager ' , 'company_id ' => 1 ]);
120
+ $ footprint = new Entity ([
121
+ 'id ' => 3 ,
122
+ 'company ' => new Entity (['id ' => 5 ]),
123
+ 'manager ' => new Entity (['id ' => 4 ]),
124
+ ]);
125
+
126
+ $ entity = $ this ->Table ->save ($ entity , ['_footprint ' => $ footprint ]);
127
+ $ expected = [
128
+ 'id ' => $ entity ->id ,
129
+ 'title ' => 'different manager ' ,
130
+ 'created_by ' => 3 ,
131
+ 'modified_by ' => 3 ,
132
+ 'company_id ' => 1 ,
133
+ 'manager_id ' => 4 ,
134
+ ];
135
+
136
+ $ this ->assertSame ($ expected , $ entity ->extract (['id ' , 'title ' , 'created_by ' , 'modified_by ' , 'company_id ' , 'manager_id ' ]));
80
137
}
81
138
82
139
public function testFind ()
83
140
{
84
- $ result = $ this ->Table ->find ('all ' , _footprint: $ this ->footprint )
141
+ $ footprint = new Entity (['id ' => 4 ]);
142
+
143
+ $ result = $ this ->Table ->find ('all ' , _footprint: $ footprint )
85
144
->enableHydration (false )
86
145
->first ();
87
146
88
- $ expected = ['id ' => 3 , 'title ' => 'article 3 ' , 'created_by ' => 2 , 'modified_by ' => 1 ];
147
+ $ expected = [
148
+ 'id ' => 4 ,
149
+ 'title ' => 'find article ' ,
150
+ 'created_by ' => 4 ,
151
+ 'modified_by ' => 4 ,
152
+ 'company_id ' => 2 ,
153
+ 'manager_id ' => null ,
154
+ ];
89
155
$ this ->assertSame ($ expected , $ result );
90
156
91
157
// Test to show value of "id" is not used from footprint if
92
158
// "Articles.created_by" is already set in condition.
93
- $ result = $ this ->Table ->find ('all ' , _footprint: $ this -> footprint )
94
- ->where (['Articles.created_by ' => 1 ])
159
+ $ result = $ this ->Table ->find ('all ' , _footprint: $ footprint )
160
+ ->where (['Articles.created_by ' => 3 ])
95
161
->enableHydration (false )
96
162
->first ();
97
163
98
- $ expected = ['id ' => 1 , 'title ' => 'article 1 ' , 'created_by ' => 1 , 'modified_by ' => 1 ];
164
+ $ expected = [
165
+ 'id ' => 5 ,
166
+ 'title ' => 'final article ' ,
167
+ 'created_by ' => 3 ,
168
+ 'modified_by ' => 4 ,
169
+ 'company_id ' => 4 ,
170
+ 'manager_id ' => null ,
171
+ ];
172
+ $ this ->assertSame ($ expected , $ result );
173
+
174
+ // Test to show value of "id" is not used from footprint even
175
+ // "Articles.manager_id" validates the Model.beforeSave closure
176
+ $ result = $ this ->Table ->find ('all ' , _footprint: $ footprint )
177
+ ->where (['Articles.company_id ' => 1 ])
178
+ ->enableHydration (false )
179
+ ->first ();
180
+
181
+ $ expected = [
182
+ 'id ' => 6 ,
183
+ 'title ' => 'penultimate article ' ,
184
+ 'created_by ' => 4 ,
185
+ 'modified_by ' => 4 ,
186
+ 'company_id ' => 1 ,
187
+ 'manager_id ' => null ,
188
+ ];
99
189
$ this ->assertSame ($ expected , $ result );
100
190
}
101
191
102
192
public function testInjectEntityException ()
103
193
{
104
194
$ this ->expectException ('UnexpectedValueException ' );
105
- $ this ->expectExceptionMessage ('When should be one of "always", "new" or "existing". The passed value "invalid" is invalid ' );
195
+ $ this ->expectExceptionMessage ('When should be one of "always", "new" or "existing", ' .
196
+ 'or a closure that takes an EntityInterface and returns a bool. ' .
197
+ 'The passed value "invalid" is invalid. ' );
198
+
199
+ $ footprint = new Entity ([
200
+ 'id ' => 2 ,
201
+ ]);
106
202
107
203
$ this ->Table ->behaviors ()->Footprint ->setConfig (
108
204
'events ' ,
@@ -113,6 +209,6 @@ public function testInjectEntityException()
113
209
]
114
210
);
115
211
$ entity = new Entity (['title ' => 'new article ' ]);
116
- $ entity = $ this ->Table ->save ($ entity , ['_footprint ' => $ this -> footprint ]);
212
+ $ entity = $ this ->Table ->save ($ entity , ['_footprint ' => $ footprint ]);
117
213
}
118
214
}
0 commit comments