it_throws_when_requesting_an_unknown_link()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace spec\TomPHP\HalClient\Resource;
4
5
use PhpSpec\ObjectBehavior;
6
use TomPHP\HalClient\Exception\FieldNotFoundException;
7
use TomPHP\HalClient\Exception\LinkNotFoundException;
8
use TomPHP\HalClient\Exception\ResourceNotFoundException;
9
use TomPHP\HalClient\ResourceFetcher;
10
use TomPHP\HalClient\Resource\FieldNode;
11
use TomPHP\HalClient\Resource\Link;
12
use TomPHP\HalClient\Resource\ResourceNode;
13
14
class ResourceSpec extends ObjectBehavior
15
{
16
    /** @var Link */
17
    private $link;
18
19
    function let(ResourceFetcher $fetcher, FieldNode $f1, FieldNode $f2, ResourceNode $resource)
20
    {
21
        $this->link = new Link($fetcher->getWrappedObject(), 'href');
22
23
        $this->beConstructedWith(
24
            [
25
                'field1' => $f1->getWrappedObject(),
26
                'field2' => $f2->getWrappedObject(),
27
            ],
28
            ['link1' => $this->link],
29
            ['resource1' => $resource->getWrappedObject()]
30
        );
31
    }
32
33
    function it_returns_fields_by_name(FieldNode $f1)
34
    {
35
        $this->getField('field1')->shouldBeLike($f1);
36
    }
37
38
    function it_returns_fields_by_magic_method(FieldNode $f1)
39
    {
40
        $this->field1->shouldReturn($f1);
0 ignored issues
show
Documentation introduced by
The property field1 does not exist on object<spec\TomPHP\HalCl...\Resource\ResourceSpec>. 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...
41
    }
42
43
    function it_throws_when_requesting_an_unknown_field()
44
    {
45
        $this->shouldThrow(new FieldNotFoundException('unknown'))
46
             ->duringGetField('unknown');
47
    }
48
49
    function it_lists_links()
50
    {
51
        $this->getLinks()->shouldReturn(['link1']);
52
    }
53
54
    function it_gets_link_by_name()
55
    {
56
        $this->getLink('link1')->shouldBeLike($this->link);
57
    }
58
59
    function it_throws_when_requesting_an_unknown_link()
60
    {
61
        $this->shouldThrow(new LinkNotFoundException('unknown'))
62
             ->duringGetLink('unknown');
63
    }
64
65
    function it_gets_resource_by_name(ResourceNode $resource)
66
    {
67
        $this->getResource('resource1')->shouldReturn($resource);
68
    }
69
70
    function it_throws_when_requesting_an_unknown_resource()
71
    {
72
        $this->shouldThrow(new ResourceNotFoundException('unknown'))
73
             ->duringGetResource('unknown');
74
    }
75
76
    function it_does_not_match_if_critera_includes_unknown_field()
77
    {
78
        $this->matches(['unknown_field' => 'some-value'])->shouldReturn(false);
79
    }
80
81 View Code Duplication
    function it_does_match_if_all_criteria_fields_match(FieldNode $f1, FieldNode $f2)
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...
82
    {
83
        $f1->matches('search-value1')->willReturn(true);
84
        $f2->matches('search-value2')->willReturn(true);
85
86
        $this->matches([
87
            'field1' => 'search-value1',
88
            'field2' => 'search-value2',
89
        ])->shouldReturn(true);
90
    }
91
92 View Code Duplication
    function it_does_not_match_if_any_criteria_fields_fail_to_match(FieldNode $f1, FieldNode $f2)
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...
93
    {
94
        $f1->matches('search-value1')->willReturn(true);
95
        $f2->matches('search-value2')->willReturn(false);
96
97
        $this->matches([
98
            'field1' => 'search-value1',
99
            'field2' => 'search-value2',
100
        ])->shouldReturn(false);
101
    }
102
103
    function it_does_not_match_unknown_resource()
104
    {
105
        $this->matches([
106
            'field1' => 'search-value1',
107
            ['resource', 'unknown', ['resource_field' => 'search-value2']],
108
        ])->shouldReturn(false);
109
    }
110
111 View Code Duplication
    function it_matches_if_resource_matches(FieldNode $f1, ResourceNode $resource)
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...
112
    {
113
        $f1->matches('search-value1')->willReturn(true);
114
        $resource->matches(['resource_field' => 'search-value2'])->willReturn(true);
115
116
        $this->matches([
117
            'field1' => 'search-value1',
118
            ['resource', 'resource1', ['resource_field' => 'search-value2']],
119
        ])->shouldReturn(true);
120
    }
121
122 View Code Duplication
    function it_does_not_match_if_resource_does_not_match(FieldNode $f1, ResourceNode $resource)
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...
123
    {
124
        $f1->matches('search-value1')->willReturn(true);
125
        $resource->matches(['resource_field' => 'search-value2'])->willReturn(false);
126
127
        $this->matches([
128
            'field1' => 'search-value1',
129
            ['resource', 'resource1', ['resource_field' => 'search-value2']],
130
        ])->shouldReturn(false);
131
    }
132
}
133