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

ErrorHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 46
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assertError() 0 9 4
A errorHandler() 0 4 1
A setUpErrorHandler() 0 5 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