This repository contains test cases and simple, and deep explanations for the most common features of Mockery/Mockery. Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec, or any other testing framework. It is also the default mock object framework for Laravel.
This repository is designed to explain and test some basic concepts and features of Mockery. Inside the ./tests
directory, you'll find ordered files that explain and test Mockery's features. You can easily run the tests, modify them, and observe the results.
- Clone the repository to your local machine:
git clone https://github.com/amyavari/php-mockery-examples-and-explanations.git
- Use Composer to install the required dependencies by running the following command:
composer install
It will install mockery/mockery
, phpunit/phpunit
, laravel/pint
and symfony/var-dumper
.
-
Explore the
./tests
directory, where each file corresponds to a specific section of Mockery official documentation. Each test file contains multiple test cases which the method names represents the concept it will explain and test.- Each test includes doc (
/* */
) comments that explain the concept - You can run a specific test to observe its behavior using:
vendor/bin/phpunit --filter=<TEST_METHOD_NAME>
- Feel free to modify the tests as instructed in the doc comments or as needed to explore different scenarios
- Don't forget to check
./src
directory to understand the code being tested
- Each test includes doc (
-
Check the output in your command-line interface, including errors, warnings, and
dump
outputs.
Each section below links to the relevant part of the Mockery documentation and the corresponding test file in this repository:
- Creating a Mock object →
A_MakingMock_Test.php
- Creating a Spy object →
A_MakingMock_Test.php
- Creating a Partial Mock →
A_MakingMock_Test.php
- Behavior Modifiers →
A_MakingMock_Test.php
- Mock Final Classes/Methods →
A_MakingMock_Test.php
- Declaring Method Call Expectations →
B_Expectations_Part1_Test.php
- Declaring Method Argument Expectations →
B_Expectations_Part1_Test.php
- Declaring Return Value Expectations →
B_Expectations_Part2_Test.php
- Setting Public Properties →
B_Expectations_Part2_Test.php
- Declaring Call Count Expectations →
B_Expectations_Part3_Test.php
- Multiple Calls with Different Expectations →
B_Expectations_Part3_Test.php
- Expectation Declaration Utilities →
B_Expectations_Part3_Test.php
- Argument Validation →
C_Argument_Validation_Test.php
- Alternative shouldReceive Syntax →
D_Alternative_Should_Test.php
- Preserving Pass-By-Reference Method Parameter Behavior →
E_Other_Features_Test.php
- Mocking Demeter Chains And Fluent Interfaces →
E_Other_Features_Test.php
- PHP Magic Methods →
E_Other_Features_Test.php
To see updates, what has changed/added, and when, you can check the CHANGELOG.md file.
Of course, this repository cannot replace the official documentation and does not cover all of PHP Mockery's features. To fully understand Mockery's capabilities, we highly encourage you to read the official documentation. Use this repository as a supplementary resource with simple explanations and prepared tests.
Thank you for considering contributing to this repository! If you find any mistakes or want to add more tests, explanations, or anything else that might be useful for others, your pull requests are highly welcome.
PHP Mockery Examples and Explanations was created by Ali Mohammad Yavari under the MIT license.