44
55namespace Atk4 \Data \Tests ;
66
7- use Atk4 \Core \Exception as CoreException ;
7+ use Atk4 \Data \Exception ;
88use Atk4 \Data \Model ;
99use Atk4 \Data \Persistence ;
1010use Atk4 \Data \Schema \TestCase ;
@@ -85,7 +85,7 @@ public function testBasic(): void
8585 $ client ->unload ();
8686
8787 // test system action
88- $ act2 = $ client ->getUserAction ('backupClients ' );
88+ $ act2 = $ client ->getModel ()-> getUserAction ('backupClients ' );
8989
9090 // action takes no arguments. If it would, we should be able to find info about those
9191 self ::assertSame ([], $ act2 ->args );
@@ -108,6 +108,17 @@ public function testCustomSeedClass(): void
108108 self ::assertSame ($ customClass , get_class ($ client ->getUserAction ('foo ' )));
109109 }
110110
111+ public function testExecuteUndefinedMethodException (): void
112+ {
113+ $ client = new UaClient ($ this ->pers );
114+ $ client ->addUserAction ('new_client ' );
115+ $ client = $ client ->load (1 );
116+
117+ $ this ->expectException (\Error::class);
118+ $ this ->expectExceptionMessage ('Call to undefined method ' );
119+ $ client ->executeUserAction ('new_client ' );
120+ }
121+
111122 public function testPreview (): void
112123 {
113124 $ client = new UaClient ($ this ->pers );
@@ -116,12 +127,13 @@ public function testPreview(): void
116127 });
117128
118129 $ client = $ client ->load (1 );
130+
119131 self ::assertSame ('John ' , $ client ->getUserAction ('say_name ' )->execute ());
120132
121- $ client ->getUserAction ('say_name ' )->preview = function (UaClient $ m, string $ arg ) {
133+ $ client ->getUserAction ('say_name ' )->preview = function (UaClient $ m ) {
122134 return 'will say ' . $ m ->get ('name ' );
123135 };
124- self ::assertSame ('will say John ' , $ client ->getUserAction ('say_name ' )->preview (' x ' ));
136+ self ::assertSame ('will say John ' , $ client ->getUserAction ('say_name ' )->preview ());
125137
126138 $ client ->getModel ()->addUserAction ('also_backup ' , ['callback ' => 'backupClients ' ]);
127139 self ::assertSame ('backs up all clients ' , $ client ->getUserAction ('also_backup ' )->execute ());
@@ -132,55 +144,68 @@ public function testPreview(): void
132144 self ::assertSame ('Also Backup UaClient ' , $ client ->getUserAction ('also_backup ' )->getDescription ());
133145 }
134146
135- public function testAppliesTo1 (): void
147+ public function testAppliesToSingleRecordNotEntityException (): void
136148 {
137149 $ client = new UaClient ($ this ->pers );
138- $ client = $ client ->createEntity ();
139150
140- $ this ->expectExceptionMessage ('load existing record ' );
151+ $ this ->expectException (\TypeError::class);
152+ $ this ->expectExceptionMessage ('Expected entity, but instance is a model ' );
141153 $ client ->executeUserAction ('sendReminder ' );
142154 }
143155
144- public function testAppliesTo2 (): void
156+ public function testAppliesToAllRecordsEntityException (): void
145157 {
146158 $ client = new UaClient ($ this ->pers );
147- $ client ->addUserAction ('new_client ' , ['appliesTo ' => Model \UserAction::APPLIES_TO_NO_RECORDS ]);
148159 $ client = $ client ->load (1 );
149160
150- $ this ->expectExceptionMessage ('can be executed on non-existing record ' );
151- $ client ->executeUserAction ('new_client ' );
161+ $ this ->expectException (\TypeError::class);
162+ $ this ->expectExceptionMessage ('Expected model, but instance is an entity ' );
163+ $ client ->executeUserAction ('backupClients ' );
152164 }
153165
154- public function testAppliesTo3 (): void
166+ public function testAppliesToSingleRecordNotLoadedException (): void
155167 {
156168 $ client = new UaClient ($ this ->pers );
157- $ client ->addUserAction ('new_client ' , ['appliesTo ' => Model \UserAction::APPLIES_TO_NO_RECORDS , 'atomic ' => false ]);
158169 $ client = $ client ->createEntity ();
159170
160- $ this ->expectExceptionMessage ('undefined method ' );
171+ $ this ->expectException (Exception::class);
172+ $ this ->expectExceptionMessage ('User action can be executed on loaded entity only ' );
173+ $ client ->executeUserAction ('sendReminder ' );
174+ }
175+
176+ public function testAppliesToNoRecordsLoadedRecordException (): void
177+ {
178+ $ client = new UaClient ($ this ->pers );
179+ $ client ->addUserAction ('new_client ' , ['appliesTo ' => Model \UserAction::APPLIES_TO_NO_RECORDS ]);
180+ $ client = $ client ->load (1 );
181+
182+ $ this ->expectException (Exception::class);
183+ $ this ->expectExceptionMessage ('User action can be executed on new entity only ' );
161184 $ client ->executeUserAction ('new_client ' );
162185 }
163186
164- public function testException1 (): void
187+ public function testNotDefinedException (): void
165188 {
166189 $ client = new UaClient ($ this ->pers );
167190
168- $ this ->expectException (CoreException::class);
191+ $ this ->expectException (Exception::class);
192+ $ this ->expectExceptionMessage ('User action is not defined ' );
169193 $ client ->getUserAction ('non_existent_action ' );
170194 }
171195
172- public function testDisabled1 (): void
196+ public function testDisabledBoolException (): void
173197 {
174198 $ client = new UaClient ($ this ->pers );
175199 $ client = $ client ->load (1 );
176200
177201 $ client ->getUserAction ('sendReminder ' )->enabled = false ;
178202
179- $ this ->expectExceptionMessage ('disabled ' );
203+ $ this ->expectException (Exception::class);
204+ $ this ->expectExceptionMessage ('User action is disabled ' );
180205 $ client ->getUserAction ('sendReminder ' )->execute ();
181206 }
182207
183- public function testDisabled2 (): void
208+ public function testDisabledClosureException (): void
184209 {
185210 $ client = new UaClient ($ this ->pers );
186211 $ client = $ client ->load (1 );
@@ -194,7 +219,8 @@ public function testDisabled2(): void
194219 return false ;
195220 };
196221
197- $ this ->expectExceptionMessage ('disabled ' );
222+ $ this ->expectException (Exception::class);
223+ $ this ->expectExceptionMessage ('User action is disabled ' );
198224 $ client ->getUserAction ('sendReminder ' )->execute ();
199225 }
200226
@@ -211,7 +237,7 @@ public function testFields(): void
211237 self ::assertSame ('Peter ' , $ client ->get ('name ' ));
212238 }
213239
214- public function testFieldsTooDirty1 (): void
240+ public function testFieldsTooDirtyException (): void
215241 {
216242 $ client = new UaClient ($ this ->pers );
217243 $ client ->addUserAction ('change_details ' , ['callback ' => 'save ' , 'fields ' => ['name ' ]]);
@@ -222,21 +248,8 @@ public function testFieldsTooDirty1(): void
222248 $ client ->set ('name ' , 'Peter ' );
223249 $ client ->set ('reminder_sent ' , true );
224250
225- $ this ->expectExceptionMessage ('dirty fields ' );
226- $ client ->getUserAction ('change_details ' )->execute ();
227- }
228-
229- public function testFieldsIncorrect (): void
230- {
231- $ client = new UaClient ($ this ->pers );
232- $ client ->addUserAction ('change_details ' , ['callback ' => 'save ' , 'fields ' => 'whops_forgot_brackets ' ]);
233-
234- $ client = $ client ->load (1 );
235-
236- self ::assertNotSame ('Peter ' , $ client ->get ('name ' ));
237- $ client ->set ('name ' , 'Peter ' );
238-
239- $ this ->expectExceptionMessage ('must be either array or boolean ' );
251+ $ this ->expectException (Exception::class);
252+ $ this ->expectExceptionMessage ('User action cannot be executed as unrelated fields are dirty ' );
240253 $ client ->getUserAction ('change_details ' )->execute ();
241254 }
242255
0 commit comments