PHP code example of codetetic / pest-plugin-prophecy

1. Go to this page and download the library: Download codetetic/pest-plugin-prophecy 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/ */

    

codetetic / pest-plugin-prophecy example snippets


// Pest.php

uses(Pest\Prophecy\ProphecyTrait::class)->in('Unit');

use function Pest\Prophecy\prophesize;
use function Pest\Prophecy\reveal;

class Example
{
}

it('can be accessed as a function', function (): void {
    $prophecy = prophesize(Example::class);

    expect($prophecy)
        ->toBeInstanceOf(Prophecy\Prophecy\ObjectProphecy::class);

    expect($prophecy->reveal())
        ->toBeInstanceOf(Example::class);
});

it('can be accessed as a function and use reveal helper', function (): void {
    expect(prophesize(Example::class))
        ->toBeInstanceOf(Prophecy\Prophecy\ObjectProphecy::class);

    expect(reveal(Example::class))
        ->toBeInstanceOf(Example::class);
});

use function Pest\Prophecy\argument;
use function Pest\Prophecy\autowire;
use function Pest\Prophecy\exact;

class ExampleAutowire
{
    public function __construct(
        public string $string,
    ) {
    }

    public function string(string $string): string
    {
        return $string;
    }
}

final class ExampleAutowireWrapper
{
    public function __construct(
        public ExampleAutowire $object,
    ) {
    }

    public function string(string $string): string
    {
        return $this->object->string($string);
    }
}

it('can be autowired', function (): void {
    $object = autowire(ExampleAutowireWrapper::class);

    expect(argument('object'))
        ->toBeInstanceOf(Prophecy\Prophecy\ObjectProphecy::class);

    argument('object')
        ->string(exact('format'))
        ->shouldBeCalled()
        ->willReturn('result');

    expect($object)
        ->toBeInstanceOf(ExampleAutowireWrapper::class);

    expect($object->object)
        ->toBeInstanceOf(ExampleAutowire::class);

    expect($object->string('format'))
        ->toBe('result');
});

use function Pest\Prophecy\prophesize;
use function Pest\Prophecy\reveal;
use function Pest\Prophecy\allOf;
use function Pest\Prophecy\exact;
use function Pest\Prophecy\type;

class Example
{
    public function __construct(
        public string $string,
    ) {
    }

    public function string(string $string): string
    {
        return $string;
    }
}

it('can be asserted with allOf()', function (): void {
    prophesize(Example::class)
        ->string(allOf(exact('format'), type('string')))
        ->shouldBeCalled()
        ->willReturn('result');

    expect(reveal(Example::class)->string('format'))
        ->toBe('result');
});

it('can be asserted with allOf()', function (): void {
    prophesize(Example::class)
        ->string(allOf(exact('format'), type('string')))
        ->shouldBeCalled()
        ->willReturn('result');

    expect(reveal(Example::class)->string('format'))
        ->toBe('result');
});

// More tests demonstrating the use of different argument tokens like `any`, `exact`, `in`, `notIn`, `size`, `withEntry`, etc.