Skip to content

Commit

Permalink
Merge pull request #1 from kurozumi/add-test
Browse files Browse the repository at this point in the history
テスト追加
  • Loading branch information
kurozumi authored Sep 12, 2024
2 parents ccd5281 + 04a9913 commit 9b846d1
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 4 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/phpstan.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: PHPStan
on: [workflow_dispatch, pull_request]

env:
PLUGIN_CODE: StripeWebhook
PLUGIN_PACKAGE_NAME: 'ec-cube/stripewebhook'

jobs:
phpstan:
name: PHPStan
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
eccube-versions: ['4.3']
php-versions: [ '8.2' ]
database: [ 'mysql' ]
include:
- database: mysql
database_url: mysql://root:[email protected]:3306/eccube_db
database_server_version: 5.7
database_charset: utf8mb4
services:
mysql:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: password
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}

- uses: kurozumi/[email protected]
with:
plugin-code: ${{ env.PLUGIN_CODE }}
plugin-package-name: ${{ env.PLUGIN_PACKAGE_NAME }}
eccube-versions: ${{ matrix.eccube-versions }}
php-versions: ${{ matrix.php-versions }}
database-url: ${{ matrix.database_url }}
database-server-version: ${{ matrix.database_server_version }}
database-charset: ${{ matrix.database_charset }}
71 changes: 71 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: CI/CD for EC-CUBE4 Plugin
on: [workflow_dispatch, pull_request]

env:
PLUGIN_CODE: StripeWebhook
PLUGIN_PACKAGE_NAME: 'ec-cube/stripewebhook'

jobs:
phpunit:
name: PHPUnit
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
eccube-versions: [ '4.3' ]
php-versions: [ '8.2', '8.3' ]
database: [ 'mysql', 'mysql8', 'pgsql' ]
include:
- database: mysql
database_url: mysql://root:[email protected]:3306/eccube_db
database_server_version: 5.7
database_charset: utf8mb4
- database: mysql8
database_url: mysql://root:[email protected]:3308/eccube_db
database_server_version: 8
database_charset: utf8mb4
- database: pgsql
database_url: postgres://postgres:[email protected]:5432/eccube_db
database_server_version: 14
database_charset: utf8

services:
mysql:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: password
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
mysql8:
image: mysql:8
env:
MYSQL_ROOT_PASSWORD: password
ports:
- 3308:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
postgres:
image: postgres:11
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
ports:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}

- uses: kurozumi/eccube-plugin-test@main
with:
plugin-code: ${{ env.PLUGIN_CODE }}
plugin-package-name: ${{ env.PLUGIN_PACKAGE_NAME }}
eccube-versions: ${{ matrix.eccube-versions }}
php-versions: ${{ matrix.php-versions }}
database-url: ${{ matrix.database_url }}
database-server-version: ${{ matrix.database_server_version }}
database-charset: ${{ matrix.database_charset }}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use Stripe\Checkout\Session;
use Stripe\Event;
use Stripe\Invoice;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\RemoteEvent\RemoteEvent;

class StripeEventListener implements EventSubscriberInterface
{
Expand Down
6 changes: 3 additions & 3 deletions RemoteEvent/Event/StripeEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Plugin\StripeWebhook\RemoteEvent\Event;

use Stripe\ApiResource;
use Stripe\StripeObject;
use Symfony\Component\RemoteEvent\RemoteEvent;

final class StripeEvent extends RemoteEvent
Expand All @@ -24,12 +24,12 @@ public function __construct(
private readonly string $name,
private readonly string $id,
private readonly array $payload,
private readonly ApiResource $resource
private readonly StripeObject $resource
) {
parent::__construct($this->name, $this->id, $this->payload);
}

public function getResource(): ApiResource
public function getResource(): StripeObject
{
return $this->resource;
}
Expand Down
9 changes: 9 additions & 0 deletions Tests/Webhook/Fixtures/StripeEvent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": "event_id",
"data": {
"object": {
"object": "test"
}
},
"type": "event_name"
}
19 changes: 19 additions & 0 deletions Tests/Webhook/Fixtures/StripeEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of Stripe Webhook
*
* Copyright(c) Akira Kurozumi All Rights Reserved.
*
* https://a-zumi.net
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

