Skip to content

Commit

Permalink
refactor!: Move csv to core
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Jan 1, 2024
1 parent 00ba598 commit 6616cd5
Show file tree
Hide file tree
Showing 20 changed files with 450 additions and 37 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@
"MatchersProvider\\Tests\\": "example/matchers/provider/tests",
"GeneratorsConsumer\\": "example/generators/consumer/src",
"GeneratorsConsumer\\Tests\\": "example/generators/consumer/tests",
"GeneratorsProvider\\Tests\\": "example/generators/provider/tests"
"GeneratorsProvider\\Tests\\": "example/generators/provider/tests",
"CsvConsumer\\": "example/csv/consumer/src",
"CsvConsumer\\Tests\\": "example/csv/consumer/tests",
"CsvProvider\\": "example/csv/provider/src",
"CsvProvider\\Tests\\": "example/csv/provider/tests"
}
},
"scripts": {
Expand Down
11 changes: 11 additions & 0 deletions example/csv/consumer/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="../../../vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="PhpPact Example Tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="PACT_LOGLEVEL" value="DEBUG"/>
</php>
</phpunit>
28 changes: 28 additions & 0 deletions example/csv/consumer/src/Service/HttpClientService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace CsvConsumer\Service;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Uri;

class HttpClientService
{
private Client $httpClient;

private string $baseUri;

public function __construct(string $baseUri)
{
$this->httpClient = new Client();
$this->baseUri = $baseUri;
}

public function getReport(): array
{
$response = $this->httpClient->get(new Uri("{$this->baseUri}/report.csv"), [
'headers' => ['Accept' => 'text/csv']
]);

return str_getcsv($response->getBody());
}
}
63 changes: 63 additions & 0 deletions example/csv/consumer/tests/Service/HttpClientServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace CsvConsumer\Tests\Service;

use CsvConsumer\Service\HttpClientService;
use PhpPact\Consumer\InteractionBuilder;
use PhpPact\Consumer\Model\Body\Text;
use PhpPact\Consumer\Model\ConsumerRequest;
use PhpPact\Consumer\Model\ProviderResponse;
use PhpPact\Plugins\Csv\Factory\CsvInteractionDriverFactory;
use PhpPact\Standalone\MockService\MockServerConfig;
use PHPUnit\Framework\TestCase;

class HttpClientServiceTest extends TestCase
{
public function testGetCsvFile(): void
{
$request = new ConsumerRequest();
$request
->setMethod('GET')
->setPath('/report.csv')
->addHeader('Accept', 'text/csv')
;

$response = new ProviderResponse();
$response
->setStatus(200)
->setBody(new Text(
json_encode([
'csvHeaders' => false,
'column:1' => "matching(type,'Name')",
'column:2' => 'matching(number,100)',
'column:3' => "matching(datetime, 'yyyy-MM-dd','2000-01-01')",
]),
'text/csv'
))
;

$config = new MockServerConfig();
$config
->setConsumer('csvConsumer')
->setProvider('csvProvider')
->setPactSpecificationVersion('4.0.0')
->setPactDir(__DIR__.'/../../../pacts');
if ($logLevel = \getenv('PACT_LOGLEVEL')) {
$config->setLogLevel($logLevel);
}
$builder = new InteractionBuilder($config, new CsvInteractionDriverFactory());
$builder
->given('report.csv file exist')
->uponReceiving('request for a report.csv')
->with($request)
->willRespondWith($response)
;

$service = new HttpClientService($config->getBaseUri());
$columns = $service->getReport();

$this->assertTrue($builder->verify());
$this->assertCount(3, $columns);
$this->assertSame(['Name', '100', '2000-01-01'], $columns);
}
}
108 changes: 108 additions & 0 deletions example/csv/pacts/csvConsumer-csvProvider.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"consumer": {
"name": "csvConsumer"
},
"interactions": [
{
"description": "request for a report.csv",
"interactionMarkup": {
"markup": "# Data\n\n|Name|100|2000-01-01|\n",
"markupType": "COMMON_MARK"
},
"pending": false,
"pluginConfiguration": {
"csv": {
"csvHeaders": false
}
},
"providerStates": [
{
"name": "report.csv file exist"
}
],
"request": {
"headers": {
"Accept": [
"text/csv"
]
},
"method": "GET",
"path": "/report.csv"
},
"response": {
"body": {
"content": "Name,100,2000-01-01\n",
"contentType": "text/csv;charset=utf-8",
"contentTypeHint": "DEFAULT",
"encoded": false
},
"generators": {
"body": {
"column:3": {
"format": "yyyy-MM-dd",
"type": "DateTime"
}
}
},
"headers": {
"content-type": [
"text/csv"
]
},
"matchingRules": {
"body": {
"column:1": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
},
"column:2": {
"combine": "AND",
"matchers": [
{
"match": "number"
}
]
},
"column:3": {
"combine": "AND",
"matchers": [
{
"format": "yyyy-MM-dd",
"match": "datetime"
}
]
}
},
"status": {}
},
"status": 200
},
"transport": "http",
"type": "Synchronous/HTTP"
}
],
"metadata": {
"pactRust": {
"ffi": "0.4.11",
"mockserver": "1.2.4",
"models": "1.1.12"
},
"pactSpecification": {
"version": "4.0"
},
"plugins": [
{
"configuration": {},
"name": "csv",
"version": "0.0.3"
}
]
},
"provider": {
"name": "csvProvider"
}
}
11 changes: 11 additions & 0 deletions example/csv/provider/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="../../../vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="PhpPact Example Tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="PACT_LOGLEVEL" value="DEBUG"/>
</php>
</phpunit>
15 changes: 15 additions & 0 deletions example/csv/provider/public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

