Skip to content

Commit 1525265

Browse files
authored
Use assertCount where applicable (#863)
* Use assertCount where applicable * fix ValidationTest class name
1 parent 184421a commit 1525265

7 files changed

+38
-50
lines changed

composer.json

-7
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@
5656
"phpunit/phpcov": "*",
5757
"phpunit/phpunit": ">=9.0"
5858
},
59-
"require-dev-release": {
60-
"friendsofphp/php-cs-fixer": "^2.17",
61-
"johnkary/phpunit-speedtrap": "^3.2",
62-
"phpstan/phpstan": "^0.12.58",
63-
"phpunit/phpcov": "*",
64-
"phpunit/phpunit": ">=9.0"
65-
},
6659
"config": {
6760
"sort-packages": true
6861
},

tests/ConditionSqlTest.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -289,22 +289,22 @@ public function testArrayCondition()
289289
$m = new Model($this->db, ['table' => 'user']);
290290
$m->addField('name');
291291
$m->addCondition('name', ['John', 'Doe']);
292-
$this->assertSame(1, count($m->export()));
292+
$this->assertCount(1, $m->export());
293293

294294
$m = new Model($this->db, ['table' => 'user']);
295295
$m->addField('name');
296296
$m->addCondition('name', 'in', ['Johhny', 'Doe', 'Mary']);
297-
$this->assertSame(2, count($m->export()));
297+
$this->assertCount(2, $m->export());
298298

299299
$m = new Model($this->db, ['table' => 'user']);
300300
$m->addField('name');
301301
$m->addCondition('name', []); // this should not fail, always should be false
302-
$this->assertSame(0, count($m->export()));
302+
$this->assertCount(0, $m->export());
303303

304304
$m = new Model($this->db, ['table' => 'user']);
305305
$m->addField('name');
306306
$m->addCondition('name', 'not in', []); // this should not fail, always should be true
307-
$this->assertSame(3, count($m->export()));
307+
$this->assertCount(3, $m->export());
308308
}
309309

310310
public function testDateCondition()
@@ -435,18 +435,18 @@ public function testLikeCondition()
435435
$u->addField('created', ['type' => 'datetime']);
436436

437437
$t = (clone $u)->addCondition('created', 'like', '%19%');
438-
$this->assertSame(2, count($t->export())); // only year 2019 records
438+
$this->assertCount(2, $t->export()); // only year 2019 records
439439

440440
$t = (clone $u)->addCondition('active', 'like', '%1%');
441-
$this->assertSame(2, count($t->export())); // only active records
441+
$this->assertCount(2, $t->export()); // only active records
442442

443443
$t = (clone $u)->addCondition('active', 'like', '%0%');
444-
$this->assertSame(1, count($t->export())); // only inactive records
444+
$this->assertCount(1, $t->export()); // only inactive records
445445

446446
$t = (clone $u)->addCondition('active', 'like', '%999%');
447-
$this->assertSame(0, count($t->export())); // bad value, so it will not match anything
447+
$this->assertCount(0, $t->export()); // bad value, so it will not match anything
448448

449449
$t = (clone $u)->addCondition('active', 'like', '%ABC%');
450-
$this->assertSame(0, count($t->export())); // bad value, so it will not match anything
450+
$this->assertCount(0, $t->export()); // bad value, so it will not match anything
451451
}
452452
}

tests/ConditionTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public function testBasicDiscrimination()
2929

3030
$m->addCondition('gender', 'M');
3131

32-
$this->assertSame(1, count($m->scope()->getNestedConditions()));
32+
$this->assertCount(1, $m->scope()->getNestedConditions());
3333

3434
$m->addCondition('gender', 'F');
3535

36-
$this->assertSame(2, count($m->scope()->getNestedConditions()));
36+
$this->assertCount(2, $m->scope()->getNestedConditions());
3737
}
3838

3939
public function testEditableAfterCondition()

tests/PersistentArrayTest.php