return new Plugin\StripeWebhook\RemoteEvent\Event\StripeEvent(
name: 'event_name',
id: 'event_id',
payload: ['object' => 'test'],
resource: new Stripe\StripeObject()
);
61 changes: 61 additions & 0 deletions Tests/Webhook/StripeRequestParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

/*
* This file is part of Stripe Webhook
*
* Copyright(c) Akira Kurozumi All Rights Reserved.
*
* https://a-zumi.net
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Plugin\StripeWebhook\Tests\Webhook;

use Plugin\StripeWebhook\Webhook\StripeRequestParser;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\RemoteEvent\RemoteEvent;
use Symfony\Component\Webhook\Client\RequestParserInterface;
use Symfony\Component\Webhook\Test\AbstractRequestParserTestCase;

class StripeRequestParserTest extends AbstractRequestParserTestCase
{
/**
* @dataProvider getPayloads
*/
public function testParse(string $payload, RemoteEvent $expected)
{
$request = $this->createRequest($payload);
$parser = $this->createRequestParser();
$wh = $parser->parse($request, $this->getSecret());

self::assertEquals($expected->getName(), $wh->getName());
self::assertEquals($expected->getId(), $wh->getId());
self::assertEquals($expected->getPayload(), $wh->getPayload());
}

protected function createRequestParser(): RequestParserInterface
{
return new StripeRequestParser();
}

protected function getSecret(): string
{
return 'STRIPE_SIGNING_SECRET';
}

protected function createRequest(string $payload): Request
{
$timestamp = time();
$signedPayload = "{$timestamp}.{$payload}";
$expectedSignature = \hash_hmac('sha256', $signedPayload, $this->getSecret());

return Request::create('/', 'POST', [], [], [], [
'Content-Type' => 'application/json',
'HTTP_STRIPE_SIGNATURE' => "t={$timestamp},v1={$expectedSignature},v0=signature",
], $payload);
}
}
21 changes: 21 additions & 0 deletions Tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of Stripe Webhook
*
* Copyright(c) Akira Kurozumi All Rights Reserved.
*
* https://a-zumi.net
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

$loader = require __DIR__.'/../../../../vendor/autoload.php';

$envFile = __DIR__.'/../../../../.env';
if (file_exists($envFile)) {
(new Symfony\Component\Dotenv\Dotenv())
->usePutenv()
->bootEnv($envFile);
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "eccube-plugin",
"require": {
"ec-cube/plugin-installer": "^2.0",
"php": ">=8.1",
"php": ">=8.2",
"symfony/webhook": "*",
"symfony/remote-event": "*",
"stripe/stripe-php": "*"
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
level: 1
45 changes: 45 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="Tests/bootstrap.php"
>
<php>
<ini name="error_reporting" value="-1" />
<env name="KERNEL_CLASS" value="Eccube\Kernel" />
<env name="APP_ENV" value="test" />
<env name="APP_DEBUG" value="0" />
<env name="SHELL_VERBOSITY" value="-1" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
<env name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
<!-- define your env variables for the test env here -->
</php>

<!-- テストの場所 -->
<testsuites>
<testsuite name="Plugin Test Suite">
<directory>./Tests</directory>
</testsuite>
</testsuites>

<!-- 出力するログファイル
<logging>
<log type="coverage-clover" target="./reports/coverage.clover"/>
</logging>
-->

<!-- カバーレージのターゲット -->
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./</directory>
<exclude>
<directory suffix=".php">./Tests</directory>
<directory suffix=".php">./Resource</directory>
<file>./PluginManager.php</file>
</exclude>
</whitelist>
</filter>

<listeners>

0 comments on commit 9b846d1

Please sign in to comment.