theResponseFieldInEmbeddedResourceShouldContain()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Behat\Behat\Context\Context;
4
use Behat\Behat\Context\SnippetAcceptingContext;
5
use Behat\Gherkin\Node\PyStringNode;
6
use PHPUnit_Framework_Assert as Assert;
7
use TomPHP\HalClient\Client;
8
use TomPHP\HalClient\Exception\HalClientException;
9
use TomPHP\HalClient\Exception\UnknownContentTypeException;
10
use TomPHP\HalClient\HttpClient;
11
use TomPHP\HalClient\HttpClient\DummyHttpClient;
12
use TomPHP\HalClient\HttpClient\GuzzleHttpClient;
13
use TomPHP\HalClient\Processor\HalJsonProcessor;
14
use TomPHP\HalClient\Resource\Node;
15
16
/**
17
 * Defines application features from the specific context.
18
 */
19
class FeatureContext implements Context, SnippetAcceptingContext
0 ignored issues
show
Deprecated Code introduced by
The interface Behat\Behat\Context\SnippetAcceptingContext has been deprecated with message: will be removed in 4.0. Use --snippets-for CLI option instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
20
{
21
    /** @var HttpClient */
22
    private $httpClient;
23
24
    /** @var Client */
25
    private $client;
26
27
    /** @var Response */
28
    private $response;
29
30
    /** @var HalClientException */
31
    private $error;
32
33
    /** @var Node */
34
    private $result;
35
36
    /** @var string */
37
    private $urlPrefix = '';
38
39
    /**
40
     * Initializes context.
41
     *
42
     * Every scenario gets its own context instance.
43
     * You can also pass arbitrary arguments to the
44
     * context constructor through behat.yml.
45
     */
46
    public function __construct($client)
47
    {
48
        if ($client === 'guzzle') {
49
            $this->httpClient = new GuzzleHttpClient();
50
            $this->urlPrefix  = 'http://localhost:1080';
51
        } else {
52
            $this->httpClient = new DummyHttpClient();
53
        }
54
55
        $this->client = new Client($this->httpClient, [
56
            new HalJsonProcessor(),
57
        ]);
58
    }
59
60
    /**
61
     * @Given a :method endpoint :url which returns content type :contentType and body:
62
     */
63
    public function aEndpointWhichReturnsContentTypeAndBody($method, $url, $contentType, PyStringNode $body)
64
    {
65
        $this->httpClient->createEndpoint($method, $url, $contentType, (string) $body);
66
    }
67
68
    /**
69
     * @When I make a GET request to :url
70
     */
71
    public function iMakeAGetRequestTo($url)
72
    {
73
        try {
74
            $this->response = $this->client->get($this->urlPrefix.$url);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->client->get($this->urlPrefix . $url) of type resource is incompatible with the declared type object<Response> of property $response.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
75
        } catch (HalClientException $error) {
76
            $this->error = $error;
77
        }
78
    }
79
80
    /**
81
     * @When I make a GET request to link :linkName from the response
82
     */
83
    public function iMakeAGetRequestToLinkFromTheResponse($linkName)
84
    {
85
        $this->response = $this->response->getLink($linkName)->get();
86
    }
87
88
    /**
89
     * @Then I should get a bad content type error
90
     */
91
    public function iShouldGetABadContentTypeError()
92
    {
93
        Assert::assertInstanceOf(UnknownContentTypeException::class, $this->error);
94
    }
95
96
    /**
97
     * @Then the response field :field should contain :value
98
     */
99
    public function theResponseFieldShouldContain($field, $value)
100
    {
101
        Assert::assertEquals($value, $this->response->$field->getValue());
102
    }
103
104
    /**
105
     * @Then the response field :fieldName in embedded resource :resourceName should contain :value
106
     */
107
    public function theResponseFieldInEmbeddedResourceShouldContain($resourceName, $fieldName, $value)
108
    {
109
        Assert::assertEquals($value, $this->response->getResource($resourceName)->$fieldName->getValue());
110
    }
111
112
    /**
113
     * @Then the field :level2 in response field :level1 should contain :value
114
     */
115
    public function theResponseFieldInResponseFieldShouldContain($level1, $level2, $value)
116
    {
117
        Assert::assertEquals($value, $this->response->$level1->$level2->getValue());
118
    }
119
120
    /**
121
     * @Then the field :fieldName at index :index in response field :resourceName should contain :value
122
     */
123
    public function theFieldAtIndexInResponseFieldShouldContain($fieldName, $resourceName, $value, $index)
124
    {
125
        Assert::assertEquals($value, $this->response->{$resourceName}[$index]->$fieldName->getValue());
126
    }
127
128
    /**
129
     * @Then the field :fieldName at index :index in resource field :resourceName should contain :value
130
     */
131
    public function theFieldAtIndexInResourceFieldShouldContain($fieldName, $resourceName, $value, $index)
132
    {
133
        Assert::assertEquals($value, $this->response->getResource($resourceName)[$index]->$fieldName->getValue());
134
    }
135
136
    /**
137
     * @Then I should find :count field with :fieldName matching :fieldValue in the :collection collection
138
     */
139
    public function iShouldFindFieldWithMatchingInTheCollection($fieldName, $fieldValue, $collection, $count)
140
    {
141
        $result = $this->response->$collection->findMatching([$fieldName => $fieldValue]);
142
143
        Assert::assertCount((int) $count, $result);
144
145
        $this->result = $result[0];
146
    }
147
148
    /**
149
     * @Then the field should have :name :value
150
     */
151
    public function theFieldShouldHave($name, $value)
152
    {
153
        Assert::assertEquals($value, $this->result->$name->getValue());
154
    }
155
}
156