Skip to content

Commit f32a262

Browse files
committed
Merge pull request #1 from rezzza/feature/improve-moco-reliability
Improve moco reliability
2 parents 9e95cee + 0cd0c1d commit f32a262

File tree

7 files changed

+47
-30
lines changed

7 files changed

+47
-30
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ default:
1313
extensions:
1414
Rezzza\MocoBehatExtension\MocoExtension:
1515
json_file: features/fixtures.yml
16+
hostname: 127.0.0.1
17+
port: 9997
1618
suites:
1719
default:
1820
contexts:
19-
- Rezzza\MocoBehatExtension\MocoContext:
20-
mocoIp: 127.0.0.1
21-
mocoPort: 9997
21+
- Rezzza\MocoBehatExtension\MocoContext
2222
```
2323
24-
Then you just need to add `MocoWriter` as an argument of your context.
24+
Then you just need to add `MocoWriter` as an argument of your context and tag your scenario with `@moco`
2525

2626
[See tests](features) for more details

features/check-moco.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ Feature: Check moco
1010
extensions:
1111
Rezzza\MocoBehatExtension\MocoExtension:
1212
json_file: features/fixtures.yml
13+
hostname: 127.0.0.1
14+
port: 9997
1315
Rezzza\RestApiBehatExtension\Extension:
1416
rest:
1517
base_url: http://127.0.0.1:9997
1618
adaptor_name: curl
1719
suites:
1820
default:
1921
contexts:
20-
- Rezzza\RestApiBehatExtension\RestApiContext:
21-
- Rezzza\MocoBehatExtension\MocoContext:
22-
mocoIp: 127.0.0.1
23-
mocoPort: 9997
22+
- Rezzza\RestApiBehatExtension\RestApiContext
23+
- Rezzza\MocoBehatExtension\MocoContext
2424
"""
2525
And a file named "features/call_moco.feature" with:
2626
"""

features/mock-http-call.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Feature: Mock HTTP call
1010
extensions:
1111
Rezzza\MocoBehatExtension\MocoExtension:
1212
json_file: features/fixtures.yml
13+
hostname: 127.0.0.1
14+
port: 9999
1315
Rezzza\RestApiBehatExtension\Extension:
1416
rest:
1517
base_url: http://127.0.0.1:9999
@@ -18,10 +20,8 @@ Feature: Mock HTTP call
1820
default:
1921
contexts:
2022
- FeatureContext
21-
- Rezzza\RestApiBehatExtension\RestApiContext:
22-
- Rezzza\MocoBehatExtension\MocoContext:
23-
mocoIp: 127.0.0.1
24-
mocoPort: 9999
23+
- Rezzza\RestApiBehatExtension\RestApiContext
24+
- Rezzza\MocoBehatExtension\MocoContext
2525
"""
2626
And a file named "features/bootstrap/FeatureContext.php" with:
2727
"""

src/MocoContext.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,22 @@
33
namespace Rezzza\MocoBehatExtension;
44

55
use Behat\Behat\Context\Context;
6-
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
6+
use Behat\Behat\Hook\Scope\AfterScenarioScope;
77

88
class MocoContext implements Context
99
{
1010
private $mocoWriter;
1111

12-
private $mocoIp;
13-
14-
private $mocoPort;
15-
16-
public function __construct(MocoWriter $mocoWriter, $mocoIp, $mocoPort)
12+
public function __construct(MocoWriter $mocoWriter)
1713
{
1814
$this->mocoWriter = $mocoWriter;
19-
$this->mocoIp = $mocoIp;
20-
$this->mocoPort = $mocoPort;
2115
}
2216

2317
/**
24-
* @BeforeScenario @moco
18+
* @AfterScenario @moco
2519
*/
26-
public function before(BeforeScenarioScope $scope)
20+
public function before(AfterScenarioScope $scope)
2721
{
28-
if (false === @fsockopen($this->mocoIp, $this->mocoPort, $errno, $errstr, 3)) {
29-
throw new \Exception(
30-
sprintf('You should run moco by "bin/moco start -p %s -c %s"', $this->mocoPort, $this->mocoWriter->getJsonFile())
31-
);
32-
}
3322
$this->mocoWriter->reset();
3423
}
3524
}

src/MocoExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class MocoExtension implements Extension
1414
public function load(ContainerBuilder $container, array $config)
1515
{
1616
$container->setParameter('rezzza.moco.json_file', $config['json_file']);
17+
$container->setParameter('rezzza.moco.port', $config['port']);
18+
$container->setParameter('rezzza.moco.hostname', $config['hostname']);
1719
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/Resources/config'));
1820
$loader->load('services.xml');
1921
}
@@ -29,6 +31,8 @@ public function configure(ArrayNodeDefinition $builder)
2931
->addDefaultsIfNotSet()
3032
->children()
3133
->scalarNode('json_file')->end()
34+
->scalarNode('hostname')->end()
35+
->scalarNode('port')->end()
3236
->end()
3337
;
3438
}

src/MocoWriter.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ class MocoWriter
88

99
private $jsonFile;
1010

11-
public function __construct($jsonFile)
11+
private $hostname;
12+
13+
private $port;
14+
15+
public function __construct($jsonFile, $hostname, $port)
1216
{
1317
$this->jsonFile = $jsonFile;
18+
$this->hostname = $hostname;
19+
$this->port = $port;
1420
}
1521

1622
public function mockHttpCall(array $matchedRequest, array $mockedResponse)
@@ -24,17 +30,33 @@ public function mockHttpCall(array $matchedRequest, array $mockedResponse)
2430
public function writeForMoco()
2531
{
2632
file_put_contents($this->jsonFile, json_encode($this->payload));
33+
// We need to wait for moco detecting the fixtures file changed
34+
// If not, we can perform a request on old configFile
2735
sleep(1);
36+
$this->waitForMoco();
2837
}
2938

3039
public function reset()
3140
{
3241
$this->payload = [];
42+
// To avoid false positive by having the next scenario using moco response of the previous scenario
3343
$this->writeForMoco();
3444
}
3545

36-
public function getJsonFile()
46+
private function waitForMoco()
3747
{
38-
return $this->jsonFile;
48+
$attempts = 0;
49+
$max = 10;
50+
$ip = gethostbyname($this->hostname);
51+
while (false === @stream_socket_client('tcp://'.$ip.':'.$this->port, $errno, $errstr, 5) && ($attempts < $max)) {
52+
usleep(200000); // 200ms
53+
$attempts++;
54+
}
55+
56+
if ($max <= $attempts) {
57+
throw new \Exception(
58+
sprintf('You should run moco by "bin/moco start -p %s -c %s"', $this->port, $this->jsonFile)
59+
);
60+
}
3961
}
4062
}

src/Resources/config/services.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<services>
66
<service id="rezzza.moco.moco_writer" class="Rezzza\MocoBehatExtension\MocoWriter" public="false">
77
<argument>%rezzza.moco.json_file%</argument>
8+
<argument>%rezzza.moco.hostname%</argument>
9+
<argument>%rezzza.moco.port%</argument>
810
</service>
911

1012
<service id="rezzza.moco.moco_writer_resolver" class="Rezzza\MocoBehatExtension\MocoWriterResolver" public="false">

0 commit comments

Comments
 (0)