Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Tests

on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

jobs:
tests:
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
php: [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 }}
extensions: json, mbstring
coverage: none

- name: Validate composer.json
run: composer validate --strict

- name: Get Composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache Composer dependencies
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: composer install --prefer-dist --no-interaction --no-progress

- name: Run tests
run: composer test
25 changes: 20 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,36 @@
"description": "An unofficial PHP wrapper for the official Claim.MD API.",
"type": "library",
"license": "MIT",
"version": "1.0.1",
"require": {
"php": ">=8.2",
"guzzlehttp/guzzle": "^7.9",
"ext-json": "*"
},
"require-dev": {
"mockery/mockery": "^1.6",
"pestphp/pest": "^3.0"
},
"autoload": {
"psr-4": {
"Nextvisit\\ClaimMDWrapper\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Nextvisit\\ClaimMDWrapper\\Tests\\": "tests/"
}
},
"authors": [
{
"name": "Kyle Yannelli",
"github": "kyleyannelli",
"email": "kyleyannelli@gmail.com",
"homepage": "https://github.com/kyleyannelli",
"role": "Developer"
},
{
"name": "Ryan Yannelli",
"github": "yannelli",
"email": "ryanyannelli@gmail.com",
"homepage": "https://github.com/yannelli",
"role": "Developer"
}
],
Expand All @@ -40,7 +48,14 @@
"issues": "https://github.com/Nextvisit/claim-md-php/issues"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"scripts": {
"test": "pest",
"test:coverage": "pest --coverage"
},
"minimum-stability": "stable"
}
}
18 changes: 18 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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"
cacheDirectory=".phpunit.cache"
>
<testsuites>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
2 changes: 1 addition & 1 deletion src/Requests/ResponseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(private readonly Client $client) {}
*/
public function fetchResponses(string $responseId, ?string $claimId = null): array
{
if (empty($responseId)) {
if ($responseId === '') {
throw new InvalidArgumentException('ResponseID cannot be empty');
}

Expand Down
36 changes: 36 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "pest()" function to bind a different classes or traits.
|
*/

pest()->extend(Nextvisit\ClaimMDWrapper\Tests\TestCase::class)->in('Unit');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Nextvisit\ClaimMDWrapper\Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
//
}
117 changes: 117 additions & 0 deletions tests/Unit/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Middleware;
use Nextvisit\ClaimMDWrapper\Client;
use Nextvisit\ClaimMDWrapper\Config;

describe('Client', function () {
it('creates a client with account key and config', function () {
$client = new Client('test-account-key');

expect($client)->toBeInstanceOf(Client::class);
});

it('creates a client with custom config', function () {
$config = new Config();
$client = new Client('test-account-key', $config);

expect($client)->toBeInstanceOf(Client::class);
});

it('sends a POST request with account key in form data', function () {
$container = [];
$history = Middleware::history($container);

$mock = new MockHandler([
new Response(200, [], json_encode(['status' => 'success'])),
]);

$handlerStack = HandlerStack::create($mock);
$handlerStack->push($history);

$guzzleClient = new GuzzleClient(['handler' => $handlerStack]);
$client = new Client('test-account-key', new Config(), $guzzleClient);

$result = $client->sendRequest('POST', '/test-endpoint', ['data' => 'value']);

expect($result)->toBe(['status' => 'success']);
expect($container)->toHaveCount(1);

$request = $container[0]['request'];
expect($request->getMethod())->toBe('POST');
expect((string) $request->getUri())->toBe('/test-endpoint');

$body = (string) $request->getBody();
expect($body)->toContain('AccountKey=test-account-key');
expect($body)->toContain('data=value');
});

it('sends a multipart request when isMultipart is true', function () {
$container = [];
$history = Middleware::history($container);

$mock = new MockHandler([
new Response(200, [], json_encode(['uploaded' => true])),
]);

$handlerStack = HandlerStack::create($mock);
$handlerStack->push($history);

$guzzleClient = new GuzzleClient(['handler' => $handlerStack]);
$client = new Client('test-account-key', new Config(), $guzzleClient);

$result = $client->sendRequest('POST', '/upload', ['file' => 'content'], true);

expect($result)->toBe(['uploaded' => true]);

$request = $container[0]['request'];
$contentType = $request->getHeaderLine('Content-Type');
expect($contentType)->toContain('multipart/form-data');
});

it('includes additional headers when provided', function () {
$container = [];
$history = Middleware::history($container);

$mock = new MockHandler([
new Response(200, [], json_encode(['data' => 'test'])),
]);

$handlerStack = HandlerStack::create($mock);
$handlerStack->push($history);

$guzzleClient = new GuzzleClient(['handler' => $handlerStack]);
$client = new Client('test-account-key', new Config(), $guzzleClient);

$result = $client->sendRequest('POST', '/test', [], false, ['X-Custom-Header' => 'custom-value']);

$request = $container[0]['request'];
expect($request->getHeaderLine('X-Custom-Header'))->toBe('custom-value');
});

it('sends request without extra data', function () {
$container = [];
$history = Middleware::history($container);

$mock = new MockHandler([
new Response(200, [], json_encode(['status' => 'ok'])),
]);

$handlerStack = HandlerStack::create($mock);
$handlerStack->push($history);

$guzzleClient = new GuzzleClient(['handler' => $handlerStack]);
$client = new Client('test-account-key', new Config(), $guzzleClient);

$result = $client->sendRequest('POST', '/simple');

expect($result)->toBe(['status' => 'ok']);

$body = (string) $container[0]['request']->getBody();
expect($body)->toContain('AccountKey=test-account-key');
});
});
17 changes: 17 additions & 0 deletions tests/Unit/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Nextvisit\ClaimMDWrapper\Config;

describe('Config', function () {
it('returns the correct base URI', function () {
$config = new Config();

expect($config->getBaseUri())->toBe('https://svc.claim.md/');
});

it('returns a string for base URI', function () {
$config = new Config();

expect($config->getBaseUri())->toBeString();
});
});
Loading