mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
feat: adds pest function
This commit is contained in:
71
src/Configuration.php
Normal file
71
src/Configuration.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest;
|
||||
|
||||
use Pest\PendingCalls\UsesCall;
|
||||
use Pest\Support\Backtrace;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class Configuration
|
||||
{
|
||||
/**
|
||||
* The instance of the configuration.
|
||||
*/
|
||||
private static ?Configuration $instance = null;
|
||||
|
||||
/**
|
||||
* Gets the instance of the configuration.
|
||||
*/
|
||||
public static function getInstance(): Configuration
|
||||
{
|
||||
return self::$instance ??= new Configuration(
|
||||
Backtrace::file(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new configuration instance.
|
||||
*/
|
||||
private function __construct(
|
||||
private readonly string $filename,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the configuration of a certain folder.
|
||||
*/
|
||||
public function in(string ...$targets): UsesCall
|
||||
{
|
||||
return (new UsesCall($this->filename, []))->in(...$targets);
|
||||
}
|
||||
|
||||
/**
|
||||
* Depending on where is called, it will extend the given classes and traits globally or locally.
|
||||
*/
|
||||
public function extend(string ...$classAndTraits): UsesCall
|
||||
{
|
||||
return (new UsesCall($this->filename, array_values($classAndTraits)))
|
||||
->in($this->filename)
|
||||
->extend(...$classAndTraits);
|
||||
}
|
||||
|
||||
/**
|
||||
* Depending on where is called, it will extend the given classes and traits globally or locally.
|
||||
*/
|
||||
public function use(string ...$classAndTraits): UsesCall
|
||||
{
|
||||
return $this->extend(...$classAndTraits);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the theme configuration.
|
||||
*/
|
||||
public function theme(): Configuration\Theme
|
||||
{
|
||||
return new Configuration\Theme();
|
||||
}
|
||||
}
|
||||
23
src/Configuration/Theme.php
Normal file
23
src/Configuration/Theme.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Configuration;
|
||||
|
||||
use NunoMaduro\Collision\Adapters\Phpunit\Printers\DefaultPrinter;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final readonly class Theme
|
||||
{
|
||||
/**
|
||||
* Sets the theme to compact.
|
||||
*/
|
||||
public function compact(): self
|
||||
{
|
||||
DefaultPrinter::compact(true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,7 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
use Pest\Concerns\Expectable;
|
||||
use Pest\Configuration;
|
||||
use Pest\Exceptions\AfterAllWithinDescribe;
|
||||
use Pest\Exceptions\BeforeAllWithinDescribe;
|
||||
use Pest\Expectation;
|
||||
@ -108,6 +109,16 @@ if (! function_exists('uses')) {
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('pest')) {
|
||||
/**
|
||||
* Creates a new Pest configuration instance.
|
||||
*/
|
||||
function pest(): Configuration
|
||||
{
|
||||
return Configuration::getInstance();
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('test')) {
|
||||
/**
|
||||
* Adds the given closure as a test. The first argument
|
||||
|
||||
@ -48,11 +48,14 @@ final class UsesCall
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly string $filename,
|
||||
private readonly array $classAndTraits
|
||||
private array $classAndTraits
|
||||
) {
|
||||
$this->targets = [$filename];
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use `pest()->theme()->compact()` instead.
|
||||
*/
|
||||
public function compact(): self
|
||||
{
|
||||
DefaultPrinter::compact(true);
|
||||
@ -60,11 +63,31 @@ final class UsesCall
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies the class or traits to use.
|
||||
*
|
||||
* @alias extend
|
||||
*/
|
||||
public function use(string ...$classAndTraits): self
|
||||
{
|
||||
return $this->extend(...$classAndTraits);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies the class or traits to use.
|
||||
*/
|
||||
public function extend(string ...$classAndTraits): self
|
||||
{
|
||||
$this->classAndTraits = array_values($classAndTraits);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The directories or file where the
|
||||
* class or traits should be used.
|
||||
*/
|
||||
public function in(string ...$targets): void
|
||||
public function in(string ...$targets): self
|
||||
{
|
||||
$targets = array_map(function (string $path): string {
|
||||
$startChar = DIRECTORY_SEPARATOR;
|
||||
@ -92,6 +115,8 @@ final class UsesCall
|
||||
|
||||
return $accumulator;
|
||||
}, []);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
96
src/Plugins/Configuration.php
Normal file
96
src/Plugins/Configuration.php
Normal file
@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Plugins;
|
||||
|
||||
use DOMDocument;
|
||||
use Pest\Contracts\Plugins\HandlesArguments;
|
||||
use Pest\Contracts\Plugins\Terminable;
|
||||
use Pest\Plugins\Concerns\HandleArguments;
|
||||
use PHPUnit\TextUI\CliArguments\Builder as CliConfigurationBuilder;
|
||||
use PHPUnit\TextUI\CliArguments\XmlConfigurationFileFinder;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class Configuration implements HandlesArguments, Terminable
|
||||
{
|
||||
use HandleArguments;
|
||||
|
||||
/**
|
||||
* The base PHPUnit file.
|
||||
*/
|
||||
public const BASE_PHPUNIT_FILE = __DIR__
|
||||
.DIRECTORY_SEPARATOR
|
||||
.'..'
|
||||
.DIRECTORY_SEPARATOR
|
||||
.'..'
|
||||
.DIRECTORY_SEPARATOR
|
||||
.'resources/base-phpunit.xml';
|
||||
|
||||
/**
|
||||
* Handles the arguments, adding the cache directory and the cache result arguments.
|
||||
*/
|
||||
public function handleArguments(array $arguments): array
|
||||
{
|
||||
if ($this->hasArgument('--configuration', $arguments) || $this->hasCustomConfigurationFile()) {
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
$arguments = $this->pushArgument('--configuration', $arguments);
|
||||
|
||||
return $this->pushArgument((string) realpath($this->fromGeneratedConfigurationFile()), $arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the configuration file from the generated configuration file.
|
||||
*/
|
||||
private function fromGeneratedConfigurationFile(): string
|
||||
{
|
||||
$path = $this->getTempPhpunitXmlPath();
|
||||
if (file_exists($path)) {
|
||||
unlink($path);
|
||||
}
|
||||
|
||||
$doc = new DOMDocument();
|
||||
$doc->load(self::BASE_PHPUNIT_FILE);
|
||||
|
||||
$contents = $doc->saveXML();
|
||||
|
||||
assert(is_int(file_put_contents($path, $contents)));
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the configuration file is custom.
|
||||
*/
|
||||
private function hasCustomConfigurationFile(): bool
|
||||
{
|
||||
$cliConfiguration = (new CliConfigurationBuilder)->fromParameters([]);
|
||||
$configurationFile = (new XmlConfigurationFileFinder)->find($cliConfiguration);
|
||||
|
||||
return is_string($configurationFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the temporary phpunit.xml path.
|
||||
*/
|
||||
private function getTempPhpunitXmlPath(): string
|
||||
{
|
||||
return getcwd().'/.pest.xml';
|
||||
}
|
||||
|
||||
/**
|
||||
* Terminates the plugin.
|
||||
*/
|
||||
public function terminate(): void
|
||||
{
|
||||
$path = $this->getTempPhpunitXmlPath();
|
||||
|
||||
if (file_exists($path)) {
|
||||
unlink($path);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user