1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Test Helper class provides some helpful functions for tests. |
5
|
|
|
* @author Christian Blank <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
class TestHelper { |
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @param callable $callback |
11
|
|
|
* @param string $expectedException |
12
|
|
|
* @param int $expectedCode |
|
|
|
|
13
|
|
|
* @param string $expectedMessage |
|
|
|
|
14
|
|
|
* @author VladaHejda |
15
|
|
|
*/ |
16
|
|
|
public static function assertException(callable $callback, $expectedException = 'Exception', $expectedCode = null, $expectedMessage = null) { |
17
|
|
|
$self = new SapphireTest; |
18
|
|
|
if (!ClassInfo::exists($expectedException)) { |
19
|
|
|
$self->fail(sprintf('An exception of type "%s" does not exist.', $expectedException)); |
|
|
|
|
20
|
|
|
} |
21
|
|
|
try { |
22
|
|
|
$callback(); |
23
|
|
|
} catch (\Exception $e) { |
24
|
|
|
$class = ClassInfo::class_name($e); |
25
|
|
|
$message = $e->getMessage(); |
26
|
|
|
$code = $e->getCode(); |
27
|
|
|
$errorMessage = 'Failed asserting the class of exception'; |
28
|
|
|
if ($message && $code) { |
29
|
|
|
$errorMessage .= sprintf(' (message was %s, code was %d)', $message, $code); |
30
|
|
|
} else if ($code) { |
31
|
|
|
$errorMessage .= sprintf(' (code was %d)', $code); |
32
|
|
|
} |
33
|
|
|
$errorMessage .= '.'; |
34
|
|
|
$self->assertInstanceOf($expectedException, $e, $errorMessage); |
|
|
|
|
35
|
|
|
if ($expectedCode !== null) { |
36
|
|
|
$self->assertEquals($expectedCode, $code, sprintf('Failed asserting code of thrown %s.', $class)); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
if ($expectedMessage !== null) { |
39
|
|
|
$self->assertContains($expectedMessage, $message, sprintf('Failed asserting the message of thrown %s.', $class)); |
40
|
|
|
} |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
$errorMessage = 'Failed asserting that exception'; |
44
|
|
|
if (strtolower($expectedException) !== 'exception') { |
45
|
|
|
$errorMessage .= sprintf(' of type %s', $expectedException); |
46
|
|
|
} |
47
|
|
|
$errorMessage .= ' was thrown.'; |
48
|
|
|
$self->fail($errorMessage); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Call protected/private method of a class. |
53
|
|
|
* |
54
|
|
|
* @param object|string &$object Instantiated object that we will run method on. |
55
|
|
|
* @param string $methodName Method name to call |
56
|
|
|
* @param array $parameters Array of parameters to pass into method. |
57
|
|
|
* |
58
|
|
|
* @return mixed Method return. |
59
|
|
|
* |
60
|
|
|
* @author Juan Treminio |
61
|
|
|
* @author Christian Blank |
62
|
|
|
*/ |
63
|
|
|
public static function invoke(&$object, $methodName, array $parameters = []) { |
64
|
|
|
if(is_object($object)) { |
65
|
|
|
$className = get_class($object); |
66
|
|
|
} else { |
67
|
|
|
$className = $object; |
68
|
|
|
} |
69
|
|
|
$reflection = new \ReflectionClass($className); |
70
|
|
|
$method = $reflection->getMethod($methodName); |
71
|
|
|
$method->setAccessible(true); |
72
|
|
|
return $method->invokeArgs($object, $parameters); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.