Completed
Push — master ( 06b292...adafac )
by Nicolai
02:22
created

AssertThrows::assertThrows()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace SmartWeb\ModuleTesting\Assertions;
5
6
use Exception;
7
use PHPUnit\Framework\AssertionFailedError;
8
use PHPUnit\Framework\TestCase;
9
10
11
/**
12
 * Trait AssertThrows
13
 *
14
 * @link    https://github.com/Codeception/AssertThrows
15
 *
16
 * @package SmartWeb\Testing\Assertions
17
 */
18
trait AssertThrows
19
{
20
    
21
    /**
22
     * Asserts that callback throws an exception
23
     *
24
     * @param          $throws
25
     * @param callable $fn
26
     *
27
     * @throws \Exception
28
     */
29
    public function assertThrows($throws, callable $fn)
30
    {
31
        $this->assertThrowsWithMessage($throws, false, $fn);
32
    }
33
    
34
    /**
35
     * Asserts that callback throws an exception with a message
36
     *
37
     * @param          $throws
38
     * @param          $message
39
     * @param callable $fn
40
     *
41
     * @throws Exception
42
     */
43
    public function assertThrowsWithMessage($throws, $message, callable $fn)
44
    {
45
        /** @var $this TestCase  * */
46
        $result = $this->getTestResultObject();
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
47
        
48
        $this->parseThrows($throws, $message);
0 ignored issues
show
Bug introduced by
The method parseThrows() does not seem to exist on object<PHPUnit\Framework\TestCase>.

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
        try {
51
            call_user_func($fn);
52
        } catch (AssertionFailedError $e) {
53
            if ($throws !== get_class($e)) {
54
                throw $e;
55
            }
56
            if ($this->exceptionMessageDoesNotMatchExpected($e, $message)) {
0 ignored issues
show
Bug introduced by
The method exceptionMessageDoesNotMatchExpected() does not seem to exist on object<PHPUnit\Framework\TestCase>.

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...
57
                throw new AssertionFailedError("exception message '$message' was expected, but '" . $e->getMessage() . "' was received");
58
            }
59
        } catch (\Exception $e) {
60
            if ($throws) {
61
                if ($throws !== get_class($e)) {
62
                    throw new AssertionFailedError("exception '$throws' was expected, but " . get_class($e) . ' was thrown');
63
                }
64
                if ($this->exceptionMessageDoesNotMatchExpected($e, $message)) {
0 ignored issues
show
Bug introduced by
The method exceptionMessageDoesNotMatchExpected() does not seem to exist on object<PHPUnit\Framework\TestCase>.

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...
65
                    throw new AssertionFailedError("exception message '$message' was expected, but '" . $e->getMessage() . "' was received");
66
                }
67
            } else {
68
                throw $e;
69
            }
70
        }
71
        
72
        if ($throws) {
73
            if (isset($e)) {
74
                $this->assertTrue(true, 'exception handled');
75
            } else {
76
                throw new AssertionFailedError("exception '$throws' was not thrown as expected");
77
            }
78
        }
79
    }
80
    
81
    /**
82
     * @param Exception $exception
83
     * @param string    $expectedMsg
84
     *
85
     * @return bool
86
     */
87
    private function exceptionMessageDoesNotMatchExpected(Exception $exception, string $expectedMsg) : bool
88
    {
89
        return $expectedMsg !== false && $expectedMsg !== strtolower($exception->getMessage());
90
    }
91
    
92
    /**
93
     * @param $throws
94
     * @param $message
95
     */
96
    private function parseThrows(&$throws, &$message)
97
    {
98
        if (is_array($throws)) {
99
            $message = ($throws[1])
100
                ? strtolower($throws[1])
101
                : false;
102
            $throws = $throws[0];
103
        }
104
    }
105
}