Completed
Push — master ( 6e6006...017118 )
by Richard
02:39
created

ErrorHandler::errorHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 5
crap 1
1
<?php
2
3
namespace DigiTickets\PHPUnit;
4
5
/**
6
 * Class ErrorHandling.
7
 *
8
 * To activate PHPUnit error handling:
9
 * 1. Add 'use \DigiTickets\PHPUnit\ErrorHandler;' to your unit test.
10
 * 2. Call $this->setUpErrorHandler(); from within the unit test's setUp() method.
11
 *
12
 * This code is based upon http://www.sitepoint.com/testing-error-conditions-with-phpunit/
13
 */
14
trait ErrorHandler
15
{
16
    /**
17
     * @var mixed[]
18
     */
19
    private $errors;
20
21
    /**
22
     * Assert that the requested error was generated.
23
     *
24
     * @param string $errstr
25
     * @param int    $errno
26
     */
27 9
    public function assertError($errstr, $errno)
28
    {
29 9
        foreach ($this->errors as $error) {
30 8
            if ($error['errstr'] === $errstr && $error['errno'] === $errno) {
31 8
                return $this->assertTrue(true);
0 ignored issues
show
Bug introduced by
It seems like assertTrue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
32
            }
33
        }
34 1
        $this->fail("Error with level {$errno} and message '{$errstr}' not found in ".var_export($this->errors, true));
0 ignored issues
show
Bug introduced by
It seems like fail() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
35
    }
36
37
    /**
38
     * Error handler for PHPUnit.
39
     *
40
     * @param int    $errno
41
     * @param string $errstr
42
     * @param string $errfile
43
     * @param int    $errline
44
     * @param array  $errcontext
45
     */
46 8
    public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
47
    {
48 8
        $this->errors[] = compact('errno', 'errstr', 'errfile', 'errline', 'errcontext');
49 8
    }
50
51
    /**
52
     * Activate PHPUnit error handler.
53
     */
54 9
    protected function setUpErrorHandler()
55
    {
56 9
        $this->errors = [];
57 9
        set_error_handler([$this, 'errorHandler'], -1);
58 9
    }
59
}
60