Skip to content

Commit f74b64e

Browse files
authored
Enhance test self-explanation (#62)
1 parent 08c9e87 commit f74b64e

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

.github/workflows/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: CI
22

33
on:
4+
pull_request:
45
push:
56
branches: [ master ]
67
tags:

tests/unit/CheckAccessTrait.php

+24-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ public function setAssignments()
1919
}
2020
}
2121

22+
/**
23+
* Check the $userId has exactly $allowedPermissions and no more.
24+
*
25+
* @param string $userId
26+
* @param string[] $allowedPermissions
27+
* @return void
28+
*/
2229
public function assertAccesses($userId, array $allowedPermissions)
2330
{
2431
$deniedPermissions = array_diff($this->getAllPermissions(), $allowedPermissions);
@@ -27,15 +34,29 @@ public function assertAccesses($userId, array $allowedPermissions)
2734
$this->assertAccess($userId, false, $deniedPermissions);
2835
}
2936

30-
public function assertAccess($userId, $isAllowed, array $permissions)
37+
/**
38+
* @param string $userId
39+
* @param bool $isAllowed whether the passed permissions should be allowed or denied
40+
* @param string[] $permissions
41+
*/
42+
public function assertAccess($userId, $isAllowed, array $permissions): void
3143
{
44+
$wrongPermissions = [];
3245
foreach ($permissions as $permission) {
3346
$checked = $this->auth->checkAccess($userId, $permission);
3447
if ($checked !== $isAllowed) {
35-
var_dump(compact('userId', 'isAllowed', 'permission'));
48+
$wrongPermissions[] = $permission;
3649
}
37-
$this->assertSame($isAllowed, $checked);
3850
}
51+
52+
$message = sprintf(
53+
"The following permissions for user '%s' should be %s, but they are %s instead: \n\n %s",
54+
$userId,
55+
$isAllowed ? 'allowed' : 'denied',
56+
$isAllowed ? 'denied' : 'allowed',
57+
var_export($wrongPermissions, true)
58+
);
59+
$this->assertEmpty($wrongPermissions, $message);
3960
}
4061

4162
protected $allPermissions;

0 commit comments

Comments
 (0)