+22-22
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public function testLike()
311311
// case : str%
312312
$m->addCondition('country', 'LIKE', 'La%');
313313
$result = $m->action('select')->getRows();
314-
$this->assertSame(3, count($result));
314+
$this->assertCount(3, $result);
315315
$this->assertSame($dbDataCountries[3], $result[3]);
316316
$this->assertSame($dbDataCountries[7], $result[7]);
317317
$this->assertSame($dbDataCountries[9], $result[9]);
@@ -322,7 +322,7 @@ public function testLike()
322322
$m->scope()->clear();
323323
$m->addCondition('country', 'NOT LIKE', 'La%');
324324
$result = $m->action('select')->getRows();
325-
$this->assertSame(7, count($m->export()));
325+
$this->assertCount(7, $m->export());
326326
$this->assertSame($dbDataCountries[1], $result[1]);
327327
$this->assertSame($dbDataCountries[2], $result[2]);
328328
$this->assertSame($dbDataCountries[4], $result[4]);
@@ -335,7 +335,7 @@ public function testLike()
335335
$m->scope()->clear();
336336
$m->addCondition('country', 'LIKE', '%ia');
337337
$result = $m->action('select')->getRows();
338-
$this->assertSame(4, count($result));
338+
$this->assertCount(4, $result);
339339
$this->assertSame($dbDataCountries[3], $result[3]);
340340
$this->assertSame($dbDataCountries[7], $result[7]);
341341
$this->assertSame($dbDataCountries[8], $result[8]);
@@ -347,7 +347,7 @@ public function testLike()
347347
$m->scope()->clear();
348348
$m->addCondition('country', 'LIKE', '%a%');
349349
$result = $m->action('select')->getRows();
350-
$this->assertSame(8, count($result));
350+
$this->assertCount(8, $result);
351351
$this->assertSame($dbDataCountries[1], $result[1]);
352352
$this->assertSame($dbDataCountries[2], $result[2]);
353353
$this->assertSame($dbDataCountries[3], $result[3]);
@@ -361,36 +361,36 @@ public function testLike()
361361
// case : boolean field
362362
$m->scope()->clear();
363363
$m->addCondition('active', 'LIKE', '0');
364-
$this->assertSame(4, count($m->export()));
364+
$this->assertCount(4, $m->export());
365365

366366
$m->scope()->clear();
367367
$m->addCondition('active', 'LIKE', '1');
368-
$this->assertSame(6, count($m->export()));
368+
$this->assertCount(6, $m->export());
369369

370370
$m->scope()->clear();
371371
$m->addCondition('active', 'LIKE', '%0%');
372-
$this->assertSame(4, count($m->export()));
372+
$this->assertCount(4, $m->export());
373373

374374
$m->scope()->clear();
375375
$m->addCondition('active', 'LIKE', '%1%');
376-
$this->assertSame(6, count($m->export()));
376+
$this->assertCount(6, $m->export());
377377

378378
$m->scope()->clear();
379379
$m->addCondition('active', 'LIKE', '%999%');
380-
$this->assertSame(0, count($m->export()));
380+
$this->assertCount(0, $m->export());
381381

382382
$m->scope()->clear();
383383
$m->addCondition('active', 'LIKE', '%ABC%');
384-
$this->assertSame(0, count($m->export()));
384+
$this->assertCount(0, $m->export());
385385

386386
// null value
387387
$m->scope()->clear();
388388
$m->addCondition('code', '=', null);
389-
$this->assertSame(1, count($m->export()));
389+
$this->assertCount(1, $m->export());
390390

391391
$m->scope()->clear();
392392
$m->addCondition('code', '!=', null);
393-
$this->assertSame(9, count($m->export()));
393+
$this->assertCount(9, $m->export());
394394
}
395395

