PHP code example of rasuvaeff / property-testing-phpunit

1. Go to this page and download the library: Download rasuvaeff/property-testing-phpunit 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/ */

    

rasuvaeff / property-testing-phpunit example snippets


use PHPUnit\Framework\TestCase;
use Rasuvaeff\PropertyTesting\Gen;
use Rasuvaeff\PropertyTesting\PhpUnit\PropertyTesting;

final class SortPropertyTest extends TestCase
{
    use PropertyTesting;

    public function testSortIsIdempotent(): void
    {
        $this->forAll(['values' => Gen::arrayOf(Gen::int())])
            ->runs(300)
            ->check(static function (array $values): void {
                sort($values);
                $once = $values;
                sort($values);

                self::assertSame($once, $values);
            });
    }
}

$this->forAll()
    ->auto()
    ->check(
        /**
         * @param int<1, 300> $base
         * @param int<1, 86400> $cap
         */
        function (int $base, int $cap): void {
            self::assertLessThanOrEqual(86_400, $cap);
        },
    );

$this->forAll(['multiplier' => Gen::floatBetween(1.0, 4.0)])
    ->auto()
    ->check(
        /** @param int<1, 40> $attempt */
        function (float $multiplier, int $attempt): void { /* … */ },
    );

$this->forAll(['values' => Gen::arrayOf(Gen::int())])
    ->id('sort::idempotent')
    ->runs(300)
    ->check(static function (array $values): void { /* … */ });

$this->forAll(['width' => Gen::intBetween(1, 5000), 'minWidth' => Gen::intBetween(2, 5000)])
    ->runs(200)
    ->throws(ImageUploadException::class)
    ->check(static function (int $width, int $minWidth): void {
        $validator->validate(imageWithWidth($width), profileWithMinWidth($minWidth));
    });
bash
vendor/bin/phpunit examples/SortPropertyTest.php