phpunit/phpunit-mock-objects
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
doctrine/instantiator | 1.5.0 | - | MIT | prod | |
phpunit/php-text-template | 1.2.1 | 3.86 kB | BSD-3-Clause | prod | |
sebastian/exporter | 3.1.6 | - | BSD-3-Clause | prod |
PHPUnit's Mock Object Library, also known as phpunit/phpunit-mock-objects, is a built-in feature that contributes significantly to PHPUnit's core functionality. Its chief role is to generate mock objects to assist developers in carrying out unit tests. This adds a layer of abstraction to the testing process, simulating the behaviour of real objects in a controlled way. The library aids in avoiding any unexpected side effects or external dependencies that might otherwise appear when using real objects.
The phpunit-mock-objects library is used extensively within PHPUnit to create mock, stub, or dummy objects for testing purposes. With this library, you can easily mock specific methods of your classes and set precise return values or expected invocations. For instance, if you have a class named SampleClass
and you want to mock it, your code would look like this:
$mock = $this->getMockBuilder('SampleClass')
->setMethods(['method1', 'method2'])
->getMock();
$mock->method('method1')
->willReturn('Return value of method1');
$mock->expects($this->any())
->method('method2')
->will($this->returnValue('Return value of method2'));
In this example, $mock
is a mocked object of SampleClass
. The methods method1
and method2
are being mocked where 'method1' will always return 'Return value of method1' and 'method2' will return 'Return value of method2' when called any number of times.
The phpunit-mock-objects package does not have dedicated docs; however, comprehensive documentation about Mock Objects can be found directly within the PHPUnit manual. Users can explore the PHPUnit manual at PHPUnit.de, which offers detailed explanations and examples on how to create and use mock objects in PHPUnit.