0

I am trying to create a test to see if

What is the equivalent of the config node category/collection/attributes in Magento 2 to add an attribute to the category collection

works.

I use a integration test (unfinished code)

class CatalogAttributesTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var \Magento\Catalog\Model\Category
     */
    protected $category;

    protected function setUp()
    {
        $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
        $this->category = $objectManager->getObject('Magento\Catalog\Model\Category');
    }

    protected function tearDown()
    {
        $this->block = null;
    }

    public function testRedirectUrlAttributeIsThere()
    {
        $this->category->getCollection();
    }
}

On test execution I get

PHP Fatal error:  Call to undefined method Mock_AbstractDb_87c49d8f::setStoreId() in ..../vendor/magento/module-catalog/Model/AbstractModel.php on line 227

Trace

PHP  10. ReflectionMethod->invokeArgs() /home/self/mestrona/workspace/proofgigant.de/vendor/phpunit/phpunit/src/Framework/TestCase.php:951
PHP  11. Mestrona\CategoryRedirect\Test\Unit\CatalogAttributesTest->testRedirectUrlAttributeIsThere() /home/self/mestrona/workspace/proofgigant.de/vendor/phpunit/phpunit/src/Framework/TestCase.php:951
PHP  12. Magento\Framework\Model\AbstractModel->getCollection() /home/self/mestrona/workspace/proofgigant.de/vendor/mestrona/magento-module-categoryredirect/Test/Integration/CatalogAttributesTest.php:24
PHP  13. Magento\Catalog\Model\AbstractModel->getResourceCollection() /home/self/mestrona/workspace/proofgigant.de/vendor/magento/framework/Model/AbstractModel.php:517

I use the phpunit.xml from dev/tests/integration/phpunit.xml.dist and the database seems to be set up (2 minutes delay when starting up)

It does not seem to use the real integration test database? What am I doing wrong?

Using the unit/phpunit.xml.dist produced the same result.

1 Answer 1

0

It works if I create any category in the setUp()

protected function setUp()
{
    /**
     * Create category
     * @source \Magento\Catalog\Model\Indexer\FlatTest::testCreateCategory
     */

    /** @var \Magento\Catalog\Model\Category $category */
    $category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
        'Magento\Catalog\Model\Category'
    );
    $category->getResource()->load($category, 2);

    /** @var \Magento\Catalog\Model\Category $categoryOne */
    $categoryOne = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
        'Magento\Catalog\Model\Category'
    );
    $categoryOne->setName('Category ' . uniqid())->setPath($category->getPath())->setIsActive(true);
    $category->getResource()->save($categoryOne);

    $this->category = $categoryOne;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.