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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-mockery": "^2.0",
"phpstan/phpstan-phpunit": "^2.0",
"phpunit/phpunit": "^10.5",
"phpunit/phpunit": "^11.5 || ^12.0 || ^13.0",
"psalm/plugin-mockery": "^1.1",
"psalm/plugin-phpunit": "^0.19.0",
"psalm/plugin-phpunit": "^0.19.0 || ^0.20.0",
"rector/rector": "^2.0",
"rekalogika/direct-property-access": "^1.1.2 || ^1.2",
"symfony/asset": "^6.2 || ^7.0 || ^8.0",
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
beStrictAboutOutputDuringTests="true"
Expand Down
88 changes: 88 additions & 0 deletions tests/src/RestoresExceptionHandlersTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/file-src package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\File\Tests;

use PHPUnit\Framework\Attributes\After;
use PHPUnit\Framework\Attributes\Before;

trait RestoresExceptionHandlersTrait
{
private int $exceptionHandlerStackDepth = 0;
private int $errorHandlerStackDepth = 0;

#[Before]
protected function recordHandlerStackDepth(): void
{
$this->exceptionHandlerStackDepth = self::countExceptionHandlers();
$this->errorHandlerStackDepth = self::countErrorHandlers();
}

#[After]
protected function restoreExceptionAndErrorHandlers(): void
{
while (self::countExceptionHandlers() > $this->exceptionHandlerStackDepth) {
restore_exception_handler();
}

while (self::countErrorHandlers() > $this->errorHandlerStackDepth) {
restore_error_handler();
}
}

private static function countExceptionHandlers(): int
{
$count = 0;
$stack = [];

while (true) {
$handler = set_exception_handler(static fn() => null);
restore_exception_handler();
if ($handler === null) {
break;
}
$stack[] = $handler;
restore_exception_handler();
$count++;
}

foreach (array_reverse($stack) as $handler) {
set_exception_handler($handler);
}

return $count;
}

private static function countErrorHandlers(): int
{
$count = 0;
$stack = [];

while (true) {
$handler = set_error_handler(static fn(): bool => false);
restore_error_handler();
if ($handler === null) {
break;
}
$stack[] = $handler;
restore_error_handler();
$count++;
}

foreach (array_reverse($stack) as $handler) {
set_error_handler($handler);
}

return $count;
}
}
6 changes: 5 additions & 1 deletion tests/src/Tests/FileAssociation/ClassSignatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@

namespace Rekalogika\File\Tests\Tests\FileAssociation;

use PHPUnit\Framework\Attributes\DataProvider;
use Rekalogika\File\Association\Contracts\ClassSignatureResolverInterface;
use Rekalogika\File\Tests\RestoresExceptionHandlersTrait;
use Rekalogika\File\Tests\Tests\Model\Entity;
use Rekalogika\File\Tests\Tests\Model\EntityWithOverridenSignature;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

