Skip to content

Commit d88cde0

Browse files
authored
Merge pull request #8 from ademarco/master
Add JsonApiAwareInterface so that contexts can opt-in
2 parents a3fcbc8 + 310234a commit d88cde0

File tree

4 files changed

+42
-47
lines changed

4 files changed

+42
-47
lines changed

README.md

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,16 @@ To avoid having to use OAuth to retrieve an access token for each API call you c
6262

6363
## Usage
6464

65-
To use the step definitions provided by this extension you need to modify your `FeatureContext.php` file to extend the `JsonApiContext` instead of the standard `BehatContext` and call the `parent::construct()` method in the constructor.
65+
To use the step definitions provided by this extension just load the context class in your suites:
6666

67-
68-
```php
69-
<?php
70-
71-
use Kielabokkie\BehatJsonApi\Context\JsonApiContext;
72-
73-
/**
74-
* Defines application features from the specific context.
75-
*/
76-
class FeatureContext extends JsonApiContext
77-
{
78-
}
67+
```yaml
68+
default:
69+
suites:
70+
default:
71+
contexts:
72+
- Kielabokkie\BehatJsonApi\Context\JsonApiContext
7973
```
80-
81-
When you've made the changes above to your FeatureContext class you get access to the following step definitions:
74+
You will then have access to the following step definitions:
8275
8376
@Given I use the access token
8477
@Given I use access token :token
@@ -125,38 +118,12 @@ In some cases you might want to override the base url for a specific suite. Belo
125118
paths:
126119
- %paths.base%/tests/Behat/features/api
127120
contexts:
128-
- FeatureContext: ~
121+
- Kielabokkie\BehatJsonApi\Context\JsonApiContext: ~
129122
hooks:
130123
paths:
131124
- %paths.base%/tests/Behat/features/hooks
132125
contexts:
133-
- FeatureContext:
126+
- Kielabokkie\BehatJsonApi\Context\JsonApiContext:
134127
- http://hooks.yourapp.dev
135128
extensions:
136129
Kielabokkie\BehatJsonApi: ~
137-
138-
You also need to add the following to the constructor of your `FeatureContext.php` class.
139-
140-
```php
141-
<?php
142-
143-
use Kielabokkie\BehatJsonApi\Context\JsonApiContext;
144-
145-
/**
146-
* Defines application features from the specific context.
147-
*/
148-
class FeatureContext extends JsonApiContext
149-
{
150-
/**
151-
* Initialize the context
152-
*/
153-
public function __construct($baseUrl = null)
154-
{
155-
parent::__construct();
156-
157-
if (is_null($baseUrl) === false) {
158-
$this->baseUrl = rtrim($baseUrl, '/');
159-
}
160-
}
161-
}
162-
```

src/Context/Initializer/JsonApiInitializer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Behat\Behat\Context\Initializer\ContextInitializer;
44
use Behat\Behat\Context\Context;
5+
use Kielabokkie\BehatJsonApi\Context\JsonApiAwareInterface;
56

67
class JsonApiInitializer implements ContextInitializer
78
{
@@ -34,7 +35,9 @@ public function __construct($baseUrl, array $parameters)
3435
*/
3536
public function initializeContext(Context $context)
3637
{
37-
$context->setBaseUrl($this->baseUrl);
38-
$context->setParameters($this->parameters);
38+
if ($context instanceof JsonApiAwareInterface) {
39+
$context->setBaseUrl($this->baseUrl);
40+
$context->setParameters($this->parameters);
41+
}
3942
}
4043
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Kielabokkie\BehatJsonApi\Context;
4+
5+
/**
6+
* Interface JsonApiAwareContext.
7+
*
8+
* @package Kielabokkie\BehatJsonApi\Context
9+
*/
10+
interface JsonApiAwareInterface
11+
{
12+
13+
/**
14+
* Set the base url (specified in behat.yml)
15+
*
16+
* @param string $baseUrl [description]
17+
*/
18+
public function setBaseUrl($baseUrl);
19+
20+
/**
21+
* Set extension specific parameters (specified in behat.yml)
22+
*/
23+
public function setParameters(array $parameters);
24+
25+
}

src/Context/JsonApiContext.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* Defines application features from the specific context.
1212
*/
13-
class JsonApiContext implements SnippetAcceptingContext
13+
class JsonApiContext implements SnippetAcceptingContext, JsonApiAwareInterface
1414
{
1515
/**
1616
* @var \Buzz\Browser
@@ -825,7 +825,7 @@ protected function getResponsePayload()
825825
/**
826826
* Checks the response exists and returns it.
827827
*
828-
* @return Guzzle\Http\Message\Response
828+
* @return \Buzz\Message\Response
829829
*/
830830
protected function getResponse()
831831
{

0 commit comments

Comments
 (0)