PHP code example of laragear / meta-testing

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

    

laragear / meta-testing example snippets


public function test_has_service_registered(): void
{
    $this->assertHasServices('my-cool-service');
}

use Orchestra\Testbench\TestCase
use Laragear\MetaTesting\InteractsWithServiceProvider;

class ServiceProviderTest extends TestCase
{
    use InteractsWithServiceProvider;
    
    public function test_is_registered_as_singleton(): void
    {
        $this->assertHasSingletons(\Vendor\Package\MyService::class);
    }
}

public function test_something_important(): void
{
    // Get a service from the Service Container, optionally run over a callback.
    $cache = $this->service('cache', fn ($cache) => $cache->set('foo', 'bar', 30));
    
    // Run a service once and forgets it, while running a callback over it.
    $compiler = $this->serviceOnce('blade.compiler', fn($compiler) => $compiler->check('cool'));
    
    // Executes a callback over a REAL service when already mocked.
    $this->unmock('files', function ($files): void {
        $files->copyDirectory('foo', 'bar');
    });
}

use Laragear\MetaTesting\Validation\InteractsWithValidator;

public function test_validation_rule(): void
{
    // Assert the validation rule passes.
    $this->assertValidationPasses(['test' => 'foo'],['test' => 'my_rule']);
    
    // Assert the validation rule fails.
    $this->assertValidationFails(['test' => 'bar'],['test' => 'my_rule']);
}

use Illuminate\Http\Request;
use Vendor\Package\Http\Middleware\MyMiddleware;
use Laragear\MetaTesting\Http\Middleware\InteractsWithMiddleware;

public function test_middleware(): void
{
    $response = $this->middleware(MyMiddleware::class)->using(function (Request $request) {
        // ...
    })->post('test', ['foo' => 'bar']);
    
    $response->assertOk();
}

$this->middleware(MyMiddleware::class, 'test_argument')
    ->withUnencryptedCookie()
    ->be($this->myTestUser)
    ->post('test/route', ['foo' => 'bar'])
    ->assertSee('John');

public function test_form_request()
{
    $this->formRequest(MyFormRequest::class, ['foo' => 'bar'])->assertOk();
}

public function test_authorization()
{
    $admin = User::factory()->admin()->create();
    
    $this->assertUserCan($admin, 'viewDashboard');
}

public function test_cast()
{
    $this->cast(MyTestCast::class)
        ->assertCastFrom(null, new Cast)
        ->assertCastTo('{"foo":"bar"}', new Cast(['foo' => 'bar']));
}

use App\Models\User;

public function test_builder()
{
    $this->mockQueryFor(User::class)->whereKey(1)->and()->get()->shouldReturn(null);
    
    $result = User::query()->whereKey(1)->get();
    
    $this->assertNull($result);
}

use App\Models\User;
use Mockery\MockInterface;

public function test_builder()
{
    $this->mockQueryFor(User::class, function (MockInterface $mock) {
        $mock->expects('find', 1)->andReturnNull();
    });
    
    $this->assertNull(User::find(1));
}

use App\Models\User;
use Mockery\MockInterface;
use Laragear\MetaTesting\Eloquent\PendingBuilderTestProxy;

public function test_builder()
{
    // $mock = $this->mockQueryFor(User::class)->mock();

    $this->mockQueryFor(User::class, function (MockInterface $mock) {
        $mock->expects('find', 1)->andReturnUsing(function () {
            $this->unmockQueryFor(User::class);
            
            return null;
        });;
    });
    
    $this->assertNull(User::find(1));
    
    $this->assertNotNull(User::find(1))
}

public function test_pipeline()
{
    $pipeline = $this->pipeline(MyPipeline::class)
        // Check the order of pipes
        ->assertPipes([
            FirstPipe::class,
            SecondPipe::class,
            ThirdPipe::class,
        ])
        // Check all pipes have their handler method.
        ->assertVia('handle');

    // Mock the pipeline to be used elsewhere in your application
    $pipeline->mock(function ($mock) {
        $mock->expects('send')->andReturnSelf();
    })

    // Test the passable through all pipes and assert its result.         
    $pipeline->send(new Passable(['value' => 10]))
        ->assertPassable(function (Passable $passable) {
            return $passable->value === 130;
        })
        
    // Test a pipe in isolation and assert the results.
    $pipeline->isolatePipe(SecondClass::class)
        ->send(new Passable(['value' => 10]))
        ->assertPassable(function (Passable $passable) {
            return $passable->value === 100;
        });
        
    // Mock services a pipe or pipeline