chg: remove wiremock, can be replaced with HttpClientTrait

pull/9489/head
Luciano Righetti 2024-01-18 10:13:26 +01:00
parent 277188a9de
commit f35d1a4fb5
6 changed files with 3 additions and 186 deletions

View File

@ -34,8 +34,7 @@
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.5",
"psy/psysh": "@stable",
"sirbrillig/phpcs-variable-analysis": "^2.11",
"wiremock-php/wiremock-php": "^2.33"
"sirbrillig/phpcs-variable-analysis": "^2.11"
},
"suggest": {
"markstory/asset_compress": "An asset compression plugin which provides file concatenation and a flexible filter system for preprocessing and minification.",
@ -69,9 +68,7 @@
"cs-fix": "phpcbf --colors --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/",
"stan": "phpstan analyse src/",
"test": [
"nohup sh ./tests/Helper/wiremock/start.sh >/dev/null 2>&1 &",
"phpunit",
"sh ./tests/Helper/wiremock/stop.sh"
"phpunit"
],
"migrate": [
"./bin/cake migrations migrate",
@ -88,4 +85,4 @@
"php-http/discovery": true
}
}
}
}

View File

@ -30,8 +30,6 @@
<php>
<ini name="memory_limit" value="-1" />
<ini name="apc.enable_cli" value="1" />
<env name="WIREMOCK_HOST" value="localhost" />
<env name="WIREMOCK_PORT" value="8080" />
<env name="OPENAPI_SPEC" value="webroot/docs/openapi.yaml" />
<env name="SKIP_DB_MIGRATIONS" value="0" />
</php>

View File

@ -1,89 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Test\Helper;
use \WireMock\Client\WireMock;
use \WireMock\Client\ValueMatchingStrategy;
use \WireMock\Client\RequestPatternBuilder;
use \WireMock\Stubbing\StubMapping;
trait WireMockTestTrait
{
/** @var WireMock */
private $wiremock;
/** @var array<mixed> */
private $config = [
'hostname' => 'localhost',
'port' => 8080
];
public function initializeWireMock(): void
{
$this->wiremock = WireMock::create(
$_ENV['WIREMOCK_HOST'] ?? $this->config['hostname'],
$_ENV['WIREMOCK_PORT'] ?? $this->config['port']
);
if (!$this->wiremock->isAlive()) {
throw new \Exception('Failed to connect to WireMock server.');
}
$this->clearWireMockStubs();
}
public function clearWireMockStubs(): void
{
$this->wiremock->resetToDefault();
}
public function getWireMock(): WireMock
{
return $this->wiremock;
}
public function getWireMockBaseUrl(): string
{
return sprintf('http://%s:%s', $this->config['hostname'], $this->config['port']);
}
/**
* Verify all WireMock stubs were called.
*
* @return void
*/
public function verifyAllStubsCalled(): void
{
$stubs = $this->wiremock->listAllStubMappings()->getMappings();
foreach ((array)$stubs as $stub) {
$this->verifyStubCalled($stub);
}
}
/**
* Verify the WireMock stub was called.
*
* @param StubMapping $stub
* @return void
*/
public function verifyStubCalled(StubMapping $stub): void
{
$validator = new RequestPatternBuilder($stub->getRequest()->getMethod(), $stub->getRequest()->getUrlMatchingStrategy());
// validate headers
$headers = $stub->getRequest()->getHeaders();
if (is_array($headers)) {
foreach ($headers as $header => $rule) {
$validator = $validator->withHeader($header, $rule);
}
}
// TODO: Add body matching
// TODO: Add query matching
// TODO: Add cookie matching
$this->wiremock->verify($validator);
}
}

View File

@ -1,38 +0,0 @@
#!/bin/bash
# Adapted from @rowanhill wiremock start.sh script
# https://github.com/rowanhill/wiremock-php/blob/master/wiremock/start.sh
cd ./tmp/
instance=1
port=8080
if [ $# -gt 0 ]; then
instance=$1
port=$2
fi
pidFile=wiremock.$instance.pid
logFile=wiremock.$instance.log
# Ensure WireMock isn't already running
if [ -e $pidFile ]; then
echo WireMock is already started: see process `cat $pidFile` 1>&2
exit 0
fi
# Download the wiremock jar if we need it
if ! [ -e wiremock-standalone.jar ]; then
echo WireMock standalone JAR missing. Downloading.
curl https://repo1.maven.org/maven2/com/github/tomakehurst/wiremock-jre8-standalone/2.32.0/wiremock-jre8-standalone-2.32.0.jar -o wiremock-standalone.jar
status=$?
if [ ${status} -ne 0 ]; then
echo curl could not download WireMock JAR 1>&2
exit ${status}
fi
fi
# Start WireMock in standalone mode (in a background process) and save its output to a log
java -jar wiremock-standalone.jar --port $port --root-dir $instance --disable-banner &> $logFile 2>&1 &
pgrep -f wiremock-standalone.jar > $pidFile
echo WireMock $instance started on port $port

View File

@ -1,22 +0,0 @@
#!/bin/bash
# Adapted from @rowanhill wiremock stop.sh script
# https://github.com/rowanhill/wiremock-php/blob/master/wiremock/stop.sh
cd ./tmp/
instance=1
if [ $# -gt 0 ]; then
instance=$1
fi
pidFile=wiremock.$instance.pid
if [ -e $pidFile ]; then
kill -9 `cat $pidFile`
rm $pidFile
else
echo WireMock is not started 2>&1
fi
echo WireMock $instance stopped

View File

@ -38,18 +38,9 @@ return [
```
$ composer install
$ composer test
> sh ./tests/Helper/wiremock/start.sh
WireMock 1 started on port 8080
> phpunit
[ * ] Running DB migrations, it may take some time ...
The WireMock server is started .....
port: 8080
enable-browser-proxying: false
disable-banner: true
no-request-journal: false
verbose: false
PHPUnit 8.5.22 by Sebastian Bergmann and contributors.
@ -78,26 +69,6 @@ By default the database is re-generated before running the test suite, to skip t
</php>
```
## Extras
### WireMock
Some integration tests perform calls to external APIs, we use WireMock to mock the response of these API calls.
To download and run WireMock run the following script in a separate terminal:
```
sh ./tests/Helper/wiremock/start.sh
```
You can also run WireMock with docker, check the official docs: http://wiremock.org/docs/docker/
> NOTE: When running the tests with `composer test` WireMock is automatically started and stoped after the tests finish.
The default `hostname` and `port` for WireMock are set in `phpunit.xml` as environment variables:
```xml
<php>
...
<env name="WIREMOCK_HOST" value="localhost" />
<env name="WIREMOCK_PORT" value="8080" />
</php>
```
### Coverage
HTML:
```