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
44 changes: 44 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: PHPUnit

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

jobs:
test:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5' ]

name: PHP ${{ matrix.php }}

steps:
- uses: actions/checkout@v6

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

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

- name: Run PHPUnit
run: vendor/bin/phpunit --coverage-text=coverage.txt

- name: Check coverage
run: |
cat coverage.txt
COVERAGE=$(grep -A3 'Summary:' coverage.txt | grep 'Lines:' | grep -oP '\d+\.\d+(?=%)')
THRESHOLD=100
if (( $(echo "$COVERAGE < $THRESHOLD" | bc -l) )); then
echo "Coverage is $COVERAGE%, below ${THRESHOLD}% threshold"
exit 1
fi
echo "Coverage is $COVERAGE% (threshold: ${THRESHOLD}%)"
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"packaged/helpers": "~1.0||~2.0"
},
"require-dev": {
"phpunit/phpunit": "~4.5"
"phpunit/phpunit": "~9"
},
"autoload": {
"psr-4": {
Expand Down
22 changes: 9 additions & 13 deletions phpunit.xml.dist → phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
colors="true"
processIsolation="false"
stopOnFailure="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true"
syntaxCheck="true"
bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Fortifi Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<coverage>
<include>
<directory suffix=".php">src</directory>
</whitelist>
<blacklist>
<directory suffix=".php">vendor</directory>
<directory suffix=".php">tests</directory>
</blacklist>
</filter>
</include>
</coverage>
</phpunit>
1 change: 1 addition & 0 deletions src/Payloads/FortifiWebhookPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ final public function __construct($event)
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return [
Expand Down
2 changes: 1 addition & 1 deletion src/Webhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function getPayload($json)
$decoded = json_decode($json);
$events = static::all();
$event = Objects::property($decoded, 'event');
if(isset($events[$event]))
if($event !== null && isset($events[$event]))
{
return call_user_func([$events[$event], 'hydrateFromJson'], $json);
}
Expand Down
35 changes: 34 additions & 1 deletion tests/Payloads/FortifiWebhookPayloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
namespace Fortifi\Tests\Webhooks\Payloads;

use Fortifi\Webhooks\Payloads\FortifiWebhookPayload;
use Fortifi\Webhooks\Payloads\Test\TestWHP;
use PHPUnit\Framework\TestCase;

class FortifiWebhookPayloadTest extends \PHPUnit_Framework_TestCase
class FortifiWebhookPayloadTest extends TestCase
{
public function testWebookPayload()
{
Expand All @@ -25,4 +27,35 @@ public function testWebookPayload()
$this->assertEquals(456, $object->getRequestId());
}
}

/**
* Test that jsonSerialize works without deprecation in PHP 8+
* Requires #[\ReturnTypeWillChange] attribute on jsonSerialize()
*/
public function testJsonSerializeCompatibility()
{
$deprecation = null;
set_error_handler(
function($errno, $errstr) use (&$deprecation) {
if(strpos($errstr, 'jsonSerialize') !== false)
{
$deprecation = $errstr;
}
return true;
},
E_DEPRECATED
);

try
{
$payload = new TestWHP('test.event');
json_encode($payload);
}
finally
{
restore_error_handler();
}

$this->assertNull($deprecation, $deprecation ?? '');
}
}
3 changes: 2 additions & 1 deletion tests/Payloads/Test/TestWHPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
namespace Fortifi\Tests\Webhooks\Payloads\Test;

use Fortifi\Webhooks\Payloads\Test\TestWHP;
use PHPUnit\Framework\TestCase;

class TestWHPTest extends \PHPUnit_Framework_TestCase
class TestWHPTest extends TestCase
{
public function testTransport()
{
Expand Down
103 changes: 103 additions & 0 deletions tests/WebhooksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
namespace Fortifi\Tests\Webhooks;

use Fortifi\Webhooks\Events\CustomerWHE;
use Fortifi\Webhooks\Events\InvoiceWHE;
use Fortifi\Webhooks\Payloads\Customer\CustomerCreatedWHP;
use Fortifi\Webhooks\Payloads\Invoice\InvoiceWHP;
use Fortifi\Webhooks\Webhooks;
use PHPUnit\Framework\TestCase;

class WebhooksTest extends TestCase
{
public function testAll()
{
$all = Webhooks::all();

$this->assertIsArray($all);
$this->assertNotEmpty($all);
$this->assertArrayHasKey(CustomerWHE::CREATED, $all);
$this->assertEquals(CustomerCreatedWHP::class, $all[CustomerWHE::CREATED]);
}

public function testGetPayloadWithValidEvent()
{
$json = json_encode([
'event' => CustomerWHE::CREATED,
'sig' => 'test-sig',
'uuid' => 'test-uuid',
'rqid' => 'test-rqid',
'data' => ['customerFid' => 'cust-123'],
]);

$payload = Webhooks::getPayload($json);

$this->assertInstanceOf(CustomerCreatedWHP::class, $payload);
$this->assertEquals(CustomerWHE::CREATED, $payload->getEventType());
$this->assertEquals('test-uuid', $payload->getPayloadId());
$this->assertEquals('test-rqid', $payload->getRequestId());
}

public function testGetPayloadWithUnknownEvent()
{
$json = json_encode([
'event' => 'unknown.event',
'sig' => 'test-sig',
'uuid' => 'test-uuid',
'rqid' => 'test-rqid',
'data' => [],
]);

$payload = Webhooks::getPayload($json);

$this->assertNull($payload);
}

public function testGetPayloadWithMissingEvent()
{
$json = json_encode([
'sig' => 'test-sig',
'uuid' => 'test-uuid',
'data' => [],
]);

$payload = Webhooks::getPayload($json);

$this->assertNull($payload);
}

public function testAllWithDisplayNames()
{
$displayNames = Webhooks::allWithDisplayNames();

$this->assertIsArray($displayNames);
$this->assertNotEmpty($displayNames);
$this->assertCount(count(Webhooks::all()), $displayNames);

foreach($displayNames as $name)
{
$this->assertIsString($name);
$this->assertNotEmpty($name);
}
}

public function testGetDisplayName()
{
$this->assertEquals('Customer Created', Webhooks::getDisplayName('customer.created'));
$this->assertEquals('Customer Email Unsubscribed', Webhooks::getDisplayName('customer.email.unsubscribed'));
$this->assertEquals('Invoice Add Payment', Webhooks::getDisplayName('invoice.add.payment'));
}

public function testGetDisplayNamePreservesCase()
{
$this->assertEquals('Test Event', Webhooks::getDisplayName('test.event'));
}

public function testAllEventsMapToValidClasses()
{
foreach(Webhooks::all() as $event => $class)
{
$this->assertTrue(class_exists($class), "Class $class for event $event does not exist");
}
}
}