G
G
Gleb Vas2018-04-02 00:27:49
Yii
Gleb Vas, 2018-04-02 00:27:49

How to test this class?

<?php 

namespace app\models;

use app\models\relations\Categories;

class MenuSite {
    
    //отправление дерева категорий пользователю
  public function getMenu() {
        $categoriesList = $this->categoriesList();
        $treeCategories = $this->buildingTreeOfCategories($categoriesList);
        return $treeCategories;
    }

    //получение списка категорий посредством SQL запроса
    private function categoriesList() {
        return $categoriesList = Categories::find()
            ->indexBy('id')
            ->asArray()
            ->all();
    }

    //формирование дерева с неограниченной вложенностью категорий
    private function buildingTreeOfCategories($categoriesList) {
        $treeCategories = [];
        foreach($categoriesList as $id => &$category) {
            if ($category['id_parent'] === '0') {
                $treeCategories[$id] = &$category;
            }
            $categoriesList[$category['id_parent']]  ['subcategories'][$category['id']] = &$category;
        }
        return $treeCategories;
    }
}

Good afternoon. I'm interested in the question of how to properly test a melon class.
The SQL query is implemented in a separate private method "categoryList", and actually I want to lock it up, but at the same time I don't want to break class encapsulation using the PUBLIC modifier, because private methods don't get wet.
Is there any way to hack the "Categories" class itself so that the entire "MenuSite" class, when referring to this "Categories" class, refers to the mock and not to the Categories class directly?
Or would I still have to make the "categoryList" method public for an adequate test?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question