PHP unit testing advanced (8) - core technology - stub (stub) - attribute injection stub

PHP unit testing advanced (8) - core technology - stub (stub) - attribute injection stub

The main code and text of this series of articles come from "The Art of Unit Testing", the original author: Roy Osherove. Translator: Jin Ying.

This series of articles has been adapted according to the syntax and usage habits of php. All codes are tested locally. If reproduced, please indicate the source.
The previous article introduced how to inject stubs with the construction method, and the code is particularly easy to understand. However, the disadvantage is that the original design is modified, and the construction method is modified to modify the code intent. At the same time, if there are too many piles, the code will be particularly ugly.
It can be solved with a dependency injection library such as pimple, but it is still not good.

This article introduces the use of property acquisition and setting methods to inject stubs, and the code is easy to read and write. In fact, this method is not much different from constructor injection.
An interface and its two implementations do not need to change the code. The classes that need to be modified are the class under test, the log analyzer class, and the test class.
Source code

(1) Class under test under t2\application\index\controller, log analyzer
LogAnalyzer.php
<?php
namespace app\index\controller;

/**
 * The log analyzer class is also the class under test
 *
 * Note that this is an example with property injection.
 */
class LogAnalyzer
{
    /**
     * @var IExtensionManager
     */
    private $manager;

    public function __construct()
    {
        $this->manager = new FileExtensionManager();
    }

    public function setManager($mgr)
    {
        $this->manager = $mgr;
    }

    public function getManager()
    {
        return $this->manager;
    }

    /**
     * To determine whether the file name is valid, call another class to implement
     * @param string $filename
     */
    public function isValidLogFileName($filename)
    {
        return $this->manager->isValid($filename);
    }
}

Test code

(2) Under t2\tests\index\controller\, test class, inject stub
LogAnalyzerTest.php with attributes
<?php
namespace tests\index\controller;

/**
 * class for testing
 */
class LogAnalyzerTest extends \think\testing\TestCase
{

    /**
     * @test
     * Test using the method of property injection stubs
     * Note that it is very important to make the test method names as meaningful as possible to facilitate the maintenance of the test code. regular
     */
    public function isValidFileName_NameSupportedExtension_ReturnTrue()
    {
        // prepare a stub that returns true
        $myFakeManager = new FakeExtensionManager();
        $myFakeManager->willBeValid = true;

        //Start creating the object of the class under test, ready to test
        $analyzer = new \app\index\controller\LogAnalyzer();
        $analyzer->setManager($myFakeManager); // property injection
        $result = $analyzer->isValidLogFileName("short.ext");
        $this->assertTrue($result);
    }
}

The test under cmd passed.

Previous: Advanced PHP Unit Testing (7) - Core Technology - Stub - Constructor Injection Stub
Next: Advanced PHP Unit Testing (9) - Core Technology - Stub - Factory class injection stub

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327074921&siteId=291194637