@@ -28,19 +28,22 @@ public function setUp(): void
2828 'created_by ' => 'new ' ,
2929 'modified_by ' => 'always ' ,
3030 '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 ' ,
3138 ],
32- 'Model.beforeFind ' => 'created_by ' ,
3339 ],
3440 'propertiesMap ' => [
3541 'company_id ' => '_footprint.company.id ' ,
42+ 'manager_id ' => '_footprint.manager.id ' ,
3643 ],
3744 ]);
3845
3946 $ this ->Table = $ table ;
40- $ this ->footprint = new Entity ([
41- 'id ' => 2 ,
42- 'company ' => new Entity (['id ' => 5 ]),
43- ]);
4447 }
4548
4649 public function tearDown (): void
@@ -51,58 +54,151 @@ public function tearDown(): void
5154
5255 public function testSave ()
5356 {
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 ]);
5667 $ expected = [
5768 'id ' => $ entity ->id ,
5869 'title ' => 'new article ' ,
5970 'created_by ' => 2 ,
6071 'modified_by ' => 2 ,
61- 'company_id ' => 5 ,
72+ 'company_id ' => 1 ,
73+ 'manager_id ' => 7 ,
6274 ];
75+
6376 $ this ->assertSame (
6477 $ 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 ' ])
6679 );
6780
81+ // Closure fields won't set if disallowed
82+ // even if provided.
83+ $ entity = new Entity ();
84+ $ entity ->title = 'new title ' ;
6885 $ footprint = new Entity ([
6986 'id ' => 3 ,
87+ 'company ' => new Entity (['id ' => 5 ]),
88+ 'manager ' => new Entity (['id ' => 4 ]),
7089 ]);
71- $ entity -> title = ' new title ' ;
90+
7291 $ 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+ ];
75100
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
76104 $ entity = new Entity (['title ' => 'without footprint ' ]);
105+
77106 $ 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 ' ]));
80137 }
81138
82139 public function testFind ()
83140 {
84- $ result = $ this ->Table ->find ('all ' , _footprint: $ this ->footprint )
141+ $ footprint = new Entity (['id ' => 4 ]);
142+
143+ $ result = $ this ->Table ->find ('all ' , _footprint: $ footprint )
85144 ->enableHydration (false )
86145 ->first ();
87146
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+ ];
89155 $ this ->assertSame ($ expected , $ result );
90156
91157 // Test to show value of "id" is not used from footprint if
92158 // "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 ])
95161 ->enableHydration (false )
96162 ->first ();
97163
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+ ];
99189 $ this ->assertSame ($ expected , $ result );
100190 }
101191
102192 public function testInjectEntityException ()
103193 {
104194 $ 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+ ]);
106202
107203 $ this ->Table ->behaviors ()->Footprint ->setConfig (
108204 'events ' ,
@@ -113,6 +209,6 @@ public function testInjectEntityException()
113209 ]
114210 );
115211 $ entity = new Entity (['title ' => 'new article ' ]);
116- $ entity = $ this ->Table ->save ($ entity , ['_footprint ' => $ this -> footprint ]);
212+ $ entity = $ this ->Table ->save ($ entity , ['_footprint ' => $ footprint ]);
117213 }
118214}
0 commit comments