Completed
Push — master ( 091a77...23fe96 )
by Richard
02:30
created

ErrorHandler::assertNoErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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
nc 1
nop 0
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
 *
11
 * This code is based upon http://www.sitepoint.com/testing-error-conditions-with-phpunit/
12
 */
13
trait ErrorHandler
14
{
15
    /**
16
     * @var mixed[]
17
     */
18
    private $errors;
19
20
    /**
21
     * Assert that the requested error was generated.
22
     *
23
     * @param string $errstr
24
     * @param int $errno
25
     */
26 8
    public function assertError(string $errstr, int $errno)
27
    {
28 8
        foreach ($this->errors as $error) {
29 7
            if ($error['errstr'] === $errstr && $error['errno'] === $errno) {
30 7
                $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...
31
32 7
                return;
33
            }
34
        }
35 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...
36
    }
37
38
    /**
39
     * Assert that no errors were generated.
40
     */
41 1
    public function assertNoErrors()
42
    {
43 1
        $this->assertEmpty($this->errors, sprintf('$s errors generated.', number_format(count($this->errors))));
0 ignored issues
show
Bug introduced by
It seems like assertEmpty() 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...
44 1
    }
45
46
    /**
47
     * Activate PHPUnit error handler.
48
     *
49
     * @before
50
     */
51 9
    protected function setUpErrorHandler()
52
    {
53 9
        $this->errors = [];
54 9
        set_error_handler(
55 9
            function (int $errno, string $errstr, string $errfile, int $errline, array $errcontext) {
56 7
                $this->errors[] = [
57 7
                    'errno' => $errno,
58 7
                    'errstr' => $errstr,
59 7
                    'errfile' => $errfile,
60 7
                    'errline' => $errline,
61 7
                    'errcontext' => $errcontext,
62
                ];
63 9
            },
64 9
            -1
65
        );
66 9
    }
67
}
68