Skip to content

Commit

Permalink
Add setup for integration tests and other fixes (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
briwa committed Apr 14, 2023
1 parent 1f36b82 commit 55c2d0d
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ composer:
dependencies:
make -s composer update -- --prefer-dist
test:
$(RUNNER) "phpunit --coverage-text --coverage-html ./coverage"
$(RUNNER) "XDEBUG_MODE=coverage phpunit --coverage-text --coverage-html ./coverage"
phpunit:
$(RUNNER) "phpunit $(filter-out $@,$(MAKECMDGOALS))"
php:
Expand Down
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
},
"autoload-dev": {
"psr-4": {
"ChartMogul\\Tests\\": "tests/Unit"
"ChartMogul\\Tests\\": "tests/Unit",
"ChartMogul\\IntegrationTests\\": "tests/Integration"
}
},
"require": {
Expand All @@ -34,6 +35,13 @@
"require-dev": {
"php-http/mock-client": "^1.5",
"guzzlehttp/psr7": "^1.0",
"http-interop/http-factory-guzzle": "^1.0"
"http-interop/http-factory-guzzle": "^1.0",
"php-vcr/php-vcr": "^1.5",
"php-http/curl-client": "^2.2"
},
"config": {
"allow-plugins": {
"php-http/discovery": false
}
}
}
8 changes: 4 additions & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" stderr="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" stopOnFailure="false" verbose="true" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</whitelist>
</filter>
<testsuites>
<testsuite name="ChartMogul PHP SDK Test Suite">
<directory suffix="Test.php">./tests</directory>
Expand Down
24 changes: 24 additions & 0 deletions tests/Fixtures/IntegrationExampleTest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

-
request:
method: GET
url: 'https://api.chartmogul.com/v1/ping'
headers:
Host: api.chartmogul.com
Authorization: hidden
content-type: application/json
user-agent: chartmogul-php/5.0.1/PHP-7.4
Expect: ''
response:
status:
http_version: '1.1'
code: '200'
message: OK
headers:
Access-Control-Allow-Credentials: 'true'
Content-Type: application/json
Date: 'Wed, 12 Apr 2023 07:13:41 GMT'
Content-Length: '16'
Connection: keep-alive
body: '{"data":"pong!"}'
index: 0
23 changes: 23 additions & 0 deletions tests/Integration/IntegrationExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace ChartMogul\IntegrationTests;

use ChartMogul;
use VCR\VCR;

class IntegrationExampleTest extends IntegrationTestCase
{
public function testIntegrationExample()
{
VCR::insertCassette('IntegrationExampleTest.yaml');

/*
* When writing the test, set this to your own API key.
* Once all fixtures are recorded, remove the line from your test
* and remove all references to your own API key in the fixtures.
*/
// $this->setApiKey('<YOUR_API_KEY>');

$response = ChartMogul\Ping::ping($this->client);
$this->assertEquals("pong!", $response->data);
}
}
32 changes: 32 additions & 0 deletions tests/Integration/IntegrationTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace ChartMogul\IntegrationTests;

use ChartMogul\Configuration;
use ChartMogul\Http\Client;
use VCR\VCR;

class IntegrationTestCase extends \PHPUnit\Framework\TestCase
{
public $client = null;

public function setUp(): void
{
VCR::configure()
->enableLibraryHooks(['curl'])
->setCassettePath('tests/Fixtures')
->enableRequestMatchers(['method', 'url', 'body', 'query_string']);
VCR::turnOn();
}

public function tearDown(): void
{
VCR::eject();
VCR::turnOff();
}

public function setApiKey($apiKey)
{
$this->client = new Client(new Configuration($apiKey));
return $this;
}
}

0 comments on commit 55c2d0d

Please sign in to comment.