Commit | Line | Data |
---|---|---|
423c9229 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
6 | | This work is published under the GNU AGPLv3 license with some | | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
9 | +--------------------------------------------------------------------+ | |
10 | */ | |
11 | ||
12 | namespace Civi\Test; | |
13 | ||
14 | use PHPUnit\Framework\Assert; | |
15 | ||
16 | /** | |
17 | * An EventCheck is a fragment of a unit-test -- it is mixed into | |
18 | * various test-scenarios and applies extra assertions. | |
19 | */ | |
20 | class EventCheck extends Assert { | |
21 | ||
22 | /** | |
23 | * @var \PHPUnit\Framework\Test | |
24 | */ | |
25 | private $test; | |
26 | ||
27 | /** | |
28 | * Determine whether this check should be used during the current test. | |
29 | * | |
4dbdebf0 | 30 | * @param \PHPUnit\Framework\Test|null $test |
423c9229 TO |
31 | * |
32 | * @return bool|string | |
33 | * FALSE: The check will be completely skipped. | |
34 | * TRUE: The check will be enabled. However, if the events never | |
35 | * execute, that is OK. Useful for general compliance-testing. | |
36 | */ | |
37 | public function isSupported($test) { | |
38 | return TRUE; | |
39 | } | |
40 | ||
41 | /** | |
42 | * @return \PHPUnit\Framework\Test|NULL | |
43 | */ | |
44 | public function getTest() { | |
45 | return $this->test; | |
46 | } | |
47 | ||
48 | /** | |
4dbdebf0 | 49 | * @param \PHPUnit\Framework\Test|null $test |
423c9229 TO |
50 | */ |
51 | public function setTest($test): void { | |
52 | $this->test = $test; | |
53 | } | |
54 | ||
55 | /** | |
56 | * Assert that a variable has a given type. | |
57 | * | |
58 | * @param string|string[] $types | |
59 | * List of types, per `gettype()` or `get_class()` | |
60 | * Ex: 'int|string|NULL' | |
61 | * Ex: [`array`, `NULL`, `CRM_Core_DAO`] | |
62 | * @param mixed $value | |
63 | * The variable to check | |
4dbdebf0 | 64 | * @param string|null $msg |
423c9229 TO |
65 | * @see \CRM_Utils_Type::validatePhpType |
66 | */ | |
67 | public function assertType($types, $value, ?string $msg = NULL) { | |
68 | if (!\CRM_Utils_Type::validatePhpType($value, $types, FALSE)) { | |
69 | $defactoType = is_object($value) ? get_class($value) : gettype($value); | |
70 | $types = is_array($types) ? implode('|', $types) : $types; | |
71 | $this->fail(sprintf("Expected one of (%s) but found %s\n%s", $types, $defactoType, $msg)); | |
72 | } | |
73 | } | |
74 | ||
75 | public function setUp() { | |
76 | } | |
77 | ||
78 | public function tearDown() { | |
79 | } | |
80 | ||
81 | } |