R
R
rumasterov2016-12-04 15:34:12
PHP
rumasterov, 2016-12-04 15:34:12

How to properly test such a class?

Hello.
There is the following class:

<?php

class ProductImporter 
{
    private function loadProductListFromExternalSource()
    {
        // обращение к внешнему сервису для загрузки товаров, возвращает массив товаров
    }
  
    private function saveProduct($product)
    {
        // сохранение товара из внешнего сервиса в локальную базу данных
    }
  
    public function import()
    {
        $products = $this->loadProductListFromExternalSource();
  
        foreach ($products as $product) {
            $this->saveProduct($product);
        }
    }
}

How to properly test such a class? Test each method separately? Or test only the import method and then check that there are products in the database?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xfg, 2016-12-04
@rumasterov

You only need to test the public methods of the class. In general, it is better to implement external services that are responsible for saving and unloading to the constructor of this class, block the work with the base / unload and write a clean unit test. Such tests will be fast, short and easy to maintain. Then you can write several slow functional tests for the most important functionality of the site. And that's it, that's enough. Now you want to write an integration test. Ugly, slow, with a bunch of extra boilerplate. This makes very little sense. You need to write unit tests for your code and write functional tests only for important features such as payment or registration. Do not write integration tests at all. Remember, every extra line of code you write requires your continued support in the future. We write tests

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question