Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NTR - Various updates #104

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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
38 changes: 36 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,57 @@ 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: Setup database
run: composer setup:test
- 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
- name: Setup database
run: composer setup:test
- name: Run Infection
run: composer infection -- --min-msi=95
env:
Expand Down
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,19 @@
"setup": [
"bin/console doctrine:schema:drop --force --full-database",
"bin/console doctrine:migrations:migrate -n",
"@setup:url"
"@setup:url",
"@setup:test"
],
"setup:url": [
"bin/console setup:url",
"npm run dev"
]
],
"setup:test": [
"bin/console doctrine:schema:drop --env=test --force --full-database",
"bin/console doctrine:migrations:migrate --env=test -n"
],
"phpunit:unit": "@phpunit --testsuite=SwagBraintreeUnitTest",
"phpunit:integration": "@phpunit --testsuite=SwagBraintreeIntegrationTest"
},
"conflict": {
"symfony/symfony": "*"
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
30 changes: 0 additions & 30 deletions src/Doctrine/RespectfulUuidGenerator.php

This file was deleted.

9 changes: 4 additions & 5 deletions src/Entity/Contract/EntityIdTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Swag\Braintree\Entity\Contract;

use Doctrine\ORM\Mapping as ORM;
use Swag\Braintree\Doctrine\RespectfulUuidGenerator;
use Symfony\Component\Uid\Uuid;

#[ORM\MappedSuperclass]
Expand All @@ -12,17 +11,17 @@ trait EntityIdTrait
#[ORM\Id]
#[ORM\Column(type: 'uuid', unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: RespectfulUuidGenerator::class)]
#[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
// will never be null in the database
// null only signalizes the uuid-generator to generate a new uuid for the database
private ?Uuid $id = null;
private Uuid $id;

public function getId(): ?Uuid
{
return $this->id;
return $this->id ?? null;
}

public function setId(?Uuid $id): self
public function setId(Uuid $id): self
{
$this->id = $id;

Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Contract/EntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface EntityInterface
{
public function getId(): ?Uuid;

public function setId(?Uuid $id): self;
public function setId(Uuid $id): self;

public function getCreatedAt(): \DateTimeInterface;

Expand Down
54 changes: 54 additions & 0 deletions tests/integration/DoctrineUuidTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);

namespace Swag\Braintree\Tests\Integration;

use Swag\Braintree\Entity\ConfigEntity;

class DoctrineUuidTest extends SwagBraintreeTestCase
{
public function testPersistTwice(): void
{
$em = $this->getEntityManager();

$shop = $this->createShop();
$config = (new ConfigEntity())
->setShop($shop);

$em->persist($config);
$em->flush();

static::assertNotNull($config->getId());
$id = (string) $config->getId();

$em->persist($config);
$em->flush();

static::assertSame($id, (string) $config->getId());
}

public function testWithRef(): void
{
$em = $this->getEntityManager();

$shop = $this->createShop();
$config = (new ConfigEntity())
->setShop($shop);

$em->persist($config);
$em->flush();

static::assertNotNull($config->getId());
$id = (string) $config->getId();

$configRef = $em->getReference(ConfigEntity::class, $id);
$configRef->setThreeDSecureEnforced(true);

$em->persist($configRef);
$em->flush();

$config = $em->find(ConfigEntity::class, $id);

static::assertSame($id, (string) $config->getId());
static::assertTrue($config->isThreeDSecureEnforced());
}
}
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;
}
}
99 changes: 0 additions & 99 deletions tests/unit/Doctrine/RespectfulUuidGeneratorTest.php

This file was deleted.