Add mocking capabilities to Pest or PHPUnit

Overview

This repository contains the Pest Plugin Mock. The Mocking API can be used in regular PHPUnit projects. For that, you just have to run the following command in your terminal:

composer require pestphp/pest-plugin-mock --dev

If you want to start testing your application with Pest, visit the main Pest Repository.

Pest was created by Nuno Maduro under the Sponsorware license. It got open-sourced and is now licensed under the MIT license.

You might also like...
A Pest plugin to control the flow of time
A Pest plugin to control the flow of time

This Pest plugin offers a function testTime that allows you to freeze and manipulate the current time in your tests.

Integrates Pest with PHP-VCR using plugins.

Pest plugin for PHP-VCR Integrates Pest with PHP-VCR using plugins. Installation You can install the package via composer: composer require phpjuice/p

The Pest Parallel Plugin

This repository contains the Pest Plugin Parallel. If you want to start testing your application with Pest, visit the main Pest Repository. Explore th

Wraps your Pest suite in a Laravel application instance, allowing global use of the framework in tests.

Pest Larastrap Plugin This is currently a highly experimental project and is subject to large pre-release changes. Pest PHP is an awesome PHP testing

Pest is an elegant PHP Testing Framework with a focus on simplicity
Pest is an elegant PHP Testing Framework with a focus on simplicity

Pest is an elegant PHP Testing Framework with a focus on simplicity. It was carefully crafted to bring the joy of testing to PHP. Explore the docs: pe

Enforce consistent styling for your Pest PHP tests

A set of PHP CS rules for formatting Pest PHP tests.

This plugin adds basic HTTP requests functionality to Pest tests, using minicli/curly

Curly Pest Plugin This plugin adds basic HTTP requests functionality to Pest tests, using minicli/curly. Installation composer require minicli/pest-pl

Prevent none-test output in your Pest tests.
Prevent none-test output in your Pest tests.

Pest Plugin Silence Often, when writing tests, we echo and dump test code to debug and check everything is working correctly. It can be easy to forget

Given When Then (GWT) Plugin for Pest

Given When Then (GWT) Plugin for Pest A simple API allows you to structure your tests focused on the behaviour. Given-When-Then separation makes the t

Comments
  • PHP7.4 Support

    PHP7.4 Support

    It doesn't look like there's any PHP8 specific in this codebase, so why not also enable support for PHP7.4? Otherwise the userbase for this is quite limited.

    I haven't tested this out yet, I wanted to discuss this first, but there's no Issues or Discussions tab for this project and thus no way to communicate with the maintainers. So I created this PR for the discussion.

    opened by fabis94 4
  • Allows mocks to inherit public methods from other objects

    Allows mocks to inherit public methods from other objects

    Hey!

    So, this is one that I find super useful but completely understand if you're not a fan. Usually in larger projects, a mocked interface might have multiple public methods that get called in a feature test. You're only interested in testing one of those methods, but still need to provide a fallback for the other methods.

    You might think "that's a partial mock", but I beg to differ because the partial mock might still be doing too much and may still require you to mock the methods/dependencies out.

    This PR adds the idea of inheriting public class methods for a mock. You pass it a class, and it will look up its public methods and defer to those if no hard expectation has been set.

    Example

    First of all, we create an object. It doesn't have to implement anything, but it can if we want to force it to have all of the methods of the interface we're testing.

    class BackupClass implements SomeInterface {
        public function get() {
            return 'Hello World';
        }
    
        public function post() {
            return 'Foo Bar';
        }
    }
    

    Now we write our test. We can call methods without using expect, and it will use the method call from the inherited class instead.

    $mock = mock(Http::class)->inherit(new BackupClass);
    
    expect($mock->post('get'))->toBe('Hello World');
    

    However, if we use an extend call, it will opt for our overridden version:

    $mock = mock(Http::class)
        ->inherit(new BackupClass)
        ->extend(post: fn() => "Hey Nuno!");
    
    expect($mock->post('get'))->toBe('Hey Nuno!');
    

    This gets even more powerful in feature tests, where other methods on the interface might be being called, but you're not interested in testing them at this moment.

    $mock = mock(Http::class)
        ->inherit(new BackupClass)
        ->extend(post: fn() => "Hey Nuno!");
    
    $this->swap(Http:class, $mock);
    
    expect(someFeatureThatCallsGetButReturnsPost())->toBe('Hey Nuno!');
    

    This implementation also allows you to provide multiple inherited classes, so you could mix and match methods together based on the use case required for the given test.

    Let me know what you think. Hope you're having a good week!

    opened by lukeraymonddowning 1
  • Fixes a bug with parameter names on mocks.

    Fixes a bug with parameter names on mocks.

    Howdy 👋🏼

    Previously, arguments in the magic __call method were being passed through as the captured array rather than varadically being forwarded, which was causing long-form expectations to fail. This PR fixes the issue and adds a regression test.

    opened by lukeraymonddowning 0
Owner
PEST
Pest is an elegant PHP Testing Framework with a focus on simplicity.
PEST
PHPUnit to Pest Converter

PestConverter PestConverter is a PHP library for converting PHPUnit tests to Pest tests. Before use Before using this converter, make sure your files

null 10 Nov 21, 2022
Provides generic data providers for use with phpunit/phpunit.

data-provider Installation Run composer require --dev ergebnis/data-provider Usage This package provides the following generic data providers: Ergebni

null 25 Jan 2, 2023
Qase-phpunit - Qase TMS PHPUnit reporter.

Qase TMS PHPUnit reporter Publish results simple and easy. How to integrate composer require qase/phpunit-reporter Example of usage The PHPUnit report

Qase TMS 6 Nov 24, 2022
Mockery - Designed as a drop in alternative to PHPUnit's phpunit-mock-objects library

Mockery Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its c

Mockery 10.3k Jan 1, 2023
PHP Mocking Framework

Phake Phake is a framework for PHP that aims to provide mock objects, test doubles and method stubs. Phake was inspired by a lack of flexibility and e

Phake 469 Dec 2, 2022
Highly opinionated mocking framework for PHP 5.3+

Prophecy Prophecy is a highly opinionated yet very powerful and flexible PHP object mocking framework. Though initially it was created to fulfil phpsp

PHPSpec Framework 8.5k Jan 3, 2023
A PHP library for mocking date and time in tests

ClockMock Slope s.r.l. ClockMock provides a way for mocking the current timestamp used by PHP for \DateTime(Immutable) objects and date/time related f

Slope 44 Dec 7, 2022
PHP Mocking Framework

Phake Phake is a framework for PHP that aims to provide mock objects, test doubles and method stubs. Phake was inspired by a lack of flexibility and e

Phake 469 Dec 2, 2022
Removes final keywords from source code on-the-fly and allows mocking of final methods and classes

Removes final keywords from source code on-the-fly and allows mocking of final methods and classes. It can be used together with any test tool such as PHPUnit or Mockery.

David Grudl 326 Dec 9, 2022
⛽ Pest plugin to test Laravel applications powered by Octane.

⛽ Laravel Octane (Pest Plugin) Pest plugin to test Laravel applications powered by Octane. Install Via Composer composer require --dev cerbero/pest-pl

Andrea Marco Sartori 21 Apr 5, 2022