Testování handle metody v komponentě

Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
tttpapi
Člen | 100
+
0
-

Mám komponentu pro vrácení smazaného zboží do košíku

public function handleReturn($id)
{
    if (!isset($this->section->deleted[$id]) || isset($this->section->books[$id]))
    {
        $this->template->deleted = $this->section->deleted;

	    ...
}

Kód sám o sobě není ani tak důležitý. V aplikaci vše funguje.
Pokud však pustím test, tak mi $this->shoppingCartList->handleReturn(3); skončí chybou.

-- FAILED: nette\tests\ShoppingCartListTest.phpt [method=testRemoveAndReturn]
   Exited with error code 255 (expected 0)
   Nette\InvalidStateException: Component '' is not attached to 'Nette\Applicat
on\UI\Presenter'.

   in src\ComponentModel\Component.php(80)
   in Application\UI\PresenterComponent.php(38) Nette\ComponentModel\Component-
lookup()
   in Application\UI\Control.php(67) Nette\Application\UI\PresenterComponent->g
tPresenter()
   in Application\UI\Control.php(51) Nette\Application\UI\Control->createTempla
e()
   in src\Utils\ObjectMixin.php(139) Nette\Application\UI\Control->getTemplate(

   in src\Utils\Object.php(125) Nette\Utils\ObjectMixin::get()
   in component\ShoppingCart\ShoppingCartList.php(147) Nette\Object->__get()
   in nette\tests\ShoppingCartListTest.phpt(58) App\Component\ShoppingCartList-
handleReturn()
   in [internal function]ShoppingCartListTest->testRemoveAndReturn()
   in src\Framework\TestCase.php(130) call_user_func_array()
   in src\Framework\TestCase.php(113) Tester\TestCase->runTest()
   in src\Framework\TestCase.php(51) Tester\TestCase->runMethod()
   in nette\tests\ShoppingCartListTest.phpt(250) Tester\TestCase->run()

Test vypadá následovně:

<?php

/**
 * @testCase ShoppingCartListTest
 */

use App\Component\ShoppingCartList;
use Tester\Assert;

$container = require_once __DIR__.'/bootstrap.php';

class ShoppingCartListTest extends Tester\TestCase
{
    public $shoppingCartList;
    public $session;

    public function __construct(\Nette\Http\Session $session)
    {
        $this->session = $session;
    }

    public function setUp()
    {
        parent::setUp();

        /* nastaveni prostredi pro testovani */
        $this->shoppingCartList = new ShoppingCartList($this->session);
    }

    public function tearDown()
    {
        parent::tearDown();

        /* uvolneni prostredku */
        $this->shoppingCartList = NULL;
        $this->session = NULL;
    }

    public function testRemoveAndReturn()
    {
        Assert::true($this->session->hasSection('ShoppingCart'));
        $sessionSection = $this->session->getSection('ShoppingCart');

        /* Vytvoreni 3 knih, kazda cena 100. */
        $sessionSection->books = $this->createTestArray(3, 100);
        $sessionSection->deleted = array();
        $sessionSection->price = $this->shoppingCartList->getFinalPrice();

        /* Pocatecni hodnoty. */
        Assert::count(3, $sessionSection->books);
        Assert::count(0, $sessionSection->deleted);
        Assert::equal(300, $sessionSection->price['finalPrice']);
        Assert::equal(330, $sessionSection->price['finalPriceDph']);

        /* Vraceni knihy, ktera nebyla smazana nic neudela. */
        $this->shoppingCartList->handleReturn(3);

        ...
    }

    /* Vytvoreni testovaciho pole pro naplneni kosiku knihami. */
    private function createTestArray($count = 0, $price = 100)
    {
        $books = array();

        for ($i = 1; $i <= $count; $i++)
        {
            $books[$i] = $this->createTestBook($i, $price);
        }

        return $books;
    }

    /* Vytvoreni testovaci knihy. */
    private function createTestBook($id = 1, $price = 100, $stock = 3, $count = 1, $title = '', $availability = 'Skladem', $author = 'Bušek I., Calda E.', $img = '/nette/www/img/illust/product-xsmall.jpg', $formats = array('pdf' => true, 'epub' => true, 'mobi' => true), $cover = 'Pevná vazba')
    {
        $bookData = new \stdClass();
        $bookData->id = $id;
        $bookData->title = !empty($title) ? $title : "{$id}. kniha";
        $bookData->availability = $availability;
        $bookData->stock = $stock;
        $bookData->price = $price;
        $bookData->count = $count;
        $bookData->author = $author;
        $bookData->img = $img;
        $bookData->cover = $cover;

        if (isset($formats))
        {
            $bookData->formats = $formats;
        }

        $book = new \App\Model\DAO\Book($bookData);

        return $book;
    }
}

$testCase = new ShoppingCartListTest($container->getService('session'));
$testCase->run();
JuniorJR
Člen | 181
+
0
-

Ahoj, chyba mluvi za vse:

Nette\InvalidStateException: Component '' is not attached to 'Nette\Applicaton\UI\Presenter'.

Tzn. ze komponenta by mela byt pripojena k Presenteru.