Unit testing Plugin in Magento 2

I got a problem in my Testunit in Magento I can’t figure out the problem

This is the File
Mode.php

<?php
namespace Mxxxx\Category\Plugin\Model\Category\Attribute\Source;

use Mxxxx\Category\Model\Category\Category;

/**
 * Class Mode
 * @package Mxxxx\Category\Plugin\Model\Category\Attribute\Source
 */
class Mode
{
    /**
     * @param \Magento\Catalog\Model\Category\Attribute\Source\Mode $subject
     * @param $result
     * @return mixed
     */
    public function afterGetAllOptions(
        \Magento\Catalog\Model\Category\Attribute\Source\Mode $subject,
        $result
    ) {
        $result[] = ['value' => Category::DM_BUFFER_PAGE, 'label' => 'Buffer page'];
        return $result;
    }
}

This is the File of Unit Test
ModeTest.php

<?php
namespace Mxxxx\Category\Test\Unit\Plugin\Model\Category\Attribute\Source;

use Mxxxx\Category\Model\Category\Category;
use Mxxxx\Category\Plugin\Model\Category\Attribute\Source\Mode;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class ModeTest extends \PHPUnit\Framework\TestCase
{
    private $model;

    public function testAfterGetAllOptions($subject, $validResult)
    {
        $validResult = ['value' => Category::DM_BUFFER_PAGE, 'label' => 'Buffer page'];
        $this->assertEquals(\Magento\Catalog\Model\Category\Attribute\Source\Mode );
    }

    protected function setUp()
    {
        $helper = new ObjectManager($this);
        $this->model = $helper->getObject(
            Mode::class
        );
    }
}

When I run the unit test using this command

./vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Mxxxx/Category/Test/Unit/Plugin/Model/Category/Attribute/Source/ModeTest

I got this error

E                                                                   1 / 1 (100%)

Time: 14.26 seconds, Memory: 10.00MB

There was 1 error:

1) Mxxxx\Category\Test\Unit\Plugin\Model\Category\Attribute\Source\ModeTest::testAfterGetAllOptions
ArgumentCountError: Too few arguments to function Mxxxx\Category\Test\Unit\Plugin\Model\Category\Attribute\Source\ModeTest::testAfterGetAllOptions(), 0 passed and exactly 2 expected

/shared/httpd/Mxxxx/magento-v2/app/code/Mxxxx/Category/Test/Unit/Plugin/Model/Category/Attribute/Source/ModeTest.php:12

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

Thanks in advance

Well you call the function testAfterGetAllOptions() without any argument .
It expecting an $subject and $validResult variable.
You can make them optional

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.