ekvedaras/laravel-test-helpers

This package is abandoned and no longer maintained. No replacement package was suggested.

Various helpers for writing tests for Laravel applications

v1.1.0 2018-09-26 11:43 UTC

This package is auto-updated.

Last update: 2022-02-01 13:14:22 UTC


README

Latest Version Build Status Quality Score Coverage Status StyleCI Total Downloads

When writing tests daily, some actions might get frustrating to repeat. This package offers a solution to some of those problems and helps to write tests faster and make them more clean. It does not enforce anything. The main way of building mocks and expectations can still be used where it makes sense to.

Changelog

Installation

composer require ekvedaras/laravel-test-helpers --dev

Usage

Just use TestHelpers trait (or any of others provided if you want to be explicit) in your test class.

Helpers

TestHelpersMock

Providers helper methods for defining PhpUnit mock expectations more quickly.

NOTE: To create this mock BuildsMocks@mock has to be used

  • once
  • twice
  • times
  • any
  • consecutiveTwice
  • consecutive
  • never
  • fail

BuildsMocks trait

Creates mocks and injects them into Laravel, so mocks would be resolve instead of real instances using app(My::class) and when container auto resolves injector(s) constructor arguments.

// Creates PhpUnit mock wrapped with TestHelpersMock 
$this->mock($mockClass, $injectorClass, $methods, $constructorArgs, $onlyForInjector);

// Creates Mockery mock
$this->mockery($mockClass, $injectorClass, $onlyForInjector);

// Creates spy mock
$this->spy($mockClass, $injectorClass, $onlyForInjector);

NOTE: Only the first parameter is required

ChecksSingletons trait

Checks if given class is marked as a singleton.

$this->assertSingleton(My::class);

TestsCommands trait

Creates command tester for a given command class.

$tester = $this->getCommandTester(MyCommand::class);
$tester->execute($input);
$out = $tester->getDisplay();

TestsHelpers trait

Just a convenient trait which includes all traits mentioned above.

Examples

More examples can be found in packages tests.

Mock expectations with TestHelpersMock

use BuildsMocks;

$mock = $this->mock(My::class);


// Using helpers
// Equivalent


$mock->once('someMethod');
$mock->expects($this->once())->method('someMethod');

$mock->once('someMethod', $foo, $bar);        
$mock->expects($this->once())->method('someMethod')->with($foo, $bar);

$mock->twice('someMethod', $foo, $bar);
$mock->expects($this->exactly(2))->method('someMethod')->with($foo, $bar);

$mock->times(3, 'someMethod', $foo, $bar)->willReturn(true);
$mock->expects($this->exactly(3))->method('someMethod')->with($foo, $bar)->willReturn(true);

$mock->any('someMethod', $foo, $bar);
$mock->expects($this->any())->method('someMethod')->with($foo, $bar);

$mock->consecutiveTwice('someMethod', [$foo], [$bar]);
$mock->expects($this->exactly(2))->method('someMethod')->withConsecutive([$foo], [$bar]);

$mock->consecutive(3, 'someMethod', [$foo], [$bar], [$foo, $bar]);
$mock->expects($this->exactly(3)->method('someMethod')->withConsecutive([$foo], [$bar], [$foo, $bar]);

$mock->never('someMethod');
$mock->expects($this->never())->method('someMethod');

$mock->fail('someMethod', new \Exception());
$mock->expects($this->any())->method('someMethod')->willThrowException(new \Exception());

$mock->fail('someMethod', new \Exception(), $foo, $bar);
$mock->expects($this->any())->method('someMethod')->with($foo, $bar)->willThrowException(new \Exception());