In Unit Testing, mocking is the most important part of testing a single unit is that you want to simulate the surrounding around it which is the dependencies of that class.
For example : If you’re testing a single unit that needs to process some output of a database, you don’t want to perform an actual query on a live database, you’d rather mock the database dependency to make sure that for your test it will always return a certain set of data.
Benefits of mock objects
- The unit test will run quickly in case of mocking databases, file systems, or external services.
- Useful for testing external objects that produce nondeterministic results.
PHP Unit provides multiple methods that will automatically create mock objects which will replace the original object in a unit test.
- createMock :
It uses the getMockBuilder method to create a test double for the specified class. It disabled the constructor of the original class.
- createPartialMock :
The createPartialMock method is very similar to createMock but it lets you specify an array with a subset of methods to be mocked (createMock doesn’t mock any method by default). This is especially useful in Magento 2 when mocking factories since you will need the create
method of the factory to be mocked.
- createConfiguredMock :
Similar to createMock but accepts a configuration array with a list of methods and their corresponding return values. Internally, it sets the willReturn
value for the methods specified.
- getMockBuilder :
getMockBuilder will mock the object using its fluent interface.
- getMockForAbstractClass :
It uses the getMockBuilder method to create a test double for the specified class. It disables the constructor of the original class and uses for abstract classes or Interfaces.
After mocking we will know how to use the mocked object expects and its return types.
In the above example, the method expects has different types of parameters. It is given below.
- $this->any(): It is for expecting a method many times.
- $this->once() : it is for expecting a method only one time.
- $this->atLeastOnce() : It is for expecting a method at least one time.
- $this->exactly(3): It is for expecting a method exactly 3 times or you can pass the value as per your requirement.
- $this->never(): When expects method takes never as a parameter it will not use that method.
Thank’s for reading this. If you have any queries please comment below.