Skip to content

Commit

Permalink
Merge pull request #15 from mnastalski/tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
mnastalski authored Jul 30, 2024
2 parents b1301fd + b9c3cb9 commit bd91e5f
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 1 deletion.
39 changes: 39 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: tests

on:
pull_request:
push:
branches:
- master
- '*.x'

jobs:
tests:
runs-on: ubuntu-22.04

strategy:
matrix:
php: [7.1, 7.2, 7.3, 7.4, 8.0, 8.1, 8.2, 8.3]

name: PHP ${{ matrix.php }}

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: none

- name: Install composer dependencies
uses: nick-invision/retry@v3
with:
timeout_minutes: 5
max_attempts: 5
command: composer install --no-interaction --no-progress

- name: Execute tests
run: vendor/bin/phpunit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/vendor

# Files
.phpunit.result.cache
composer.lock
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"guzzlehttp/guzzle": "^6.4|^7.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
"phpunit/phpunit": "^7.0|^8.0"
},
"autoload": {
"psr-4": {
Expand Down
13 changes: 13 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<php>
</php>
</phpunit>
17 changes: 17 additions & 0 deletions tests/Unit/Api/Response/BaselinkerExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Api\Response;

use Baselinker\Api\Response\BaselinkerException;
use PHPUnit\Framework\TestCase;

class BaselinkerExceptionTest extends TestCase
{
public function testGetters(): void
{
$exception = new BaselinkerException('Message', 'CODE');

$this->assertEquals('Message', $exception->responseMessage());
$this->assertEquals('CODE', $exception->responseCode());
}
}
54 changes: 54 additions & 0 deletions tests/Unit/Api/Response/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Api\Response;

use Baselinker\Api\Response\BaselinkerException;
use Baselinker\Api\Response\Response;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use PHPUnit\Framework\TestCase;

class ResponseTest extends TestCase
{
public function testGetters(): void
{
$body = json_encode([
'status' => 'SUCCESS',
'list' => [
'foo' => 'bar',
],
]);

$response = new Response(
new GuzzleResponse(200, [], $body)
);

$this->assertEquals('{"status":"SUCCESS","list":{"foo":"bar"}}', $response->contents());

$this->assertEquals([
'status' => 'SUCCESS',
'list' => [
'foo' => 'bar',
],
], $response->toArray());

$this->assertEquals([
'foo' => 'bar',
], $response->getParameter('list'));
}

public function testWithErrorThrowsException(): void
{
$body = json_encode([
'status' => 'ERROR',
'error_code' => 'CODE',
'error_message' => 'Message',
]);

$this->expectException(BaselinkerException::class);
$this->expectExceptionMessage('[CODE] Message');

new Response(
new GuzzleResponse(200, [], $body)
);
}
}
69 changes: 69 additions & 0 deletions tests/Unit/BaselinkerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Baselinker\Tests\Unit;

use Baselinker\Api\Request\CourierShipmentsInterface;
use Baselinker\Api\Request\ExternalStoragesInterface;
use Baselinker\Api\Request\OrderReturnsInterface;
use Baselinker\Api\Request\OrdersInterface;
use Baselinker\Api\Request\ProductCatalogInterface;
use Baselinker\Baselinker;
use PHPUnit\Framework\TestCase;

class BaselinkerTest extends TestCase
{
public function testProductCatalog(): void
{
$baselinker = new Baselinker([
'token' => 'foo',
]);

$productCatalog = $baselinker->productCatalog();

$this->assertInstanceOf(ProductCatalogInterface::class, $productCatalog);
}

public function testExternalStorages(): void
{
$baselinker = new Baselinker([
'token' => 'foo',
]);

$externalStorages = $baselinker->externalStorages();

$this->assertInstanceOf(ExternalStoragesInterface::class, $externalStorages);
}

public function testOrders(): void
{
$baselinker = new Baselinker([
'token' => 'foo',
]);

$orders = $baselinker->orders();

$this->assertInstanceOf(OrdersInterface::class, $orders);
}

public function testOrderReturns(): void
{
$baselinker = new Baselinker([
'token' => 'foo',
]);

$orderReturns = $baselinker->orderReturns();

$this->assertInstanceOf(OrderReturnsInterface::class, $orderReturns);
}

public function testCourierShipments(): void
{
$baselinker = new Baselinker([
'token' => 'foo',
]);

$courierShipments = $baselinker->courierShipments();

$this->assertInstanceOf(CourierShipmentsInterface::class, $courierShipments);
}
}
50 changes: 50 additions & 0 deletions tests/Unit/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Baselinker\Tests\Unit;

use Baselinker\Config;
use InvalidArgumentException;
use Iterator;
use PHPUnit\Framework\TestCase;

class ConfigTest extends TestCase
{
public function testGetToken(): void
{
$config = new Config([
'token' => 'foo',
]);

$this->assertEquals('foo', $config->getToken());
}

/**
* @dataProvider invalidConfigDataProvider
*/
public function testSetWithInvalidTokenThrowsException(array $config): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Parameter "token" must be provided in the configuration.');

new Config($config);
}

public static function invalidConfigDataProvider(): Iterator
{
yield 'empty config' => [
[],
];

yield 'null as token' => [
[
'token' => null,
]
];

yield 'empty string as token' => [
[
'token' => '',
]
];
}
}

0 comments on commit bd91e5f

Please sign in to comment.