mirror of
https://github.com/pestphp/pest.git
synced 2026-03-07 00:07:22 +01:00
first
This commit is contained in:
86
src/PendingObjects/AfterEachCall.php
Normal file
86
src/PendingObjects/AfterEachCall.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\PendingObjects;
|
||||
|
||||
use Closure;
|
||||
use Pest\Support\Backtrace;
|
||||
use Pest\Support\ChainableClosure;
|
||||
use Pest\Support\HigherOrderMessageCollection;
|
||||
use Pest\Support\NullClosure;
|
||||
use Pest\TestSuite;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class AfterEachCall
|
||||
{
|
||||
/**
|
||||
* Holds the test suite.
|
||||
*
|
||||
* @var TestSuite
|
||||
*/
|
||||
private $testSuite;
|
||||
|
||||
/**
|
||||
* Holds the filename.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $filename;
|
||||
|
||||
/**
|
||||
* Holds the before each closure.
|
||||
*
|
||||
* @var Closure
|
||||
*/
|
||||
private $closure;
|
||||
|
||||
/**
|
||||
* Holds calls that should be proxied.
|
||||
*
|
||||
* @var HigherOrderMessageCollection
|
||||
*/
|
||||
private $proxies;
|
||||
|
||||
/**
|
||||
* Creates a new instance of before each call.
|
||||
*/
|
||||
public function __construct(TestSuite $testSuite, string $filename, Closure $closure = null)
|
||||
{
|
||||
$this->testSuite = $testSuite;
|
||||
$this->filename = $filename;
|
||||
$this->closure = $closure instanceof Closure ? $closure : NullClosure::create();
|
||||
|
||||
$this->proxies = new HigherOrderMessageCollection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch the creation of each call.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$proxies = $this->proxies;
|
||||
|
||||
$this->testSuite->afterEach->set(
|
||||
$this->filename,
|
||||
ChainableClosure::from(function () use ($proxies): void {
|
||||
$proxies->chain($this);
|
||||
}, $this->closure)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the calls to be used on the target.
|
||||
*
|
||||
* @param array<int, mixed> $arguments
|
||||
*/
|
||||
public function __call(string $name, array $arguments): self
|
||||
{
|
||||
$this->proxies
|
||||
->add(Backtrace::file(), Backtrace::line(), $name, $arguments);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
86
src/PendingObjects/BeforeEachCall.php
Normal file
86
src/PendingObjects/BeforeEachCall.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\PendingObjects;
|
||||
|
||||
use Closure;
|
||||
use Pest\Support\Backtrace;
|
||||
use Pest\Support\ChainableClosure;
|
||||
use Pest\Support\HigherOrderMessageCollection;
|
||||
use Pest\Support\NullClosure;
|
||||
use Pest\TestSuite;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class BeforeEachCall
|
||||
{
|
||||
/**
|
||||
* Holds the test suite.
|
||||
*
|
||||
* @var TestSuite
|
||||
*/
|
||||
private $testSuite;
|
||||
|
||||
/**
|
||||
* Holds the filename.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $filename;
|
||||
|
||||
/**
|
||||
* Holds the before each closure.
|
||||
*
|
||||
* @var Closure
|
||||
*/
|
||||
private $closure;
|
||||
|
||||
/**
|
||||
* Holds calls that should be proxied.
|
||||
*
|
||||
* @var HigherOrderMessageCollection
|
||||
*/
|
||||
private $proxies;
|
||||
|
||||
/**
|
||||
* Creates a new instance of before each call.
|
||||
*/
|
||||
public function __construct(TestSuite $testSuite, string $filename, Closure $closure = null)
|
||||
{
|
||||
$this->testSuite = $testSuite;
|
||||
$this->filename = $filename;
|
||||
$this->closure = $closure instanceof Closure ? $closure : NullClosure::create();
|
||||
|
||||
$this->proxies = new HigherOrderMessageCollection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch the creation of each call.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$proxies = $this->proxies;
|
||||
|
||||
$this->testSuite->beforeEach->set(
|
||||
$this->filename,
|
||||
ChainableClosure::from(function () use ($proxies): void {
|
||||
$proxies->chain($this);
|
||||
}, $this->closure)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the calls to be used on the target.
|
||||
*
|
||||
* @param array<int, mixed> $arguments
|
||||
*/
|
||||
public function __call(string $name, array $arguments): self
|
||||
{
|
||||
$this->proxies
|
||||
->add(Backtrace::file(), Backtrace::line(), $name, $arguments);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
133
src/PendingObjects/TestCall.php
Normal file
133
src/PendingObjects/TestCall.php
Normal file
@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\PendingObjects;
|
||||
|
||||
use Closure;
|
||||
use Pest\Factories\TestCaseFactory;
|
||||
use Pest\Support\Backtrace;
|
||||
use Pest\Support\NullClosure;
|
||||
use Pest\TestSuite;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class TestCall
|
||||
{
|
||||
/**
|
||||
* Holds the test case factory.
|
||||
*
|
||||
* @readonly
|
||||
*
|
||||
* @var TestCaseFactory
|
||||
*/
|
||||
private $testCaseFactory;
|
||||
|
||||
/**
|
||||
* Creates a new instance of a pending test call.
|
||||
*/
|
||||
public function __construct(TestSuite $testSuite, string $filename, string $description, Closure $closure = null)
|
||||
{
|
||||
$this->testCaseFactory = new TestCaseFactory($filename, $description, $closure);
|
||||
|
||||
$testSuite->tests->set($this->testCaseFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the test throws the given `$exceptionClass` when called.
|
||||
*/
|
||||
public function throws(string $exceptionClass, string $exceptionMessage = null): TestCall
|
||||
{
|
||||
$this->testCaseFactory
|
||||
->proxies
|
||||
->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exceptionClass]);
|
||||
|
||||
if (is_string($exceptionMessage)) {
|
||||
$this->testCaseFactory
|
||||
->proxies
|
||||
->add(Backtrace::file(), Backtrace::line(), 'expectExceptionMessage', [$exceptionMessage]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the current test multiple times with
|
||||
* each item of the given `iterable`.
|
||||
*
|
||||
* @param \Closure|iterable<int, mixed>|string $data
|
||||
*/
|
||||
public function with($data): TestCall
|
||||
{
|
||||
$this->testCaseFactory->dataset = $data;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the test suite only this test case.
|
||||
*/
|
||||
public function only(): TestCall
|
||||
{
|
||||
$this->testCaseFactory->only = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the test groups(s).
|
||||
*/
|
||||
public function group(string ...$groups): TestCall
|
||||
{
|
||||
$this->testCaseFactory
|
||||
->factoryProxies
|
||||
->add(Backtrace::file(), Backtrace::line(), 'addGroups', [$groups]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Skips the current test.
|
||||
*
|
||||
* @param Closure|bool|string $conditionOrMessage
|
||||
*/
|
||||
public function skip($conditionOrMessage = true, string $message = ''): TestCall
|
||||
{
|
||||
$condition = is_string($conditionOrMessage)
|
||||
? NullClosure::create()
|
||||
: $conditionOrMessage;
|
||||
|
||||
$condition = is_callable($condition)
|
||||
? $condition
|
||||
: function () use ($condition) { /* @phpstan-ignore-line */
|
||||
return $condition;
|
||||
};
|
||||
|
||||
$message = is_string($conditionOrMessage)
|
||||
? $conditionOrMessage
|
||||
: $message;
|
||||
|
||||
if ($condition() !== false) {
|
||||
$this->testCaseFactory
|
||||
->chains
|
||||
->add(Backtrace::file(), Backtrace::line(), 'markTestSkipped', [$message]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the calls to be used on the target.
|
||||
*
|
||||
* @param array<int, mixed> $arguments
|
||||
*/
|
||||
public function __call(string $name, array $arguments): self
|
||||
{
|
||||
$this->testCaseFactory
|
||||
->chains
|
||||
->add(Backtrace::file(), Backtrace::line(), $name, $arguments);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
97
src/PendingObjects/UsesCall.php
Normal file
97
src/PendingObjects/UsesCall.php
Normal file
@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\PendingObjects;
|
||||
|
||||
use Pest\Exceptions\InvalidUsesPath;
|
||||
use Pest\TestSuite;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class UsesCall
|
||||
{
|
||||
/**
|
||||
* Holds the class and traits.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
private $classAndTraits;
|
||||
|
||||
/**
|
||||
* Holds the base dirname here the uses call was performed.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $filename;
|
||||
|
||||
/**
|
||||
* Holds the targets of the uses.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
private $targets;
|
||||
|
||||
/**
|
||||
* Holds the groups of the uses.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
private $groups = [];
|
||||
|
||||
/**
|
||||
* Creates a new instance of a pending test uses.
|
||||
*
|
||||
* @param array<int, string> $classAndTraits
|
||||
*/
|
||||
public function __construct(string $filename, array $classAndTraits)
|
||||
{
|
||||
$this->classAndTraits = $classAndTraits;
|
||||
$this->filename = $filename;
|
||||
$this->targets = [$filename];
|
||||
}
|
||||
|
||||
/**
|
||||
* The directories or file where the
|
||||
* class or trais should be used.
|
||||
*/
|
||||
public function in(string ...$targets): void
|
||||
{
|
||||
$targets = array_map(function ($path): string {
|
||||
return $path[0] === DIRECTORY_SEPARATOR
|
||||
? $path
|
||||
: implode(DIRECTORY_SEPARATOR, [
|
||||
dirname($this->filename),
|
||||
$path,
|
||||
]);
|
||||
}, $targets);
|
||||
|
||||
$this->targets = array_map(function ($target): string {
|
||||
$realTarget = realpath($target);
|
||||
if ($realTarget === false) {
|
||||
throw new InvalidUsesPath($target);
|
||||
}
|
||||
|
||||
return $realTarget;
|
||||
}, $targets);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the test group(s).
|
||||
*/
|
||||
public function group(string ...$groups): UsesCall
|
||||
{
|
||||
$this->groups = $groups;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch the creation of uses.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
TestSuite::getInstance()->tests->use($this->classAndTraits, $this->groups, $this->targets);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user