1. Go to this page and download the library: Download mrpunyapal/peststan library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
/** @var int|string $value */
$value = getValue();
expect($value)->toBeString();
// PHPStan now knows the expectation wraps a string
expect($value)->toBeInstanceOf(User::class);
// PHPStan now knows the expectation wraps a User
// tests/Pest.php
uses(Tests\TestCase::class)->in('Feature');
// tests/Feature/ExampleTest.php
it('can access test case methods', function () {
$this->get('/'); // PHPStan knows $this is Tests\TestCase
});
beforeEach(function () {
$this->assertTrue(true); // Works in hooks too
});
beforeEach(function () {
$this->post = new Post; // Post
$this->title = 'Hello'; // 'Hello' (constant string)
$this->count = 42; // 42 (constant int)
$this->active = true; // true
});
it('knows the property types', function () {
$this->post->title; // PHPStan knows $this->post is Post — no "Cannot access property on mixed" error
strlen($this->title); // fine — PHPStan knows it is a string
});
beforeEach(function () {
/** @var User $user */
$user = User::factory()->create();
$this->user = $user; // User
});
beforeEach(function () { $this->item = new Post; });
beforeEach(function () { $this->item = new Comment; });
it('sees the union', function () {
$this->item; // Post|Comment
});
it('does something'); // fine — todo test
it('does something', function () {});
// ✘ Test 'does something' has an empty closure body. Did you forget to add assertions?
it('example', static function () {
// ✘ Test closure passed to it() must not be static.
expect(true)->toBeTrue();
});
describe('suite', function () {
beforeAll(function () { /* ... */ });
// ✘ beforeAll() cannot be used inside describe() blocks.
afterAll(function () { /* ... */ });
// ✘ afterAll() cannot be used inside describe() blocks.
});
it('does something', fn () => expect(1)->toBe(1));
it('does something', fn () => expect(2)->toBe(2));
// ✘ A test with the description 'it does something' already exists in this file.
expect(42)->toBeString();
// ✘ Calling toBeString() on Expectation<int>; assertion is impossible.
expect('hello')->toBeNull();
// ✘ Calling toBeNull() on Expectation<string>; assertion is impossible.
expect(true)->toBeTrue();
// ✘ Calling toBeTrue() on Expectation<true>; assertion is redundant.
expect('hello')->toBeString();
// ✘ Calling toBeString() on Expectation<string>; assertion is redundant.
expect(42)->toBeNumeric();
// ✘ Calling toBeNumeric() on Expectation<int>; assertion is redundant.
expect(42)->each(fn ($e) => $e->toBeInt());
// ✘ Calling each() on Expectation<int>; matcher g.
beforeAll(function () {
$this->db = new Database; // ✘ beforeAll() runs in static context — $this is not available. Use beforeEach() instead.
});
it('fails', function () { ... })->throws('App\NonExistentException');
// ✘ Class App\NonExistentException passed to throws() does not exist.
it('fails', function () { ... })->throws(stdClass::class);
// ✘ throws() expects a Throwable class, got stdClass.
it('covers something', function () { ... })->coversClass('App\Nonexistent\Service');
// ✘ Class App\Nonexistent\Service referenced in coversClass() does not exist.
describe('UserService', function () {
beforeEach(fn () => null);
// ✘ describe() block 'UserService' contains no tests.
});