Completed
Push — master ( 993045...a7faa2 )
by Christian
05:28 queued 02:54
created

TestHelper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 5
Bugs 0 Features 3
Metric Value
wmc 11
c 5
b 0
f 3
lcom 0
cbo 2
dl 0
loc 68
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
D assertException() 0 34 9
A invoke() 0 11 2
1
<?php
2
3
/**
4
 * Test Helper class provides some helpful functions for tests.
5
 * @author Christian Blank <[email protected]>
6
 */
7
class TestHelper {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
9
    /**
10
     * @param callable $callback
11
     * @param string $expectedException
12
     * @param int $expectedCode
0 ignored issues
show
Documentation introduced by
Should the type for parameter $expectedCode not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
13
     * @param string $expectedMessage
0 ignored issues
show
Documentation introduced by
Should the type for parameter $expectedMessage not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
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));
0 ignored issues
show
Bug introduced by
The method fail() does not seem to exist on object<SapphireTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<SapphireTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
            if ($expectedCode !== null) {
36
                $self->assertEquals($expectedCode, $code, sprintf('Failed asserting code of thrown %s.', $class));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SapphireTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method fail() does not seem to exist on object<SapphireTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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