Test Failed
Branch master (91c650)
by Ryuichi
03:38 queued 57s
created

okDelegatableExceptionTest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 27 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace WebStream\Exception\Test;
3
4
require_once dirname(__FILE__) . '/../Modules/DI/Injector.php';
5
require_once dirname(__FILE__) . '/../Modules/Container/Container.php';
6
require_once dirname(__FILE__) . '/../Test/Fixtures/InjectedClass.php';
7
require_once dirname(__FILE__) . '/../Test/Providers/ExceptionDelegatorProvider.php';
8
require_once dirname(__FILE__) . '/../Delegate/ExceptionDelegator.php';
9
require_once dirname(__FILE__) . '/../ApplicationException.php';
10
require_once dirname(__FILE__) . '/../SystemException.php';
11
require_once dirname(__FILE__) . '/../DelegateException.php';
12
13
use WebStream\Exception\ApplicationException;
14
use WebStream\Exception\SystemException;
15
use WebStream\Exception\DelegateException;
16
use WebStream\Exception\Delegate\ExceptionDelegator;
17
use WebStream\Container\Container;
18
use WebStream\Exception\Test\Fixtures\InjectedClass;
19
use WebStream\Exception\Test\Providers\ExceptionDelegatorProvider;
20
21
/**
22
* ExceptionDelegatorTest
23
* @author Ryuichi TANAKA.
24
* @since 2017/01/07
25
* @version 0.7
26
 */
27
class ExceptionDelegatorTest extends \PHPUnit\Framework\TestCase
28
{
29
    use ExceptionDelegatorProvider;
30
31
    /**
32
     * 正常系
33
     * 例外オブジェクトを保持でき、任意のタイミングでスローできること
34
     * @test
35
     * @dataProvider exceptionProvider
36
     * @expectedException \Exception
37
     */
38
    public function okDelegatableExceptionTest($handledException, $exceptionObject)
0 ignored issues
show
Unused Code introduced by
The parameter $handledException is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
    {
40
        $instance = new InjectedClass();
41
        $delegator = new ExceptionDelegator($instance, $exceptionObject);
42
        $delegator->raise();
43
        $this->assertTrue(false);
44
    }
45
46
    /**
47
     * 正常系
48
     * ハンドリング可能な例外がスローされた場合の例外オブジェクト構造が正しいこと
49
     * さらに例外を指定クラスで捕捉できること
50
     * @test
51
     * @dataProvider exceptionProvider
52
     */
53
    public function okDelegateAndHandleTest($handledException, $exceptionObject)
54
    {
55
        $instance = new InjectedClass();
56
        $container = new Container();
57
        $container->exceptions = [$handledException];
0 ignored issues
show
Documentation introduced by
The property exceptions does not exist on object<WebStream\Container\Container>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
58
        $container->method = new \ReflectionMethod($instance, "handled1");
0 ignored issues
show
Documentation introduced by
The property method does not exist on object<WebStream\Container\Container>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
59
        $delegator = new ExceptionDelegator($instance, $exceptionObject);
60
        $delegator->setExceptionHandler([$container]);
61
62
        $isAsserted = false;
63
        try {
64
            $delegator->raise();
65
        } catch (\Exception $e) {
66
            $this->assertInstanceOf(DelegateException::class, $e);
67
            $this->expectOutputString("handled");
68
            $isAsserted = true;
69
        }
70
71
        if (!$isAsserted) {
72
            $this->assertTrue($false);
0 ignored issues
show
Bug introduced by
The variable $false does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
73
        }
74
    }
75
}
76