4

Open file

\Magento\Cms\Test\Unit\Model\PageRepositoryTest

and take a look at the setUp() method

/**
 * Initialize repository
 */
protected function setUp()
{
    $this->pageResource = $this->getMockBuilder(\Magento\Cms\Model\ResourceModel\Page::class)
        ->disableOriginalConstructor()
        ->getMock();
    $this->dataObjectProcessor = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
        ->disableOriginalConstructor()
        ->getMock();
    $pageFactory = $this->getMockBuilder(\Magento\Cms\Model\PageFactory::class)
        ->disableOriginalConstructor()
        ->setMethods(['create'])
        ->getMock();
    $pageDataFactory = $this->getMockBuilder(\Magento\Cms\Api\Data\PageInterfaceFactory::class)
        ->disableOriginalConstructor()
        ->setMethods(['create'])
        ->getMock();
    $pageSearchResultFactory = $this->getMockBuilder(\Magento\Cms\Api\Data\PageSearchResultsInterfaceFactory::class)
        ->disableOriginalConstructor()
        ->setMethods(['create'])
        ->getMock();
    $collectionFactory = $this->getMockBuilder(\Magento\Cms\Model\ResourceModel\Page\CollectionFactory::class)
        ->disableOriginalConstructor()
        ->setMethods(['create'])
        ->getMock();
    $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
        ->disableOriginalConstructor()
        ->getMock();
    $store = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
        ->disableOriginalConstructor()
        ->getMock();
    $store->expects($this->any())->method('getId')->willReturn(0);
    $this->storeManager->expects($this->any())->method('getStore')->willReturn($store);

    $this->page = $this->getMockBuilder(\Magento\Cms\Model\Page::class)->disableOriginalConstructor()->getMock();
    $this->pageData = $this->getMockBuilder(\Magento\Cms\Api\Data\PageInterface::class)
        ->getMock();
    $this->pageSearchResult = $this->getMockBuilder(\Magento\Cms\Api\Data\PageSearchResultsInterface::class)
        ->getMock();
    $this->collection = $this->getMockBuilder(\Magento\Cms\Model\ResourceModel\Page\Collection::class)
        ->disableOriginalConstructor()
        ->setMethods(['getSize', 'setCurPage', 'setPageSize', 'load', 'addOrder'])
        ->getMock();

    $pageFactory->expects($this->any())
        ->method('create')
        ->willReturn($this->page);
    $pageDataFactory->expects($this->any())
        ->method('create')
        ->willReturn($this->pageData);
    $pageSearchResultFactory->expects($this->any())
        ->method('create')
        ->willReturn($this->pageSearchResult);
    $collectionFactory->expects($this->any())
        ->method('create')
        ->willReturn($this->collection);
    /**
     * @var \Magento\Cms\Model\PageFactory $pageFactory
     * @var \Magento\Cms\Api\Data\PageInterfaceFactory $pageDataFactory
     * @var \Magento\Cms\Api\Data\PageSearchResultsInterfaceFactory $pageSearchResultFactory
     * @var \Magento\Cms\Model\ResourceModel\Page\CollectionFactory $collectionFactory
     */

    $this->dataHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
        ->disableOriginalConstructor()
        ->getMock();

    $this->collectionProcessor = $this->getMockBuilder(CollectionProcessorInterface::class)
        ->getMockForAbstractClass();

    $this->repository = new PageRepository(
        $this->pageResource,
        $pageFactory,
        $pageDataFactory,
        $collectionFactory,
        $pageSearchResultFactory,
        $this->dataHelper,
        $this->dataObjectProcessor,
        $this->storeManager,
        $this->collectionProcessor
    );
}

Can someone explain on the concepts used here?

For example, making mocks with disabling original constructors like

$collectionFactory = $this->getMockBuilder(\Magento\Cms\Model\ResourceModel\Page\CollectionFactory::class)
    ->disableOriginalConstructor()
    ->setMethods(['create'])

and setting methods like

$this->collection = $this->getMockBuilder(\Magento\Cms\Model\ResourceModel\Page\Collection::class)
        ->disableOriginalConstructor()
        ->setMethods(['getSize', 'setCurPage', 'setPageSize', 'load', 'addOrder'])
        ->getMock();

In fact, anynthig you know about unit testing will be useful for me, like what is going on in the public function testDeleteById() and so on and on.

Thanks in advance.

Your Answer

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

Browse other questions tagged or ask your own question.