1

Suppose I overwrite 'Magento\Sales\Model\Resource\Order\Collection' to 'SR\RewriteSales\Model\Resource\Order\Collection', code looks like

public function addFieldToFilter($field, $condition = null)
    {
        // custom condition applied here

        return parent::addFieldToFilter($field, $condition);
    }

Now I need some test case for 'AddFieldToFilter' method.

I create a file inside SR\RewriteSales\Test\Unit\Model\Resource\Order\CollectionTest.php code looks like

class CollectionTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var Collection|\PHPUnit_Framework_MockObject_MockObject
     */
    protected $collection;

    /**
     * @var \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject
     */
    protected $select;

    protected function setUp()
    {
        $this->select = $this->getMockBuilder('Magento\Framework\DB\Select')
            ->disableOriginalConstructor()
            ->getMock();

        $connection = $this->getMockBuilder('Magento\Framework\DB\Adapter\Pdo\Mysql')
            ->disableOriginalConstructor()
            ->getMock();
        $connection->expects($this->any())
            ->method('select')
            ->will($this->returnValue($this->select));

        $resource = $this->getMockBuilder('Magento\Framework\Model\Resource\Db\AbstractDb')
            ->disableOriginalConstructor()
            ->setMethods(['__wakeup', 'getReadConnection'])
            ->getMockForAbstractClass();
        $resource->expects($this->any())
            ->method('getReadConnection')
            ->will($this->returnValue($connection));

        $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
        $arguments = $objectManagerHelper->getConstructArguments(
            'SR\RewriteSales\Model\Resource\Order\Collection',
            ['resource' => $resource, 'connection' => $connection]
        );

        $this->collection = $this->getMockBuilder('SR\RewriteSales\Model\Resource\Order\Collection')
            ->setConstructorArgs($arguments)
            ->setMethods(['addFilter', '_translateCondition', 'getMainTable', '__wakeup'])
            ->getMock();
    }

    public function testAddFieldToFilter()
    {

    }
}

It's come following error-> "RuntimeException: ObjectManager isn't initialized".

Is that possible to create some test case for overwrite class?

  • Do you use \Magento\Framework\App\ObjectManager::getInstance() method in your code? – KAndy Nov 2 '15 at 21:20
3

First of all better to do not mock class under test.

Second one, better to use plugins instead of class overwrite.

And last, you must pass all parameters of parent constructor to your class.

Simplest way to instantiate class is using objectMangerHelper

    $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
    $this->collection = $objectManager->getObject(
        'SR\RewriteSales\Model\Resource\Order\Collection',
        [
            'resource' => $resource, 
            'connection' => $connection,

        ]
    );
  • So can you give some example or update my question ? – Sohel Rana Nov 2 '15 at 20:14
  • Same error 'testAddFieldToFilter RuntimeException: ObjectManager isn't initialized' – Sohel Rana Nov 2 '15 at 20:34
  • Please try to add '__wakeup' to list of method to mock: ->setMethods(['addFilter', '_translateCondition', 'getMainTable', '__wakeup' ]) – KAndy Nov 2 '15 at 20:40
  • No luck, same error again. – Sohel Rana Nov 2 '15 at 20:49
0

Are you sure that you use phpunit.xml from dev/tests/unit/ dir? Also you shouldn't mock class which you want test (@KAndy comment said about that)

  • yes. I am using phpunit.xml from dev/tests/unit/ dir. – Sohel Rana Nov 3 '15 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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