-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
580bca1
commit 309b005
Showing
12 changed files
with
813 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TmpFileManager\Tests\Config; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use TmpFileManager\Config\Config; | ||
|
||
final class ConfigTest extends TestCase | ||
{ | ||
public function testArgs(): void | ||
{ | ||
$config = new Config( | ||
tmpFileDir: sys_get_temp_dir(), | ||
tmpFilePrefix: 'php', | ||
); | ||
|
||
$this->assertSame(sys_get_temp_dir(), $config->getTmpFileDir()); | ||
$this->assertSame('php', $config->getTmpFilePrefix()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TmpFileManager\Tests\Container; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use TmpFileManager\Container\Container; | ||
use TmpFileManager\TmpFile; | ||
|
||
final class ContainerTest extends TestCase | ||
{ | ||
public function testAddTmpFile(): void | ||
{ | ||
$container = new Container(); | ||
$tmpFile = new TmpFile('meow.txt'); | ||
|
||
$container->addTmpFile($tmpFile); | ||
|
||
$this->assertCount(1, $container->getTmpFiles()); | ||
} | ||
|
||
public function testAddTheSameTmpFile(): void | ||
{ | ||
$container = new Container(); | ||
$tmpFile = new TmpFile('meow.txt'); | ||
$container->addTmpFile($tmpFile); | ||
|
||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Temp file "meow.txt" has been already added.'); | ||
|
||
$container->addTmpFile($tmpFile); | ||
} | ||
|
||
public function testHasTmpFile(): void | ||
{ | ||
$container = new Container(); | ||
$tmpFile = new TmpFile('meow.txt'); | ||
|
||
$container->addTmpFile($tmpFile); | ||
|
||
$this->assertTrue($container->hasTmpFile($tmpFile)); | ||
} | ||
|
||
public function testRemoveTmpFile(): void | ||
{ | ||
$container = new Container(); | ||
$tmpFile = new TmpFile('meow.txt'); | ||
$container->addTmpFile($tmpFile); | ||
|
||
$container->removeTmpFile($tmpFile); | ||
|
||
$this->assertCount(0, $container->getTmpFiles()); | ||
} | ||
|
||
public function testRemoveNotAddedTmpFile(): void | ||
{ | ||
$container = new Container(); | ||
$tmpFile = new TmpFile('meow.txt'); | ||
|
||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Temp file "meow.txt" hasn\'t been added yet.'); | ||
|
||
$container->removeTmpFile($tmpFile); | ||
} | ||
|
||
public function testCleatTmpFiles(): void | ||
{ | ||
$container = new Container(); | ||
$container->addTmpFile(new TmpFile('cat.jpg')); | ||
$container->addTmpFile(new TmpFile('dog.jpg')); | ||
$container->addTmpFile(new TmpFile('fish.jpg')); | ||
|
||
$container->clearTmpFiles(); | ||
|
||
$this->assertCount(0, $container->getTmpFiles()); | ||
} | ||
|
||
public function testGetTmpFiles(): void | ||
{ | ||
$container = new Container(); | ||
$container->addTmpFile(new TmpFile('cat.jpg')); | ||
$container->addTmpFile(new TmpFile('dog.jpg')); | ||
$container->addTmpFile(new TmpFile('fish.jpg')); | ||
|
||
$tmpFiles = $container->getTmpFiles(); | ||
|
||
$this->assertNotEmpty($tmpFiles); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TmpFileManager\Tests\Event; | ||
|
||
use TmpFile\TmpFileInterface; | ||
use TmpFileManager\Event\AbstractTmpFileEvent; | ||
|
||
final class TmpFileEventSpy | ||
{ | ||
/** | ||
* @param array<string, int> $eventsCounter | ||
* @param array<string, TmpFileInterface> $tmpFiles | ||
*/ | ||
public function __construct( | ||
private array $eventsCounter = [], | ||
private array $tmpFiles = [], | ||
) { | ||
} | ||
|
||
public function __invoke(AbstractTmpFileEvent $event): void | ||
{ | ||
if (!isset($this->eventsCounter[$event::class])) { | ||
$this->eventsCounter[$event::class] = 0; | ||
} | ||
|
||
++$this->eventsCounter[$event::class]; | ||
$this->tmpFiles[$event->getTmpFile()->getFilename()] = $event->getTmpFile(); | ||
} | ||
|
||
public function getEventsCount(string $className = null): int | ||
{ | ||
if (null === $className) { | ||
return array_sum($this->eventsCounter); | ||
} | ||
|
||
if (!isset($this->eventsCounter[$className])) { | ||
return 0; | ||
} | ||
|
||
return $this->eventsCounter[$className]; | ||
} | ||
|
||
/** | ||
* @return TmpFileInterface[] | ||
*/ | ||
public function getTmpFiles(): array | ||
{ | ||
return array_values($this->tmpFiles); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TmpFileManager\Tests\Event; | ||
|
||
use TmpFileManager\Event\AbstractTmpFileManagerEvent; | ||
|
||
final class TmpFileManagerEventSpy | ||
{ | ||
/** | ||
* @param array<string, int> $eventsCounter | ||
* @param array<string, int> $tmpFilesCount | ||
*/ | ||
public function __construct( | ||
private array $eventsCounter = [], | ||
private array $tmpFilesCount = [], | ||
) { | ||
} | ||
|
||
public function __invoke(AbstractTmpFileManagerEvent $event): void | ||
{ | ||
if (!isset($this->eventsCounter[$event::class])) { | ||
$this->eventsCounter[$event::class] = 0; | ||
} | ||
|
||
if (!isset($this->tmpFilesCount[$event::class])) { | ||
$this->tmpFilesCount[$event::class] = 0; | ||
} | ||
|
||
++$this->eventsCounter[$event::class]; | ||
$this->tmpFilesCount[$event::class] = $event->getContainer()->count(); | ||
} | ||
|
||
public function getEventsCount(string $className = null): int | ||
{ | ||
if (null === $className) { | ||
return array_sum($this->eventsCounter); | ||
} | ||
|
||
if (!isset($this->eventsCounter[$className])) { | ||
return 0; | ||
} | ||
|
||
return $this->eventsCounter[$className]; | ||
} | ||
|
||
public function getTmpFilesCount(string $className = null): int | ||
{ | ||
if (null === $className) { | ||
return array_sum($this->tmpFilesCount); | ||
} | ||
|
||
if (!isset($this->tmpFilesCount[$className])) { | ||
return 0; | ||
} | ||
|
||
return $this->tmpFilesCount[$className]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TmpFileManager\Tests\Filesystem; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use TmpFileManager\Filesystem\Filesystem; | ||
|
||
final class FilesystemTest extends TestCase | ||
{ | ||
public function testCreateTmpFile(): void | ||
{ | ||
$filesystem = new Filesystem(); | ||
|
||
$tmpFile = $filesystem->createTmpFile(sys_get_temp_dir(), 'php'); | ||
|
||
$this->assertFileExists($tmpFile->getFilename()); | ||
|
||
$filesystem->removeTmpFile($tmpFile); | ||
} | ||
|
||
public function testExistTmpFile(): void | ||
{ | ||
$filesystem = new Filesystem(); | ||
|
||
$tmpFile = $filesystem->createTmpFile(sys_get_temp_dir(), 'php'); | ||
|
||
$this->assertTrue($filesystem->existsTmpFile($tmpFile)); | ||
|
||
$filesystem->removeTmpFile($tmpFile); | ||
} | ||
|
||
public function testRemoveTmpFile(): void | ||
{ | ||
$filesystem = new Filesystem(); | ||
|
||
$tmpFile = $filesystem->createTmpFile(sys_get_temp_dir(), 'php'); | ||
|
||
$filesystem->removeTmpFile($tmpFile); | ||
|
||
$this->assertFileDoesNotExist($tmpFile->getFilename()); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
tests/Handler/GarbageCollectionHandler/GarbageCollectionHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TmpFileManager\Tests\Handler\GarbageCollectionHandler; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Filesystem\Filesystem as Fs; | ||
use TmpFileManager\Handler\GarbageCollectionHandler\GarbageCollectionHandler; | ||
use TmpFileManager\Handler\GarbageCollectionHandler\Processor\SyncProcessor; | ||
|
||
final class GarbageCollectionHandlerTest extends TestCase | ||
{ | ||
public function testGarbageCollectionHandler(): void | ||
{ | ||
$fs = new Fs(); | ||
$tmpFile = $fs->tempnam(sys_get_temp_dir(), 'php'); | ||
$fs->touch($tmpFile, time() - 7_200); | ||
|
||
$handler = new GarbageCollectionHandler( | ||
probability: 100, | ||
divisor: 1, | ||
lifetime: 3_600, | ||
processor: new SyncProcessor(), | ||
); | ||
$handler->handle(sys_get_temp_dir(), 'php'); | ||
|
||
$this->assertFileDoesNotExist($tmpFile); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
tests/Handler/GarbageCollectionHandler/Processor/AsyncProcessorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TmpFileManager\Tests\Handler\GarbageCollectionHandler\Processor; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Filesystem\Filesystem as Fs; | ||
use TmpFileManager\Handler\GarbageCollectionHandler\Processor\AsyncProcessor; | ||
|
||
final class AsyncProcessorTest extends TestCase | ||
{ | ||
public function testSyncProcessor(): void | ||
{ | ||
$fs = new Fs(); | ||
$tmpFile = $fs->tempnam(sys_get_temp_dir(), 'php'); | ||
$fs->touch($tmpFile, time() - 7_200); | ||
|
||
$processor = new AsyncProcessor( | ||
isParallel: false, | ||
); | ||
$processor->process(sys_get_temp_dir(), 'php', 3_600); | ||
|
||
$this->assertFileDoesNotExist($tmpFile); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
tests/Handler/GarbageCollectionHandler/Processor/SyncProcessorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TmpFileManager\Tests\Handler\GarbageCollectionHandler\Processor; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Filesystem\Filesystem as Fs; | ||
use TmpFileManager\Handler\GarbageCollectionHandler\Processor\SyncProcessor; | ||
|
||
final class SyncProcessorTest extends TestCase | ||
{ | ||
public function testSyncProcessor(): void | ||
{ | ||
$fs = new Fs(); | ||
$tmpFile = $fs->tempnam(sys_get_temp_dir(), 'php'); | ||
$fs->touch($tmpFile, time() - 7_200); | ||
|
||
$processor = new SyncProcessor(); | ||
$processor->process(sys_get_temp_dir(), 'php', 3_600); | ||
|
||
$this->assertFileDoesNotExist($tmpFile); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
tests/Handler/UnclosedResourcesHandler/UnclosedResourcesHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TmpFileManager\Tests\Handler\UnclosedResourcesHandler; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use TmpFileManager\Filesystem\Filesystem; | ||
use TmpFileManager\Handler\UnclosedResourcesHandler\UnclosedResourcesHandler; | ||
|
||
final class UnclosedResourcesHandlerTest extends TestCase | ||
{ | ||
public function testUnclosedResourcesHandler(): void | ||
{ | ||
$filesystem = new Filesystem(); | ||
$tmpFile = $filesystem->createTmpFile(sys_get_temp_dir(), 'php'); | ||
$fh = fopen($tmpFile->getFilename(), 'r'); | ||
|
||
try { | ||
$handler = new UnclosedResourcesHandler(); | ||
$handler->handle([$tmpFile]); | ||
|
||
$this->assertFalse(\is_resource($fh)); | ||
} finally { | ||
$filesystem->removeTmpFile($tmpFile); | ||
} | ||
} | ||
} |
Oops, something went wrong.