Skip to content
This repository has been archived by the owner on Jun 2, 2020. It is now read-only.

Commit

Permalink
Merge pull request #26 from klarna/integration_tests
Browse files Browse the repository at this point in the history
Add Checkout API integration tests.
  • Loading branch information
alexions authored Mar 15, 2019
2 parents c3400ad + 8e3c40e commit 568ced8
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/examples/CheckoutAPI/update_checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@
try {
$checkout = new Klarna\Rest\Checkout\Order($connector, $orderId);
$checkout->update($updatedOrder);

// Get some data if needed
echo <<<ORDER
Order has been successfully updated
OrderID: $checkout[order_id]
New Order Amount: $checkout[order_amount]
New Order Tax: $checkout[order_tax_amount]
ORDER;

} catch (Exception $e) {
echo 'Caught exception: ' . $e->getMessage() . "\n";
}
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<testsuite name="Component Tests">
<directory>tests/Component</directory>
</testsuite>

<testsuite name="Integration Tests">
<directory>tests/Integration</directory>
</testsuite>
</testsuites>

<filter>
Expand Down
123 changes: 123 additions & 0 deletions tests/Integration/Checkout/OrderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* Copyright 2019 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Klarna\Tests\Integration\Checkout;

use Klarna\Rest\Tests\Integration\TestCase;
use Klarna\Rest\Transport\Connector;
use Klarna\Rest\Transport\Exception\ConnectorException;

/**
* Integration test cases for the checkout order resource.
*/
class OrderTest extends TestCase
{
public function testCreateCheckout()
{
if (!$this->hasCredentials()) {
return $this->markTestSkipped('No credentials provided');
}

$output = $this->execFile($this->rootPath . '/docs/examples/CheckoutAPI/create_checkout.php');

$this->assertFalse($this->hasException($output));
$this->assertTrue($this->isTextPresents('OrderID:[ a-zA-Z0-9-]+', $output), 'No OrderID found');
$this->assertTrue($this->isTextPresents('Order status:[ a-z]+', $output), 'No Order Status found');
}

public function testFetchNonExisting()
{
if (!$this->hasCredentials()) {
return $this->markTestSkipped('No credentials provided');
}
putenv('ORDER_ID=12345');
$output = $this->execFile($this->rootPath . '/docs/examples/CheckoutAPI/fetch_checkout.php');
$this->assertTrue($this->hasException($output));
$this->assertTrue($this->isTextPresents('404 Not Found', $output));
}

public function testCreateAndFetch()
{
if (!$this->hasCredentials()) {
return $this->markTestSkipped('No credentials provided');
}

$output = $this->execFile($this->rootPath . '/docs/examples/CheckoutAPI/create_checkout.php');
$this->assertTrue($this->isTextPresents('OrderID:[ a-zA-Z0-9-]+', $output), 'No OrderID found');

preg_match('/OrderID: ([a-zA-Z0-9-]+)/ims', $output, $matches);
$orderId = $matches[1];

putenv('ORDER_ID=' . $orderId);
$output = $this->execFile($this->rootPath . '/docs/examples/CheckoutAPI/fetch_checkout.php');
$this->assertFalse($this->hasException($output));
$this->assertTrue($this->isTextPresents('OrderID:[ a-zA-Z0-9-]+', $output), 'No OrderID found');
$this->assertTrue($this->isTextPresents('Order status:[ a-z]+', $output), 'No Order Status found');
}

public function testCreateAndUpdate()
{
if (!$this->hasCredentials()) {
return $this->markTestSkipped('No credentials provided');
}

$output = $this->execFile($this->rootPath . '/docs/examples/CheckoutAPI/create_checkout.php');
$this->assertTrue($this->isTextPresents('OrderID:[ a-zA-Z0-9-]+', $output), 'No OrderID found');

preg_match('/OrderID: ([a-zA-Z0-9-]+)/ims', $output, $matches);
$orderId = $matches[1];

putenv('ORDER_ID=' . $orderId);
$output = $this->execFile($this->rootPath . '/docs/examples/CheckoutAPI/update_checkout.php');
$this->assertFalse($this->hasException($output));
$this->assertTrue($this->isTextPresents('Order has been successfully updated', $output));
$this->assertTrue($this->isTextPresents('New Order Amount: 11000', $output));
$this->assertTrue($this->isTextPresents('New Order Tax: 2200', $output));
}

public function testHandlingExceptions()
{
if (!$this->hasCredentials()) {
return $this->markTestSkipped('No credentials provided');
}

$output = $this->execFile($this->rootPath . '/docs/examples/CheckoutAPI/create_checkout.php');
$this->assertTrue($this->isTextPresents('OrderID:[ a-zA-Z0-9-]+', $output), 'No OrderID found');

preg_match('/OrderID: ([a-zA-Z0-9-]+)/ims', $output, $matches);
$orderId = $matches[1];

putenv('ORDER_ID=' . $orderId);
$output = $this->execFile($this->rootPath . '/docs/examples/CheckoutAPI/handling_exceptions.php');

$this->assertTrue($this->isTextPresents('BAD_REQUEST', $output));
$this->assertTrue($this->isTextPresents('Code: 400', $output));
$this->assertTrue($this->isTextPresents('CorrelationID:[ a-zA-Z0-9-]+', $output));
}

public function testHandlingExceptionsUnauthorized()
{
if (!$this->hasCredentials()) {
return $this->markTestSkipped('No credentials provided');
}

putenv('PASSWORD=wrong_password');
$output = $this->execFile($this->rootPath . '/docs/examples/CheckoutAPI/handling_exceptions.php');

$this->assertTrue($this->isTextPresents('401 Unauthorized', $output));
}
}
76 changes: 76 additions & 0 deletions tests/Integration/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright 2019 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the TestCase class.
*/

namespace Klarna\Rest\Tests\Integration;

/**
* Base unit test case class.
*/
class TestCase extends \PHPUnit\Framework\TestCase
{
protected $rootPath;
protected $credentials = [];

protected function hasCredentials()
{
return !empty($this->credentials);
}

/**
* Sets up the test fixtures.
*/
protected function setUp()
{
$this->rootPath = dirname(dirname(__DIR__));
$path = getenv('CREDENTIALS');
if ($path === false) {
$path = $this->rootPath . '/credentials.json';
}

if (file_exists($path)) {
$content = file_get_contents($path);
$this->credentials = json_decode($content);

foreach ($this->credentials as $field => $value) {
$field = strtoupper($field);
putenv("${field}=${value}");
}
}
}

protected function execFile($path)
{
ob_start();
include $path;
$value = ob_get_contents();
ob_end_clean();

return $value;
}

protected function hasException($output)
{
return preg_match('/Caught exception/ims', $output) === 1;
}

protected function isTextPresents($pattern, $output)
{
return preg_match("/${pattern}/ims", $output) === 1;
}
}

0 comments on commit 568ced8

Please sign in to comment.