laradev / test-support
Laravel development test support
Requires
- php: ~7
- illuminate/container: ~5
- illuminate/support: ~5
- mockery/mockery: ~0.9
- phpunit/phpunit: ~5
This package is auto-updated.
Last update: 2024-11-13 15:16:39 UTC
README
This package provides helpers and utilities to facilitate testing of Laradev packages.
Install
composer require --dev laradev/test-support
Usage
Create a test case
Simply extend the class Laradev\Test\Support\TestCase
.
namespace Example use Laradev\Test\Support\TestCase; final class ExampleClassTest extends TestCase { /** implement abstract methods **/ }
Utilities
The TestCase
embeds a bunch of utilities in the form of traits.
Even if they can be used independently, it is more reliable to extends the TestCase
class.
Assertions
The Assertions
trait enhances the TestCase
class with new assertions directly accessible to $this
.
assertIsSubclassOf(string $expectedParent, string $actual, string $message = '')
: asserts that the actual class is a sub class of a given one.assertBootMergesConfigForProvider(string $providerClass, string $configfile, string $message = '')
: asserts that theboot
method of a given service provider instance merges the configuration of its package with the main application one.assertBootPublishesConfigForProvider(string $providerClass, string $configfile, string $message = '')
: asserts that theboot
method of a given service provider instance publishes the configuration file to the application configuration path.
MockProvider
The MockProvider
trait offers a set of factory methods to facilitate the creation of mocks of the main classes of Laravel.
Factory methods
newMock($whatToMock = null)
: returns an instance of$whatToMock
or ofMockery\MockInterface
if the argument value is null.newAppMock()
: returns an instance ofIlluminate\Contracts\Foundation\Application
.newConfigMock()
: returns an instance ofIlluminate\Contracts\Config\Repository
.newFunctionMock(string $functionName)
: should be used to create a mock of a function, it returns an instance ofMockery\CompositeExpectation
.newAppContainerWithConfigMock()
: returns an instance ofIlluminate\Contracts\Container\Container
containing a config mock instance that is accessible using the keyconfig
.
As the mock engine behind the scene is Mockery, all these instances implement the Mockery\MockInterface
or Mockery\ExpectationInterface
and then can be enhanced with expectations.
Other methods
releaseMocks()
: releases all the mocks, does some cleanup.useFunction(string $functionName, ...$args)
: static method to call as the body of a mocked function (see MockProviderTest::testMockingFunctions() for an example on how to do this).
Note:
While extending theTestCase
abstract class, thereleaseMocks
method is automatically called at the end of each test in thetearDown
method.
If you intend to use theMockProvider
trait directly, it is important to note that you will need to call thereleaseMocks
method by yourself.
License
This project is licensed under the terms of the MIT License