Skip to content
This repository was archived by the owner on Jul 4, 2023. It is now read-only.

Commit f8153e7

Browse files
committed
Tests working ok. Wiremock not dying at end.
1 parent 89a1dab commit f8153e7

File tree

10 files changed

+137
-15
lines changed

10 files changed

+137
-15
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
],
77
"autoload" : {
88
"psr-4" : {
9-
"Codeception\\Extension\\" : "src"
9+
"Codeception\\" : "src"
1010
}
1111
},
1212
"name" : "mcustiel/codeception-wiremock-extension",
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22
namespace Codeception\Extension;
33

4-
class WireMock extends \Codeception\Platform\Extension
4+
use Codeception\Platform\Extension as CodeceptionExtension;
5+
6+
class WireMock extends CodeceptionExtension
57
{
68
const DEFAULT_LOGS_PATH = '/tmp/codeceptionWireMock/logs/';
79

@@ -111,14 +113,14 @@ private function getLogsPath()
111113

112114
private function checkLogsPath($logsPath)
113115
{
114-
if (! is_dir($logsPath) || ! is_writable($logsPath)) {
116+
if (!is_dir($logsPath) || !is_writable($logsPath)) {
115117
throw \Exception("Directory $logsPath does not exist");
116118
}
117119
}
118120

119121
private function checkJarExists($jar)
120122
{
121-
if (! file_exists($jar)) {
123+
if (!file_exists($jar)) {
122124
throw \Exception("File $jar does not exist");
123125
}
124126
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Codeception\Extension;
33

4+
use WireMock\Client\WireMock as WireMockClient;
5+
46
class WireMockConnection
57
{
68
/**
@@ -9,11 +11,17 @@ class WireMockConnection
911
static private $connection;
1012

1113

12-
public static function setConnection($connection)
14+
/**
15+
* @param \WireMock\Client\WireMock $connection
16+
*/
17+
public static function setConnection(WireMockClient $connection)
1318
{
1419
self::$connection = $connection;
1520
}
1621

22+
/**
23+
* @return \WireMock\Client\WireMock
24+
*/
1725
public static function get()
1826
{
1927
return self::$connection;
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ public function start($jarPath, $logsPath, $arguments)
2121
}
2222
$logFile = rtrim($logsPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . self::LOG_FILE_NAME;
2323
$descriptors = [
24-
['pipe', 'r'],
2524
['file', $logFile, 'w'],
2625
['file', $logFile, 'a'],
2726
];
27+
echo "Command - " . $this->getCommandPrefix() . "java -jar {$jarPath}{$arguments}";
2828
$this->process = proc_open(
2929
$this->getCommandPrefix() . "java -jar {$jarPath}{$arguments}",
3030
$descriptors,
31-
$this->pipes
31+
$this->pipes,
32+
null,
33+
null,
34+
['bypass_shell' => true]
3235
);
3336
$this->checkProcessIsRunning();
3437
}
@@ -42,13 +45,17 @@ private function checkProcessIsRunning()
4245

4346
public function stop()
4447
{
45-
if ($this->process !== null) {
48+
if (is_resource($this->process)) {
4649
foreach ($this->pipes AS $pipe) {
4750
if (is_resource($pipe)) {
51+
fflush($pipe);
4852
fclose($pipe);
4953
}
5054
}
51-
proc_terminate($this->process, SIGINT);
55+
proc_terminate($this->process, SIGKILL);
56+
var_export(proc_get_status($this->process));
57+
proc_close($this->process, SIGKILL);
58+
unset($this->process);
5259
}
5360
}
5461

src/Module/WireMock.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
namespace Codeception\Module;
3+
4+
use Codeception\Module as CodeceptionModule;
5+
use Codeception\TestCase;
6+
use Codeception\Extension\WireMockConnection;
7+
use WireMock\Client\MappingBuilder;
8+
use WireMock\Client\RequestPatternBuilder;
9+
10+
class WireMock extends CodeceptionModule
11+
{
12+
/**
13+
* @var \WireMock\Client\WireMock
14+
*/
15+
private $wireMock;
16+
17+
/**
18+
* {@inheritDoc}
19+
* @see \Codeception\Module::_before()
20+
*/
21+
public function _before(TestCase $testCase)
22+
{
23+
parent::_before($testCase);
24+
$this->wireMock = WireMockConnection::get();
25+
}
26+
27+
public function ensureThereAreNoRequests()
28+
{
29+
$this->wireMock->reset();
30+
}
31+
32+
/**
33+
* @param \WireMock\Client\MappingBuilder $builder
34+
*/
35+
public function expectRequest(MappingBuilder $builder)
36+
{
37+
$this->wireMock->stubFor($builder);
38+
}
39+
40+
/**
41+
* @param \WireMock\Client\RequestPatternBuilder|integer $builderOrCount
42+
* @param \WireMock\Client\RequestPatternBuilder $builder
43+
*/
44+
public function receivedRequest($builderOrCount, RequestPatternBuilder $builder = null)
45+
{
46+
$this->wireMock->verify($builderOrCount, $builder);
47+
}
48+
49+
/**
50+
* {@inheritDoc}
51+
* @see \Codeception\Module::_after()
52+
*/
53+
public function _after(TestCase $testCase)
54+
{
55+
parent::_after($testCase);
56+
}
57+
}

tests/tests/_support/_generated/AcceptanceTesterActions.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php //[STAMP] 82653effac1fd2e2bb22b90f3b5a44d1
1+
<?php //[STAMP] d11d93aaca4415cb56005f16e3ef5973
22
namespace _generated;
33

44
// This class was automatically generated by build task
@@ -7,6 +7,7 @@
77

88
use Codeception\Module\PhpBrowser;
99
use Helper\Acceptance;
10+
use Codeception\Module\WireMock;
1011

1112
trait AcceptanceTesterActions
1213
{
@@ -2014,4 +2015,38 @@ public function dontSeeInTitle($title) {
20142015
public function switchToIframe($name) {
20152016
return $this->getScenario()->runStep(new \Codeception\Step\Action('switchToIframe', func_get_args()));
20162017
}
2018+
2019+
2020+
/**
2021+
* [!] Method is generated. Documentation taken from corresponding module.
2022+
*
2023+
*
2024+
* @see \Codeception\Module\WireMock::ensureThereAreNoRequests()
2025+
*/
2026+
public function ensureThereAreNoRequests() {
2027+
return $this->getScenario()->runStep(new \Codeception\Step\Action('ensureThereAreNoRequests', func_get_args()));
2028+
}
2029+
2030+
2031+
/**
2032+
* [!] Method is generated. Documentation taken from corresponding module.
2033+
*
2034+
* @param \WireMock\Client\MappingBuilder $builder
2035+
* @see \Codeception\Module\WireMock::expectRequest()
2036+
*/
2037+
public function expectRequest($builder) {
2038+
return $this->getScenario()->runStep(new \Codeception\Step\Action('expectRequest', func_get_args()));
2039+
}
2040+
2041+
2042+
/**
2043+
* [!] Method is generated. Documentation taken from corresponding module.
2044+
*
2045+
* @param \WireMock\Client\RequestPatternBuilder|integer $builderOrCount
2046+
* @param \WireMock\Client\RequestPatternBuilder $builder
2047+
* @see \Codeception\Module\WireMock::receivedRequest()
2048+
*/
2049+
public function receivedRequest($builderOrCount, $builder = null) {
2050+
return $this->getScenario()->runStep(new \Codeception\Step\Action('receivedRequest', func_get_args()));
2051+
}
20172052
}

tests/tests/acceptance.suite.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ modules:
99
enabled:
1010
- PhpBrowser:
1111
url: http://localhost/myapp
12-
- \Helper\Acceptance
12+
- \Helper\Acceptance
13+
- WireMock

tests/tests/acceptance/WelcomeCest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
use Codeception\Extension\WiremockConnection;
44
use Codeception\Util\Debug;
5+
use WireMock\Client\WireMock;
56

67
class WelcomeCest
78
{
89
/**
910
* @var \WireMock\Client\WireMock
1011
*/
11-
private $wiremock;
12+
//private $wiremock;
1213

1314
public function _before(\AcceptanceTester $I)
1415
{
15-
$this->wiremock = WiremockConnection::get();
16+
// $this->wiremock = WiremockConnection::get();
17+
$I->ensureThereAreNoRequests();
1618
}
1719

1820
public function _after(\AcceptanceTester $I)
@@ -22,7 +24,17 @@ public function _after(\AcceptanceTester $I)
2224
// tests
2325
public function tryToTest(\AcceptanceTester $I)
2426
{
25-
Debug::debug(var_export($this->wiremock, true));
26-
27+
$I->expectRequest(
28+
WireMock::get(WireMock::urlEqualTo('/some/url'))
29+
->willReturn(WireMock::aResponse()
30+
->withHeader('Content-Type', 'text/plain')
31+
->withBody('Hello world!'))
32+
);
33+
$response = file_get_contents('http://localhost:18080/some/url');
34+
//Debug::debug(var_export($this->wiremock, true));
35+
Debug::debug(var_export($response, true));
36+
$I->receivedRequest(
37+
WireMock::getRequestedFor(WireMock::urlEqualTo('/some/url'))
38+
);
2739
}
2840
}

0 commit comments

Comments
 (0)