Skip to content

Commit

Permalink
NTR - Added phpunit integration workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
cyl3x committed Nov 20, 2024
1 parent 2b62998 commit b4425f0
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 5 deletions.
34 changes: 32 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,50 @@ jobs:
- name: Run PHPStan
run: composer phpstan

phpunit:
phpunit-unit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup-php
- name: Run PHPUnit
run: composer phpunit -- --coverage-clover=coverage.xml
run: composer phpunit:unit -- --coverage-clover=coverage.xml
- name: Codecov
uses: codecov/codecov-action@v4
with:
files: ./coverage.xml

phpunit-integration:
runs-on: ubuntu-latest
services:
mariadb:
image: mariadb:10.5
env:
MARIADB_ROOT_PASSWORD: swagbraintree
MYSQL_DATABASE: swagbraintree_test
ports: ['3306:3306']
env:
DATABASE_URL: mysql://root:[email protected]:3306/swagbraintree
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup-php
- name: Run PHPUnit
run: composer phpunit:integration -- --coverage-clover=coverage.xml
- name: Codecov
uses: codecov/codecov-action@v4
with:
files: ./coverage.xml

infection:
runs-on: ubuntu-latest
services:
mariadb:
image: mariadb:10.5
env:
MARIADB_ROOT_PASSWORD: swagbraintree
MYSQL_DATABASE: swagbraintree_test
ports: ['3306:3306']
env:
DATABASE_URL: mysql://root:[email protected]:3306/swagbraintree
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup-php
Expand Down
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,21 @@
"eslint": "npm run eslint",
"eslint-fix": "npm run eslint-fix",
"setup": [
"bin/console doctrine:schema:drop --force --full-database",
"bin/console doctrine:migrations:migrate -n",
"bin/console doctrine:schema:drop --force --full-database; #",
"bin/console doctrine:migrations:migrate -n; #",
"@setup:url"
],
"setup:url": [
"bin/console setup:url",
"npm run dev"
],
"phpunit:unit": [
"@phpunit --testsuite=SwagBraintreeUnitTest"
],
"phpunit:integration": [
"bin/console doctrine:schema:drop --env=test --force --full-database; #",
"bin/console doctrine:migrations:migrate --env=test -n; #",
"@phpunit --testsuite=SwagBraintreeIntegrationTest"
]
},
"conflict": {
Expand Down
5 changes: 4 additions & 1 deletion devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ in {
services.mysql = {
enable = true;
package = pkgs.mariadb_105;
initialDatabases = lib.mkDefault [{ name = "swagbraintree"; }];
initialDatabases = lib.mkDefault [
{ name = "swagbraintree"; }
{ name = "swagbraintree_test"; }
];
ensureUsers = lib.mkDefault [
{
name = "swagbraintree";
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ parameters:
message: '#Multiple class/interface/trait is not allowed in single file#'
path: tests/unit

- # don't want to prefix this
message: '#Only abstract classes can be extended#'
path: tests/integration/DoctrineUuidTest.php

rules:
# rules from https://github.com/symplify/phpstan-rules
# domain
Expand Down
62 changes: 62 additions & 0 deletions tests/integration/SwagBraintreeTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php declare(strict_types=1);

namespace Swag\Braintree\Tests\Integration;

use Doctrine\ORM\EntityManagerInterface;
use Swag\Braintree\Entity\ShopEntity;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class SwagBraintreeTestCase extends KernelTestCase
{
protected function tearDown(): void
{
$conn = static::getEntityManager()->getConnection();
$tables = $conn->createSchemaManager()->listTableNames();
/** @phpstan-ignore-next-line - yes `getTableName` will work */
$migrationTable = static::getContainer()->get('doctrine.migrations.dependency_factory')
->getConfiguration()
->getMetadataStorageConfiguration()
->getTableName();

$conn->executeStatement('SET FOREIGN_KEY_CHECKS=0;');

try {
foreach ($tables as $table) {
if ($table === $migrationTable) {
continue;
}

$conn->executeStatement(\sprintf(
'TRUNCATE TABLE %s',
$conn->quoteIdentifier($table),
));
}
} finally {
$conn->executeStatement('SET FOREIGN_KEY_CHECKS=1;');
}

parent::tearDown();
}

protected static function getEntityManager(): EntityManagerInterface
{
return static::getContainer()->get('doctrine.orm.default_entity_manager');
}

protected static function createShop(string $url = 'https://shop.example.com', string $secret = 'definitly-a-secure-secret'): ShopEntity
{
$shopId = \bin2hex(\random_bytes(10));

$shop = new ShopEntity(
$shopId,
$url,
$secret,
);

$em = static::getEntityManager();
$em->persist($shop);
$em->flush();

return $shop;
}
}

0 comments on commit b4425f0

Please sign in to comment.