396396
/**
@@ -424,7 +424,7 @@ public function testConditions()
424424
$m->scope()->clear();
425425
$m->addCondition('country', 'REGEXP', 'Ireland|UK');
426426
$result = $m->action('select')->getRows();
427-
$this->assertSame(5, count($result));
427+
$this->assertCount(5, $result);
428428
$this->assertSame($dbDataCountries[1], $result[1]);
429429
$this->assertSame($dbDataCountries[2], $result[2]);
430430
$this->assertSame($dbDataCountries[4], $result[4]);
@@ -436,23 +436,23 @@ public function testConditions()
436436
$m->scope()->clear();
437437
$m->addCondition('country', 'NOT REGEXP', 'Ireland|UK|Latvia');
438438
$result = $m->action('select')->getRows();
439-
$this->assertSame(1, count($result));
439+
$this->assertCount(1, $result);
440440
$this->assertSame($dbDataCountries[8], $result[8]);
441441
unset($result);
442442
$m->unload();
443443

444444
$m->scope()->clear();
445445
$m->addCondition('code', '>', 18);
446446
$result = $m->action('select')->getRows();
447-
$this->assertSame(1, count($result));
447+
$this->assertCount(1, $result);
448448
$this->assertSame($dbDataCountries[9], $result[9]);
449449
unset($result);
450450
$m->unload();
451451

452452
$m->scope()->clear();
453453
$m->addCondition('code', '>=', 18);
454454
$result = $m->action('select')->getRows();
455-
$this->assertSame(2, count($result));
455+
$this->assertCount(2, $result);
456456
$this->assertSame($dbDataCountries[8], $result[8]);
457457
$this->assertSame($dbDataCountries[9], $result[9]);
458458
unset($result);
@@ -461,15 +461,15 @@ public function testConditions()
461461
$m->scope()->clear();
462462
$m->addCondition('code', '<', 12);
463463
$result = $m->action('select')->getRows();
464-
$this->assertSame(1, count($result));
464+
$this->assertCount(1, $result);
465465
$this->assertSame($dbDataCountries[1], $result[1]);
466466
unset($result);
467467
$m->unload();
468468

469469
$m->scope()->clear();
470470
$m->addCondition('code', '<=', 12);
471471
$result = $m->action('select')->getRows();
472-
$this->assertSame(2, count($result));
472+
$this->assertCount(2, $result);
473473
$this->assertSame($dbDataCountries[1], $result[1]);
474474
$this->assertSame($dbDataCountries[2], $result[2]);
475475
unset($result);
@@ -478,7 +478,7 @@ public function testConditions()
478478
$m->scope()->clear();
479479
$m->addCondition('code', [11, 12]);
480480
$result = $m->action('select')->getRows();
481-
$this->assertSame(2, count($result));
481+
$this->assertCount(2, $result);
482482
$this->assertSame($dbDataCountries[1], $result[1]);
483483
$this->assertSame($dbDataCountries[2], $result[2]);
484484
unset($result);
@@ -487,14 +487,14 @@ public function testConditions()
487487
$m->scope()->clear();
488488
$m->addCondition('code', 'IN', []);
489489
$result = $m->action('select')->getRows();
490-
$this->assertSame(0, count($result));
490+
$this->assertCount(0, $result);
491491
unset($result);
492492
$m->unload();
493493

494494
$m->scope()->clear();
495495
$m->addCondition('code', 'NOT IN', [11, 12, 13, 14, 15, 16, 17]);
496496
$result = $m->action('select')->getRows();
497-
$this->assertSame(2, count($result));
497+
$this->assertCount(2, $result);
498498
$this->assertSame($dbDataCountries[8], $result[8]);
499499
$this->assertSame($dbDataCountries[9], $result[9]);
500500
unset($result);
@@ -503,7 +503,7 @@ public function testConditions()
503503
$m->scope()->clear();
504504
$m->addCondition('code', '!=', [11, 12, 13, 14, 15, 16, 17]);
505505
$result = $m->action('select')->getRows();
506-
$this->assertSame(2, count($result));
506+
$this->assertCount(2, $result);
507507
$this->assertSame($dbDataCountries[8], $result[8]);
508508
$this->assertSame($dbDataCountries[9], $result[9]);
509509
unset($result);

tests/UserActionTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public function testBasic()
7171
$client = new UaClient($this->pers);
7272

7373
$actions = $client->getUserActions();
74-
$this->assertSame(4, count($actions)); // don't return system actions here, but include add/edit/delete
75-
$this->assertSame(0, count($client->getUserActions(Model\UserAction::APPLIES_TO_ALL_RECORDS))); // don't return system actions here
74+
$this->assertCount(4, $actions); // don't return system actions here, but include add/edit/delete
75+
$this->assertCount(0, $client->getUserActions(Model\UserAction::APPLIES_TO_ALL_RECORDS)); // don't return system actions here
7676

7777
$act1 = $actions['send_reminder'];
7878

tests/ValidationTest.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
use Atk4\Data\Persistence;
1010
use Atk4\Data\ValidationException;
1111

12-
class ValidationTest
13-
{
14-
// TODO ignore CS fixer error
15-
}
16-
1712
class MyValidationModel extends Model
1813
{
1914
protected function init(): void
@@ -53,7 +48,7 @@ public function validate($intent = null): array
5348
}
5449
}
5550

56-
class ValidationTests extends AtkPhpunit\TestCase
51+
class ValidationTest extends AtkPhpunit\TestCase
5752
{
5853
public $m;
5954

@@ -139,7 +134,7 @@ public function testValidateHook()
139134
$this->m->save();
140135
$this->fail('Expected exception');
141136
} catch (\Atk4\Data\ValidationException $e) {
142-
$this->assertSame(2, count($e->errors));
137+
$this->assertCount(2, $e->errors);
143138
}
144139
}
145140
}

tests/WithTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testWith()
5050
'with "i" ("user_id","invoiced") as (select "user_id","net" from "invoice" where "net" > :a) select "user"."id","user"."name","user"."salary","_i"."invoiced" from "user" inner join "i" "_i" on "_i"."user_id" = "user"."id"',
5151
$m->action('select')->render()
5252
);
53-
$this->assertSame(2, count($m->export()));
53+
$this->assertCount(2, $m->export());
5454
}
5555

5656
/**

0 commit comments

Comments
 (0)