Completed
Push — master ( cc0463...09303e )
by Chad
19s
created

FilterResponseTest::construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace TraderInteractiveTest;
4
5
use InvalidArgumentException;
6
use PHPUnit\Framework\TestCase;
7
use TraderInteractive\Exceptions\ReadOnlyViolationException;
8
use TraderInteractive\FilterResponse;
9
10
/**
11
 * @coversDefaultClass \TraderInteractive\FilterResponse
12
 */
13
class FilterResponseTest extends TestCase
14
{
15
    /**
16
     * @test
17
     * @covers ::__construct
18
     */
19
    public function construct()
20
    {
21
        $value = ['foo' => 'bar'];
22
        $errors = [];
23
        $unknowns = ['other' => 'unknown'];
24
25
        $response = new FilterResponse($value, $errors, $unknowns);
26
27
        $this->assertSame(true, $response->success);
28
        $this->assertSame($value, $response->filteredValue);
29
        $this->assertSame($errors, $response->errors);
30
        $this->assertSame(null, $response->errorMessage);
31
        $this->assertSame($unknowns, $response->unknowns);
32
    }
33
34
    /**
35
     * @test
36
     * @covers ::__construct
37
     */
38
    public function constructWithErrors()
39
    {
40
        $value = ['foo' => 'bar'];
41
        $errors = ['something bad happened', 'and something else too'];
42
        $unknowns = ['other' => 'unknown'];
43
44
        $response = new FilterResponse($value, $errors, $unknowns);
45
46
        $this->assertSame(false, $response->success);
47
        $this->assertSame($value, $response->filteredValue);
48
        $this->assertSame($errors, $response->errors);
49
        $this->assertSame("something bad happened\nand something else too", $response->errorMessage);
50
        $this->assertSame($unknowns, $response->unknowns);
51
    }
52
53
    /**
54
     * @test
55
     * @covers ::__construct
56
     */
57
    public function constructDefault()
58
    {
59
        $input = ['filtered' => 'input'];
60
61
        $response = new FilterResponse($input);
62
63
        $this->assertSame(true, $response->success);
64
        $this->assertSame($input, $response->filteredValue);
65
        $this->assertSame([], $response->errors);
66
        $this->assertSame(null, $response->errorMessage);
67
        $this->assertSame([], $response->unknowns);
68
    }
69
70
    /**
71
     * @test
72
     * @covers ::__construct
73
     */
74
    public function gettingInvalidPropertyThrowsException()
75
    {
76
        $this->expectException(InvalidArgumentException::class);
77
        $this->expectExceptionMessage("Property 'foo' does not exist");
78
79
        $response = new FilterResponse([]);
80
        $response->foo;
0 ignored issues
show
Documentation introduced by
The property foo does not exist on object<TraderInteractive\FilterResponse>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
81
    }
82
83
    /**
84
     * @test
85
     * @covers ::__construct
86
     */
87 View Code Duplication
    public function settingValidPropertyThrowsAnException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        $this->expectException(ReadOnlyViolationException::class);
90
        $this->expectExceptionMessage("Property 'success' is read-only");
91
92
        $response = new FilterResponse([]);
93
        $response->success = false;
94
    }
95
96
    /**
97
     * @test
98
     * @covers ::__construct
99
     */
100 View Code Duplication
    public function settingInvalidPropertyThrowsAnException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        $this->expectException(InvalidArgumentException::class);
103
        $this->expectExceptionMessage("Property 'foo' does not exist");
104
105
        $response = new FilterResponse([]);
106
        $response->foo = false;
0 ignored issues
show
Documentation introduced by
The property foo does not exist on object<TraderInteractive\FilterResponse>. 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...
107
    }
108
}
109