require __DIR__.'/../../../../vendor/autoload.php';

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

$app = AppFactory::create();

$app->post('/pact-change-state', function (Request $request, Response $response) {
return $response;
});

$app->run();
11 changes: 11 additions & 0 deletions example/csv/provider/public/report.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"Mack Greenholt Jr.",943,2003-02-05
"Luigi Ruecker",654,1980-09-11
"Mr. Unique Zieme",79,2020-04-30
"Leif Price",367,2013-04-07
"Destiny Rodriguez",995,1983-03-20
"Lavinia Carroll",344,2010-08-01
"Reta Schoen",113,2001-01-26
"Mrs. Carolina Swift",494,1982-01-20
"Gene Crona PhD",852,1992-11-16
"Dr. Santino Koepp",79,1984-05-05
"Luigi Sanford",969,1995-07-02
53 changes: 53 additions & 0 deletions example/csv/provider/tests/PactVerifyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace CsvProvider\Tests;

use GuzzleHttp\Psr7\Uri;
use PhpPact\Standalone\ProviderVerifier\Model\VerifierConfig;
use PhpPact\Standalone\ProviderVerifier\Verifier;
use PhpPactTest\Helper\ProviderProcess;
use PHPUnit\Framework\TestCase;

class PactVerifyTest extends TestCase
{
private ProviderProcess $process;

/**
* Run the PHP build-in web server.
*/
protected function setUp(): void
{
$this->process = new ProviderProcess(__DIR__ . '/../public/');
$this->process->start();
}

/**
* Stop the web server process once complete.
*/
protected function tearDown(): void
{
$this->process->stop();
}

public function testPactVerifyConsumer(): void
{
$config = new VerifierConfig();
$config->getProviderInfo()
->setName('csvProvider')
->setHost('localhost')
->setPort(7202);
$config->getProviderState()
->setStateChangeUrl(new Uri('http://localhost:7202/pact-change-state'))
;
if ($logLevel = \getenv('PACT_LOGLEVEL')) {
$config->setLogLevel($logLevel);
}

$verifier = new Verifier($config);
$verifier->addFile(__DIR__ . '/../../pacts/csvConsumer-csvProvider.json');

$verifyResult = $verifier->verify();

$this->assertTrue($verifyResult);
}
}
2 changes: 1 addition & 1 deletion example/xml/provider/tests/PactVerifyTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace XmlConsumer\Tests;
namespace XmlProvider\Tests;

use GuzzleHttp\Psr7\Uri;
use PhpPact\Standalone\ProviderVerifier\Model\VerifierConfig;
Expand Down
6 changes: 6 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
<testsuite name="PhpPact Generators Provider Example Tests">
<directory>./example/generators/provider/tests</directory>
</testsuite>
<testsuite name="PhpPact Csv Consumer Example Tests">
<directory>./example/csv/consumer/tests</directory>
</testsuite>
<testsuite name="PhpPact Csv Provider Example Tests">
<directory>./example/csv/provider/tests</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="./test_results/reports/unit_test_results.xml"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace PhpPact\SyncMessage\Driver\Pact;
namespace PhpPact\Plugin\Driver\Pact;

use PhpPact\Consumer\Driver\Pact\PactDriver;
use PhpPact\SyncMessage\Exception\PluginNotSupportedBySpecificationException;
use PhpPact\Plugin\Exception\PluginNotSupportedBySpecificationException;

abstract class AbstractPluginPactDriver extends PactDriver
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace PhpPact\SyncMessage\Exception;
namespace PhpPact\Plugin\Exception;

use Exception;

class InteractionContentNotAddedException extends Exception
class PluginBodyNotAddedException extends Exception
{
public function __construct(int $code)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace PhpPact\SyncMessage\Exception;
namespace PhpPact\Plugin\Exception;

use Exception;

Expand Down
Loading

0 comments on commit 6616cd5

Please sign in to comment.