Skip to content
Doug Wilbourne edited this page Jul 1, 2023 · 6 revisions

Installation

Install this library using composer in the require-dev section of your composer.json file:

"require-dev": { "pvc/testingutils": "*" }

Usage

At the moment, the testing utilities consist solely of testing traits.

Testing traits are meant to assist in certain testing situations where you want to add certain sets of behavior (traits) to your mocks without writing the code from scratch.

Maybe it is obvious, but note that your mock must have the interface appropriate for the methods of the trait you are using. For example, let's say you want to implement array access for a mocked object. The underlying object being mocked must be defined as implementing ArrayAccess.

There are several traits available, as follows:

MockeryArrayAccessTrait. Allows your mock to exhibit behavior like an array. You can use syntax like $a = $mock[$key], $mock[$key] = 'foo', etc.

MockeryIteratorTrait. Sets up your mock to exhibit Iterator interface, e.g. foreach($mock as $item) { do something }

MockeryNonNegativeIntegerValidatorTrait. Creates a mock with an interface of 'Validator' and gives it a method called validate that returns true if the argument is greater than or equal to zero. This is admittedly pedantic: The code to produce the mock is exactly the same as the actual code for pvc\validator\numeric\ValidatorNonNegativeInteger, but the things we do to keep unit testing pure......

PrivateMethodTestingTrait. Using reflection, temporarily changes the visibility on the method so you can test it.

RandomStringGeneratorTestingTrait. Allows you to create random strings for seeding / testing. You can control the range of characters used in string creation as well as the length of the generated string.

Clone this wiki locally