Completed
Push — master ( 017835...f95b3f )
by Sergey
02:43
created

ExpectExceptionTrait::expectExceptionCode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
1
<?php
2
/**
3
 * Common tests stuff
4
 *
5
 * @see       https://github.com/sergeymakinen/tests
6
 * @copyright Copyright (c) 2017 Sergey Makinen (https://makinen.ru)
7
 * @license   https://github.com/sergeymakinen/tests/blob/master/LICENSE MIT License
8
 */
9
10
namespace SergeyMakinen\Tests\Util;
11
12
trait ExpectExceptionTrait
13
{
14
    /**
15
     * @param string $exception
16
     * @throws \Exception
17
     */
18
    public function expectException($exception)
19
    {
20
        if (!is_string($exception)) {
21
            /** @var \PHPUnit_Util_InvalidArgumentHelper|\PHPUnit\Util\InvalidArgumentHelper $class */
22
            $class = $this->getInvalidArgumentHelperClass();
23
            throw $class::factory(1, 'string');
24
        }
25
26
        $this->setInaccessibleProperty($this, 'expectedException', $exception);
0 ignored issues
show
Bug introduced by
It seems like setInaccessibleProperty() 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...
27
    }
28
29
    /**
30
     * @param int|string $code
31
     * @throws \Exception
32
     */
33
    public function expectExceptionCode($code)
34
    {
35
        if (!$this->getInaccessibleProperty($this, 'expectedException')) {
0 ignored issues
show
Bug introduced by
It seems like getInaccessibleProperty() 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
            $this->setInaccessibleProperty($this, 'expectedException', 'Exception');
0 ignored issues
show
Bug introduced by
It seems like setInaccessibleProperty() 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...
37
        }
38
        if (!is_int($code) && !is_string($code)) {
39
            /** @var \PHPUnit_Util_InvalidArgumentHelper|\PHPUnit\Util\InvalidArgumentHelper $class */
40
            $class = $this->getInvalidArgumentHelperClass();
41
            throw $class::factory(1, 'integer or string');
42
        }
43
44
        $this->setInaccessibleProperty($this, 'expectedExceptionCode', $code);
0 ignored issues
show
Bug introduced by
It seems like setInaccessibleProperty() 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...
45
    }
46
47
    /**
48
     * @param string $message
49
     * @throws \Exception
50
     */
51
    public function expectExceptionMessage($message)
52
    {
53
        if (!$this->getInaccessibleProperty($this, 'expectedException')) {
0 ignored issues
show
Bug introduced by
It seems like getInaccessibleProperty() 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...
54
            $this->setInaccessibleProperty($this, 'expectedException', 'Exception');
0 ignored issues
show
Bug introduced by
It seems like setInaccessibleProperty() 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...
55
        }
56
        if (!is_string($message)) {
57
            /** @var \PHPUnit_Util_InvalidArgumentHelper|\PHPUnit\Util\InvalidArgumentHelper $class */
58
            $class = $this->getInvalidArgumentHelperClass();
59
            throw $class::factory(1, 'string');
60
        }
61
62
        $this->setInaccessibleProperty($this, 'expectedExceptionMessage', $message);
0 ignored issues
show
Bug introduced by
It seems like setInaccessibleProperty() 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...
63
    }
64
65
    /**
66
     * @param string $messageRegExp
67
     * @throws \Exception
68
     */
69
    public function expectExceptionMessageRegExp($messageRegExp)
70
    {
71
        if (!$this->getInaccessibleProperty($this, 'expectedException')) {
0 ignored issues
show
Bug introduced by
It seems like getInaccessibleProperty() 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...
72
            $this->setInaccessibleProperty($this, 'expectedException', 'Exception');
0 ignored issues
show
Bug introduced by
It seems like setInaccessibleProperty() 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...
73
        }
74
        if (!is_string($messageRegExp)) {
75
            /** @var \PHPUnit_Util_InvalidArgumentHelper|\PHPUnit\Util\InvalidArgumentHelper $class */
76
            $class = $this->getInvalidArgumentHelperClass();
77
            throw $class::factory(1, 'string');
78
        }
79
80
        $this->setInaccessibleProperty($this, 'expectedExceptionMessageRegExp', $messageRegExp);
0 ignored issues
show
Bug introduced by
It seems like setInaccessibleProperty() 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...
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    private function getInvalidArgumentHelperClass()
87
    {
88
        return class_exists('PHPUnit_Util_InvalidArgumentHelper') ? 'PHPUnit_Util_InvalidArgumentHelper' : 'PHPUnit\Util\InvalidArgumentHelper';
89
    }
90
}
91