final class ClassSignatureTest extends KernelTestCase
{
use RestoresExceptionHandlersTrait;

/**
* @param class-string $class
* @dataProvider classSignatureProvider
*/
#[DataProvider('classSignatureProvider')]
public function testClassSignature(
string $class,
string $expectedSignature,
Expand Down
3 changes: 3 additions & 0 deletions tests/src/Tests/FileAssociation/DoctrineTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Persistence\Proxy;
use Rekalogika\Contracts\File\FileRepositoryInterface;
use Rekalogika\File\Tests\RestoresExceptionHandlersTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

abstract class DoctrineTestCase extends KernelTestCase
{
use RestoresExceptionHandlersTrait;

protected EntityManagerInterface $entityManager;
protected FileRepositoryInterface $fileRepository;

Expand Down
2 changes: 2 additions & 0 deletions tests/src/Tests/FileAssociation/ObjectManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Rekalogika\File\Association\Model\MissingFile;
use Rekalogika\File\File;
use Rekalogika\File\TemporaryFile;
use Rekalogika\File\Tests\RestoresExceptionHandlersTrait;
use Rekalogika\File\Tests\Tests\File\FileTestTrait;
use Rekalogika\File\Tests\Tests\Model\Entity;
use Rekalogika\File\Tests\Tests\Model\EntityWithLazyFile;
Expand All @@ -30,6 +31,7 @@
final class ObjectManagerTest extends KernelTestCase
{
use FileTestTrait;
use RestoresExceptionHandlersTrait;

private ObjectManagerInterface $objectManager;

Expand Down
3 changes: 2 additions & 1 deletion tests/src/Tests/FileAssociation/PropertyListerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Rekalogika\File\Tests\Tests\FileAssociation;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Rekalogika\File\Association\Contracts\PropertyListerInterface;
use Rekalogika\File\Association\Model\Property;
Expand All @@ -28,8 +29,8 @@ final class PropertyListerTest extends TestCase
/**
* @param class-string $class
* @param list<Property> $expectedProperties
* @dataProvider propertyListerProvider
*/
#[DataProvider('propertyListerProvider')]
public function testPropertyLister(
PropertyListerInterface $lister,
string $class,
Expand Down
5 changes: 2 additions & 3 deletions tests/src/Tests/FileAssociation/ProxyUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
namespace Rekalogika\File\Tests\Tests\FileAssociation;

use MongoDBODMProxies\__PM__\Rekalogika\File\Tests\App\Entity\DummyEntity\Generated93deedc1e7b56ba9c8d5a337a376eda9;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Rekalogika\File\Association\Util\ProxyUtil;
use Rekalogika\File\Tests\App\Entity\DummyEntity;

final class ProxyUtilTest extends TestCase
{
/**
* @dataProvider provideTestProxy
*/
#[DataProvider('provideTestProxy')]
public function testProxy(string $class, string $expected): void
{
$this->assertTrue(class_exists($class), \sprintf(
Expand Down
99 changes: 99 additions & 0 deletions tests/src/Tests/FileImage/TwigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Rekalogika\File\Tests\Tests\FileImage;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Rekalogika\Contracts\File\FileRepositoryInterface;
use Rekalogika\File\FilePointer;
use Rekalogika\File\Image\ImageResizer;
Expand Down Expand Up @@ -74,4 +76,101 @@ protected function getFixturesDir(): string
{
return __DIR__ . '/Fixtures/';
}

/**
* @param mixed $file
* @param mixed $message
* @param mixed $condition
* @param mixed $templates
* @param mixed $exception
* @param mixed $outputs
* @param mixed $deprecation
*/
#[DataProvider('provideIntegrationTests')]
#[\Override]
public function testIntegration($file, $message, $condition, $templates, $exception, $outputs, $deprecation = ''): void
{
$this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs, $deprecation);
}

/**
* @param mixed $file
* @param mixed $message
* @param mixed $condition
* @param mixed $templates
* @param mixed $exception
* @param mixed $outputs
* @param mixed $deprecation
*/
#[DataProvider('provideLegacyIntegrationTests')]
#[Group('legacy')]
#[\Override]
public function testLegacyIntegration($file, $message, $condition, $templates, $exception, $outputs, $deprecation = ''): void
{
$this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs, $deprecation);
}

/**
* @return iterable<array-key,array<array-key,mixed>>
*/
public static function provideIntegrationTests(): iterable
{
return self::collectFixtures(false);
}

/**
* @return iterable<array-key,array<array-key,mixed>>
*/
public static function provideLegacyIntegrationTests(): iterable
{
return self::collectFixtures(true);
}

/**
* @return array<array-key,array<array-key,mixed>>
*/
private static function collectFixtures(bool $legacyTests): array
{
$fixturesDir = (string) realpath(static::getFixturesDirectory());
$tests = [];

/** @var \SplFileInfo $file */
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($fixturesDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
if (!preg_match('/\.test$/', (string) $file)) {
continue;
}

if ($legacyTests xor str_contains($file->getRealpath(), '.legacy.test')) {
continue;
}

$test = (string) file_get_contents($file->getRealpath());

if (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*(?:--DEPRECATION--\s*(.*?))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)\s*(?:--DATA--\s*(.*))?\s*--EXCEPTION--\s*(.*)/sx', $test, $match)) {
$message = $match[1];
$condition = $match[2];
$deprecation = $match[3];
$templates = IntegrationTestCase::parseTemplates($match[4]);
$exception = $match[6];
$outputs = [[null, $match[5], null, '']];
} elseif (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*(?:--DEPRECATION--\s*(.*?))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
$message = $match[1];
$condition = $match[2];
$deprecation = $match[3];
$templates = IntegrationTestCase::parseTemplates($match[4]);
$exception = false;
preg_match_all('/--DATA--(.*?)(?:--CONFIG--(.*?))?--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $outputs, \PREG_SET_ORDER);
} else {
throw new \InvalidArgumentException(\sprintf('Test "%s" is not valid.', str_replace($fixturesDir . '/', '', (string) $file)));
}

$tests[str_replace($fixturesDir . '/', '', (string) $file)] = [str_replace($fixturesDir . '/', '', (string) $file), $message, $condition, $templates, $exception, $outputs, $deprecation];
}

if ($legacyTests && !$tests) {
return [['not', '-', '', [], '', []]];
}

return $tests;
}
}
5 changes: 2 additions & 3 deletions tests/src/Tests/FileNull/NullFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Rekalogika\File\Tests\Tests\FileNull;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Rekalogika\Contracts\File\FileInterface;
use Rekalogika\Contracts\File\FilePointerInterface;
Expand All @@ -38,9 +39,7 @@ public function testNullFile(): void
$this->assertInstanceOf(NullPointer::class, $nullPointer);
}

/**
* @dataProvider equalityProvider
*/
#[DataProvider('equalityProvider')]
public function testEquality(
bool $expected,
FilePointerInterface|FileInterface $file1,
Expand Down
3 changes: 3 additions & 0 deletions tests/src/Tests/FileServer/TemporaryUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@

use Rekalogika\File\File;
use Rekalogika\File\FilePointer;
use Rekalogika\File\Tests\RestoresExceptionHandlersTrait;
use Rekalogika\TemporaryUrl\TemporaryUrlGeneratorInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

final class TemporaryUrlTest extends KernelTestCase
{
use RestoresExceptionHandlersTrait;

public function testTemporaryUrlWithFilePointer(): void
{
$this->markTestSkipped();
Expand Down
2 changes: 2 additions & 0 deletions tests/src/Tests/FileSymfonyBridge/FileAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
use Rekalogika\File\FilePointer;
use Rekalogika\File\LocalTemporaryFile;
use Rekalogika\File\TemporaryFile;
use Rekalogika\File\Tests\RestoresExceptionHandlersTrait;
use Rekalogika\File\Tests\Tests\File\FileTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\File\File;

final class FileAdapterTest extends KernelTestCase
{
use FileTestTrait;
use RestoresExceptionHandlersTrait;

private function createRemoteFile(): FileInterface
{
Expand Down
3 changes: 3 additions & 0 deletions tests/src/Tests/FileZip/ZipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
use Rekalogika\Contracts\File\FileInterface;
use Rekalogika\Contracts\File\FileRepositoryInterface;
use Rekalogika\File\FilePointer;
use Rekalogika\File\Tests\RestoresExceptionHandlersTrait;
use Rekalogika\File\Zip\FileZip;
use Rekalogika\File\Zip\Model\Directory;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

final class ZipTest extends KernelTestCase
{
use RestoresExceptionHandlersTrait;

private ?FileRepositoryInterface $fileRepository = null;

private ?FileZip $fileZip = null;
Expand Down
Loading