Skip to content

Commit e16cc97

Browse files
mcustielMariano Custiel
andauthored
Feature/select suites (#41)
* Allow to start several phiremock instances * Added support for secure phiremock server * Set defaults * Allows to provide a list of the suites phiremock-server must be run for * Cleaned a bit * Cleaned up a bit * Removed unused parameters * Added documentation for new features * Update README.md * Update README.md * Cleaned code a bit * Fixed composer json Co-authored-by: Mariano Custiel <[email protected]>
1 parent e66d233 commit e16cc97

File tree

13 files changed

+569
-185
lines changed

13 files changed

+569
-185
lines changed

README.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# phiremock-codeception-extension
2-
Codeception extension to make working with [Phiremock Server](https://github.com/mcustiel/phiremock-server) even easier. It allows to start a Phiremock Server before each suite and stop it when the suite ends.
2+
Codeception extension to make working with [Phiremock Server](https://github.com/mcustiel/phiremock-server) even easier. It allows to start a Phiremock Server before a suite is executed and stop it when the suite ends.
33

44
[![Latest Stable Version](https://poser.pugx.org/mcustiel/phiremock-codeception-extension/v/stable)](https://packagist.org/packages/mcustiel/phiremock-codeception-extension)
55
[![Build Status](https://scrutinizer-ci.com/g/mcustiel/phiremock-codeception-extension/badges/build.png?b=master)](https://scrutinizer-ci.com/g/mcustiel/phiremock-codeception-extension/build-status/master)
@@ -42,37 +42,101 @@ extensions:
4242
start_delay: 1 # default to 0
4343
expectations_path: /my/expectations/path # defaults to tests/_expectations
4444
server_factory: \My\FactoryClass # defaults to 'default'
45+
extra_instances: [] # deaults to an empty array
46+
suites: [] # defaults to an empty array
47+
certificate: /path/to/cert # defaults to null
48+
certificate_key: /path/to/cert-key # defaults to null
49+
cert_passphrase: 'my-pass' # defaults to null
4550
```
4651
Note: Since Codeception version 2.2.7, extensions configuration can be added directly in the suite configuration file. That will avoid phiremock to be started for every suite.
4752

4853
### Parameters
4954

5055
#### listen
5156
Specifies the interface and port where phiremock must listen for requests.
57+
5258
**Default:** 0.0.0.0:8086
5359

5460
#### bin_path
5561
Path where Phiremock Server's "binary" is located. You can, for instance, point to the location of the phar in your file system.
62+
5663
**Default:** codeception_dir/../vendor/bin/phiremock
5764

5865
#### logs_path
5966
Path where to write the output.
67+
6068
**Default:** codeception's tests output dir
6169

6270
#### debug
6371
Whether to write debug data to log file.
72+
6473
**Default:** false
6574

6675
#### start_delay
6776
Time to wait after Phiremock Server is started before running the tests (used to give time to Phiremock Server to boot)
77+
6878
**Default:** 0
6979

7080
#### expectations_path
7181
Specifies a directory to search for json files defining expectations to load by default.
82+
7283
**Default:** codecption_dir/_expectations
7384

85+
#### certificate
86+
Path to a certificate file to allow phiremock-server to listen for secure https connections.
87+
88+
**Default:** null. Meaning phiremock will only listen on unsecured http connections.
89+
90+
#### certificate-key
91+
Path to the certificate key file.
92+
93+
**Default:** null.
94+
95+
#### cert-passphrase
96+
Path to the certificate passphrase used to encrypt the certificate (only needed if encrypted).
97+
98+
**Default:** null. Meaning no decryption based in passphrase will be performed.
99+
100+
#### suites
101+
Specifies a list of suites for which the phiremock-server must be executed.
102+
103+
**Default:** [] Empty array, meaning that phiremock will be executed for each suite.
104+
105+
#### extra_instances
106+
Allows to specify more instances of phiremock-server to run. This is useful if you want, for instance, run one instance listening for http and one listening for https connections. Each instance has its own configuration, and can separately run for different suites.
107+
108+
**Default:** [] Empty array, meaning that no extra phiremock-server instances are configured.
109+
110+
**Example:**
111+
```yaml
112+
extensions:
113+
enabled:
114+
- \Codeception\Extension\Phiremock
115+
config:
116+
\Codeception\Extension\Phiremock:
117+
listen: 127.0.0.1:18080
118+
debug: true
119+
start_delay: 1
120+
expectations_path: /my/expectations/path-1
121+
suites:
122+
- acceptance
123+
extra_instances:
124+
-
125+
listen: 127.0.0.1:18081
126+
debug: true
127+
start_delay: 1
128+
expectations_path: /my/expectations/path-2
129+
suites:
130+
- acceptance
131+
- api
132+
certificate: /path/to/cert
133+
certificate_key: /path/to/cert-key
134+
cert_passphrase: 'my-pass'
135+
```
136+
74137
#### server_factory
75138
Specifies a Factory class extending `\Mcustiel\Phiremock\Server\Factory\Factory`. Useful if you want to provide your own PSR. This works only if you install phiremock as a local dependency required in your composer file.
139+
76140
**Default:** default
77141

78142
**Example:**

codeception.https.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
actor: Tester
2+
paths:
3+
tests: tests
4+
log: tests/_output
5+
data: tests/_data
6+
support: tests/_support
7+
envs: tests/_envs
8+
settings:
9+
colors: true
10+
memory_limit: 1024M
11+
bootstrap: _bootstrap.php
12+
extensions:
13+
enabled:
14+
- \Codeception\Extension\RunFailed
15+
- \Codeception\Extension\Phiremock
16+
config:
17+
\Codeception\Extension\Phiremock:
18+
listen: 0.0.0.0:18080
19+
expectations_path:
20+
server_factory: Mcustiel\Phiremock\Codeception\Module\Tests\Helpers\FactoryWithGuzzle7
21+
logs_path: tests/_output/phiremock.extra.log
22+
debug: true
23+
start_delay: 1
24+
extra_instances:
25+
-
26+
listen: 0.0.0.0:18443
27+
debug: true
28+
expectations_path:
29+
certificate: tests/_data/certificate-cert.pem
30+
certificate_key: tests/_data/certificate-key.key
31+
logs_path: tests/_output/phiremock.secure.log
32+
suites:
33+
- acceptance

codeception.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@ paths:
66
support: tests/_support
77
envs: tests/_envs
88
settings:
9-
bootstrap: _bootstrap.php
109
colors: true
1110
memory_limit: 1024M
11+
bootstrap: _bootstrap.php
1212
extensions:
1313
enabled:
1414
- \Codeception\Extension\RunFailed
1515
- \Codeception\Extension\Phiremock
1616
config:
1717
\Codeception\Extension\Phiremock:
18-
listen: 0.0.0.0:18080
19-
start_delay: 1
2018
debug: true
21-
expectations_path:
22-
server_factory: Mcustiel\Phiremock\Codeception\Module\Tests\Helpers\FactoryWithGuzzle7
19+
start_delay: 1
20+
suites:
21+
- unit
22+
extra_instances:
23+
-
24+
listen: 0.0.0.0:18080
25+
debug: true
26+
expectations_path:
27+
server_factory: Mcustiel\Phiremock\Codeception\Module\Tests\Helpers\FactoryWithGuzzle7
28+
logs_path: tests/_output/phiremock.extra.log

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
],
2020
"autoload" : {
2121
"psr-4" : {
22-
"Codeception\\" : "src"
22+
"Codeception\\Extension\\" : "src/Extension",
23+
"Mcustiel\\Phiremock\\Codeception\\Extension\\": "src/PhiremockExtension"
2324
}
2425
},
2526
"autoload-dev": {
@@ -37,12 +38,12 @@
3738
"symfony/process": ">=3.0.0 <6.0.0"
3839
},
3940
"require-dev": {
40-
"mcustiel/phiremock-server": "^1.0",
41+
"mcustiel/phiremock-server": "dev-master",
4142
"codeception/module-rest": "^1.0",
4243
"codeception/module-phpbrowser": "^1.0",
4344
"guzzlehttp/guzzle" : "^7.0"
4445
},
45-
"suggests": {
46+
"suggest": {
4647
"mcustiel/phiremock-server": "As a dev-requirement. Allows the extension to start phiremock automatically from vendor/bin",
4748
"guzzlehttp/guzzle" : "Version 6 to execute phiremock-server as default"
4849
},

run-tests.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
3+
./vendor/bin/codecept run && \
4+
./vendor/bin/codecept -c codeception.https.yml run

src/Extension/Phiremock.php

Lines changed: 39 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -18,68 +18,53 @@
1818

1919
namespace Codeception\Extension;
2020

21-
use Codeception\Configuration as Config;
21+
use Codeception\Event\SuiteEvent;
2222
use Codeception\Extension as CodeceptionExtension;
23+
use Codeception\Suite;
24+
use Mcustiel\Phiremock\Codeception\Extension\Config;
25+
use Mcustiel\Phiremock\Codeception\Extension\PhiremockProcessManager;
2326

24-
/**
25-
* Codeception Extension for Phiremock.
26-
*/
2727
class Phiremock extends CodeceptionExtension
2828
{
29-
private const DEFAULT_PATH = 'vendor/bin/phiremock';
30-
private const DEFAULT_PORT = 8086;
31-
3229
/** @var array */
3330
public static $events = [
3431
'suite.before' => 'startProcess',
3532
'suite.after' => 'stopProcess',
3633
];
3734

3835
/** @var array */
39-
protected $config = [
40-
'listen' => '0.0.0.0:' . self::DEFAULT_PORT,
41-
'debug' => false,
42-
'start_delay' => 0,
43-
'bin_path' => self::DEFAULT_PATH,
44-
'expectations_path' => null,
45-
'server_factory' => 'default'
46-
];
36+
protected $config = Config::DEFAULT_CONFIG;
4737

48-
/** @var PhiremockProcess */
38+
/** @var PhiremockProcessManager */
4939
private $process;
5040

51-
/**
52-
* Class constructor.
53-
*
54-
* @param array $config
55-
* @param array $options
56-
* @param PhireMockProcess $process optional PhiremockProcess object
57-
*/
41+
/** @var Config */
42+
private $extensionConfig;
43+
5844
public function __construct(
5945
array $config,
6046
array $options,
61-
PhiremockProcess $process = null
47+
PhiremockProcessManager $process = null
6248
) {
6349
$this->setDefaultLogsPath();
6450
parent::__construct($config, $options);
65-
51+
$this->extensionConfig = new Config($this->config, $this->getOutputCallable());
6652
$this->initProcess($process);
6753
}
6854

69-
public function startProcess(): void
55+
public function startProcess(SuiteEvent $event): void
7056
{
71-
list($ip, $port) = explode(':', $this->config['listen']);
72-
73-
$this->writeln('Starting phiremock...');
74-
$this->process->start(
75-
$ip,
76-
empty($port) ? self::DEFAULT_PORT: (int) $port,
77-
$this->getPathFromCodeceptionDir($this->config['bin_path']),
78-
$this->getPathFromCodeceptionDir($this->config['logs_path']),
79-
$this->config['debug'],
80-
$this->config['expectations_path'] ? $this->getPathFromCodeceptionDir($this->config['expectations_path']) : null,
81-
$this->getFactoryClass()
82-
);
57+
$this->writeln('Starting default phiremock instance...');
58+
$suite = $event->getSuite();
59+
if ($this->mustRunForSuite($suite, $this->extensionConfig->getSuites())) {
60+
$this->process->start($this->extensionConfig);
61+
}
62+
foreach ($this->extensionConfig->getExtraInstances() as $configInstance) {
63+
if ($this->mustRunForSuite($suite, $configInstance->getSuites())) {
64+
$this->writeln('Starting extra phiremock instance...');
65+
$this->process->start($configInstance);
66+
}
67+
}
8368
$this->executeDelay();
8469
}
8570

@@ -89,44 +74,35 @@ public function stopProcess(): void
8974
$this->process->stop();
9075
}
9176

92-
private function getFactoryClass(): ?string
77+
public function getOutputCallable(): callable
9378
{
94-
if (isset($this->config['server_factory'])) {
95-
$factoryClassConfig = $this->config['server_factory'];
96-
if ($factoryClassConfig !== 'default') {
97-
return $this->config['server_factory'];
98-
}
99-
}
100-
return null;
79+
return function (string $message) {
80+
$this->writeln($message);
81+
};
10182
}
10283

103-
private function executeDelay(): void
84+
private function mustRunForSuite(Suite $suite, array $allowedSuites): bool
10485
{
105-
if (isset($this->config['startDelay'])) {
106-
$this->writeln('PHIREMOCK/DEPRECATION: startDelay option is deprecated and will be removed. Please use start_delay');
107-
$this->config['start_delay'] = $this->config['startDelay'];
108-
}
109-
110-
if ($this->config['start_delay']) {
111-
sleep($this->config['start_delay']);
112-
}
86+
return empty($allowedSuites) || in_array($suite->getBaseName(), $allowedSuites, true);
11387
}
11488

115-
private function initProcess(?PhiremockProcess $process): void
89+
private function executeDelay(): void
11690
{
117-
$this->process = $process ?? new PhiremockProcess();
91+
$delay = $this->extensionConfig->getDelay();
92+
if ($delay > 0) {
93+
sleep($delay);
94+
}
11895
}
11996

120-
private function setDefaultLogsPath(): void
97+
private function initProcess(?PhiremockProcessManager $process): void
12198
{
122-
$this->config['logs_path'] = Config::logDir();
99+
$this->process = $process ?? new PhiremockProcessManager($this->getOutputCallable());
123100
}
124101

125-
private function getPathFromCodeceptionDir($path): string
102+
private function setDefaultLogsPath(): void
126103
{
127-
if (substr($path, 0, 1) === '/') {
128-
return $path;
104+
if (!isset($this->config['logs_path'])) {
105+
$this->config['logs_path'] = Config::getDefaultLogsPath();
129106
}
130-
return realpath(Config::projectDir() . $path);
131107
}
132108
}

0 commit comments

Comments
 (0)