Answer the question
In order to leave comments, you need to log in
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);
}
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question