mirror of
https://github.com/pestphp/pest.git
synced 2026-03-07 00:07:22 +01:00
feat: adds --retry option
This commit is contained in:
78
src/Repositories/TempRepository.php
Normal file
78
src/Repositories/TempRepository.php
Normal file
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Repositories;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class TempRepository
|
||||
{
|
||||
private const FOLDER = __DIR__ . '/../../.temp';
|
||||
|
||||
/**
|
||||
* Creates a new Temp Repository instance.
|
||||
*/
|
||||
public function __construct(private string $filename)
|
||||
{
|
||||
// ..
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new element.
|
||||
*/
|
||||
public function add(string $element): void
|
||||
{
|
||||
$this->save(array_merge(
|
||||
$this->all(),
|
||||
[$element]
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the existing file, if any, and re-creates it.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
@unlink(self::FOLDER . '/' . $this->filename . '.json'); // @phpstan-ignore-line
|
||||
|
||||
$this->save([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given element exists.
|
||||
*/
|
||||
public function exists(string $element): bool
|
||||
{
|
||||
return in_array($element, $this->all(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all elements.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
private function all(): array
|
||||
{
|
||||
$contents = file_get_contents(self::FOLDER . '/' . $this->filename . '.json');
|
||||
|
||||
assert(is_string($contents));
|
||||
|
||||
$all = json_decode($contents, true);
|
||||
|
||||
return is_array($all) ? $all : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the given elements.
|
||||
*
|
||||
* @param array<int, string> $elements
|
||||
*/
|
||||
private function save(array $elements): void
|
||||
{
|
||||
$contents = json_encode($elements);
|
||||
|
||||
file_put_contents(self::FOLDER . '/' . $this->filename . '.json', $contents);
|
||||
}
|
||||
}
|
||||
@ -100,7 +100,7 @@ final class TestRepository
|
||||
/**
|
||||
* Makes a Test Case from the given filename, if exists.
|
||||
*/
|
||||
public function makeIfExists(string $filename): void
|
||||
public function makeIfNeeded(string $filename): void
|
||||
{
|
||||
if (array_key_exists($filename, $this->testCases)) {
|
||||
$this->make($this->testCases[$filename]);
|
||||
|
||||
Reference in New Issue
Block a user