Home >Backend Development >PHP Tutorial >Laravel Tutorial: Using Stub to Resolve Dependencies in Unit Tests

Laravel Tutorial: Using Stub to Resolve Dependencies in Unit Tests

巴扎黑
巴扎黑Original
2017-08-16 09:31:242126browse


Summary: I have known about the concept of unit testing for a long time, and I have tried it. Slowly, my understanding of unit testing and PHPUnit has become clearer, and Start practicing unit testing slowly. As we all know about dependencies in Laravel, Laravel uses IoC, and therefore the various modules are decoupled. And it is precisely because of this...

I have known about the concept of unit testing for a long time, and I have tried it. Gradually, my understanding of unit testing and PHPUnit became clearer, and I began to slowly to practice unit testing.

Dependencies in Laravel

We all know that Laravel uses IoC, and therefore the various modules are decoupled. And it is precisely because of this that it becomes easier when we write unit tests in Laravel.

Give a chestnut

Consider the following scenario. During development, we may add a Repository between the controller and the model to process data. Then our Controller will depend on Respository. Using Laravel's IoC, we can define a Service Provider to centrally inject Respository into the container.

Suppose we now have such a Repository, which records product information. We want to obtain a certain product information in the Controller and then execute some business logic.

Class GoodRepository{    public function getGoodById($goodId)
    {        // TODO: Get good by its id.
    }
}class GoodController extends Controller{    public function show($id, GoodRepository $goodRepository)
    {        // TODO: Do something with good info from that repository.
    }
}// In route/api.phpRoute::get('/api/good/{id}', 'GoodController@show');// Create a RepositoriesServiceProvider in Provider/RepositoriesServiceProvider.php。// And inject the GoodRepository into Container.class RepositoriesServiceProvider extends ServiceProvider{    public function boot()
    {
    
    }    
    public function register()
    {        $this->app->singleton(GoodRepository::class);
    }
}

Okay, we can find that GoodController is dependent on GoodRepository, and GoodRepository is dependent on the data in the database. But when we do unit testing, we hope to generate as few dependencies as possible. Therefore, we should hope to be able to control the data returned by GoodRepository.

In Laravel, the $this->get('/path/to/route'); method is provided to test HTTP requests. This test will inevitably involve the dependencies just mentioned. How to solve this dependency problem, we can ask our protagonist—stubware.

Stubbing

The practice of replacing an object with a test double that (optionally) returns a configured return value is called stubbing.

This is the explanation from the PHPUnit documentation. My understanding is that the so-called stub is to simulate the behavior of a dependent class so that what this behavior does is under our own control. For example, in the above situation, we hope to simulate the getGoodById method of GoodRepository to return the same value as the real return structure without relying on external data sources.

Using stubs in Laravel

We registered the GoodRepository singleton through Service Provider, so according to this idea, we are writing unit tests , we can register the stub we defined as a GoodRepository singleton.

class GoodControllerTest extends TestCase{    public function testShow()
    {
        $data = []; // The data returns from GoodRepository::getGoodById.
        
        $stub = $this->createMock(GoodRepository::class);
        
        $stub->method('getGoodById')->will($this->returnValue($data));        
        $this->app->singleton(GoodRepository::class, function () use ($stub) {            return $stub;
        });
        
        $response = $this->get('/api/good/1');        
        // Some assertions.
    }
}

我们通过在这里将桩件 $stub 用单例模式注册给了 Container,在调用 $this->get('/api/good/1'); 时原本在 Controller 中的 GoodRepository 依赖就变成了我们自定义的桩件 $stub。我们将 $data 定义为和返回值相同的结构,注册到桩件中。这样,所有的数据都在我们可控的范围了。

如果我们在这里不使用桩件,而是直接依赖外部(数据库)中的数据,那么如果 id 为 1 的数据被删除了,我们是不是就要改成 2 了呢?我们是不是就要重新计算数据了匹配断言了呢?这样的测试,可靠性便大大降低。

Postscript

For any reliable system, unit testing is essential. Fortunately, PHPUnit provides us with useful unit tests. What this article talks about is just a drop in the bucket of PHPUnit. And I myself am slowly exploring and practicing. Let’s encourage you together.

The above is the detailed content of Laravel Tutorial: Using Stub to Resolve Dependencies in Unit Tests. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn