<NME> WhitelistRepositoryUnitTest.php
<BEF> <?php

/*
 * This file is part of the Force Login module for Magento2.
 *
 * (c) bitExpert AG
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace BitExpert\ForceCustomerLogin\Test\Unit\Repository;

use BitExpert\ForceCustomerLogin\Api\Data\Collection\WhitelistEntryCollectionFactoryInterface;
use BitExpert\ForceCustomerLogin\Api\Data\Collection\WhitelistEntrySearchResultInterface;
use BitExpert\ForceCustomerLogin\Api\Data\WhitelistEntryFactoryInterface;
use BitExpert\ForceCustomerLogin\Api\Repository\WhitelistRepositoryInterface;
use BitExpert\ForceCustomerLogin\Model\ResourceModel\WhitelistEntry\Collection;
use BitExpert\ForceCustomerLogin\Model\WhitelistEntry;
use BitExpert\ForceCustomerLogin\Model\WhitelistEntrySearchResultInterfaceFactory;
use BitExpert\ForceCustomerLogin\Repository\WhitelistRepository;
use Magento\Framework\Api\Filter;
use Magento\Framework\Api\Search\FilterGroup;
use Magento\Framework\Api\SearchCriteria;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManager;
use PHPUnit\Framework\TestCase;

/**
 * Class WhitelistRepositoryUnitTest
 *
 * @package BitExpert\ForceCustomerLogin\Test\Unit\Repository
 */
class WhitelistRepositoryUnitTest extends TestCase
{
    /**
     * @test
     */
    public function testClassExists()
    {
        $this->assertTrue(class_exists(WhitelistRepository::class));
    }

    /**
     * @test
     * @depends testClassExists
     */
    public function testConstructor()
    {
        $whitelistRepository = new WhitelistRepository(
            $this->getWhitelistEntryFactory(),
            $this->getWhitelistEntryCollectionFactory(),
            $this->getStoreManager(),
            $this->getWhitelistEntrySearchResultInterfaceFactory()
        );

        // check if mandatory interfaces are implemented
        $classInterfaces = class_implements($whitelistRepository);
        $this->assertContains(WhitelistRepositoryInterface::class, $classInterfaces);
    }

    /**
     * @return \BitExpert\ForceCustomerLogin\Api\Data\WhitelistEntryFactoryInterface
     */
    private function getWhitelistEntryFactory()
    {
        return $this->createMock(WhitelistEntryFactoryInterface::class);
    }

    /**
     * @return \BitExpert\ForceCustomerLogin\Api\Data\Collection\WhitelistEntryCollectionFactoryInterface
     */
    private function getWhitelistEntryCollectionFactory()
    {
        return $this->createMock(WhitelistEntryCollectionFactoryInterface::class);
    }

    /**
     * @return \Magento\Store\Model\StoreManager
     */
    private function getStoreManager()
    {
        return $this->getMockBuilder(StoreManager::class)
            ->disableOriginalConstructor()
            ->getMock();
    }

    /**
     * @return \BitExpert\ForceCustomerLogin\Model\WhitelistEntrySearchResultInterfaceFactory
     */
    private function getWhitelistEntrySearchResultInterfaceFactory()
    {
        return $this->getMockBuilder(WhitelistEntrySearchResultInterfaceFactory::class)
            ->disableOriginalConstructor()
            ->getMock();
    }

    /**
     * Run test of creating a new entry without existing entity
     *
     * @test
     * @depends testConstructor
     */
    public function testEntryCreationWithoutExistingEntity()
    {
        $entityId = null;
        $label = 'foobar';
        $urlRule = '/foobar';
        $storeId = 0;
        $strategy = 'default';

        $expectedWhitelistEntry = $this->createMock(WhitelistEntry::class);
        $expectedWhitelistEntry->expects($this->at(0))
            ->method('getId')
            ->will($this->returnValue($entityId));
        $expectedWhitelistEntry->expects($this->at(1))
            ->method('load')
            ->with($label, 'label')
            ->willReturnSelf();
        $expectedWhitelistEntry->expects($this->at(2))
            ->method('getId')
            ->will($this->returnValue($entityId));
        $expectedWhitelistEntry->expects($this->at(3))
            ->method('setLabel')
            ->with($label);
        $expectedWhitelistEntry->expects($this->any())
            ->method('getUrlRule')
            ->willReturn($urlRule);
        $expectedWhitelistEntry->expects($this->any())
            ->method('getStrategy')
            ->willReturn($strategy);
        $expectedWhitelistEntry->expects($this->any())
            ->method('getEditable')
            ->willReturn(true);

        $whitelistEntryFactory = $this->getWhitelistEntryFactory();
            ->with(true);
        $expectedWhitelistEntry->expects($this->at(8))
            ->method('getLabel')
            ->will($this->returnValue($label));
        $expectedWhitelistEntry->expects($this->at(9))
            ->method('getLabel')
            ->will($this->returnValue($label));
        $expectedWhitelistEntry->expects($this->at(10))
            ->method('getUrlRule')
            ->will($this->returnValue($urlRule));
        $expectedWhitelistEntry->expects($this->at(11))
            ->method('getUrlRule')
            ->will($this->returnValue($urlRule));
        $expectedWhitelistEntry->expects($this->at(12))
            ->method('getStrategy')
            ->will($this->returnValue($strategy));
        $expectedWhitelistEntry->expects($this->at(13))
            ->method('getStrategy')
            ->will($this->returnValue($strategy));
        $expectedWhitelistEntry->expects($this->at(14))
            ->method('getEditable')
            ->will($this->returnValue(true));
        $expectedWhitelistEntry->expects($this->at(15))
            ->method('save');

        $whitelistEntryFactory = $this->getWhitelistEntryFactory();
        $whitelistEntryFactory->expects($this->at(0))
            ->method('create')
            ->will($this->returnValue($expectedWhitelistEntry));
        $whitelistEntryFactory->expects($this->at(1))
            ->method('create')
            ->will($this->returnValue($expectedWhitelistEntry));

        $whitelistRepository = new WhitelistRepository(
            $whitelistEntryFactory,
            ->method('load')
            ->with($entityId)
            ->willReturnSelf();
        $expectedWhitelistEntry->expects($this->any())
            ->method('getId')
            ->willReturn($entityId);
        $expectedWhitelistEntry->expects($this->any())
            ->method('getEditable')
            ->willReturn(true);
        $expectedWhitelistEntry->expects($this->any())
            ->method('setEditable')
            ->with(true);
        // validation
        $expectedWhitelistEntry->expects($this->any())
            ->method('getLabel')
            ->willReturn($label);
        $expectedWhitelistEntry->expects($this->any())
            ->method('getUrlRule')
            ->willReturn($urlRule);
        $expectedWhitelistEntry->expects($this->any())
            ->method('getStrategy')
            ->willReturn($strategy);


        $whitelistEntryFactory = $this->getWhitelistEntryFactory();
        $whitelistEntryFactory->expects($this->any())
            ->method('create')
            ->willReturn($expectedWhitelistEntry);
            ->willReturnSelf();
        $expectedWhitelistEntry->expects($this->at(1))
            ->method('getId')
            ->will($this->returnValue($entityId));
        $expectedWhitelistEntry->expects($this->at(2))
            ->method('getId')
            ->will($this->returnValue($entityId));
        $expectedWhitelistEntry->expects($this->at(3))
            ->method('getEditable')
            ->willReturn(true);
        $this->assertEquals($expectedWhitelistEntry, $resultWhitelistEntity);
    }

    /**
     * Run test of creating a new entry with existing entity being not editable
     *
     * @test
     * @depends testConstructor
     */
    public function testEntryCreationWithExistingEntityFailsDueToNonEditable()
    {
        $this->expectException(\RuntimeException::class);
        $this->expectExceptionMessage('Whitelist entry not editable.');

        $entityId = 42;
        // validation
        $expectedWhitelistEntry->expects($this->at(9))
            ->method('getLabel')
            ->will($this->returnValue($label));
        $expectedWhitelistEntry->expects($this->at(10))
            ->method('getLabel')
            ->will($this->returnValue($label));
        $expectedWhitelistEntry->expects($this->at(11))
            ->method('getUrlRule')
            ->will($this->returnValue($urlRule));
        $expectedWhitelistEntry->expects($this->at(12))
            ->method('getUrlRule')
            ->will($this->returnValue($urlRule));
        $expectedWhitelistEntry->expects($this->at(13))
            ->method('getStrategy')
            ->will($this->returnValue($strategy));
        $expectedWhitelistEntry->expects($this->at(14))
            ->method('getStrategy')
            ->will($this->returnValue($strategy));
        $expectedWhitelistEntry->expects($this->at(15))
            ->method('getEditable')
            ->will($this->returnValue(true));
        $expectedWhitelistEntry->expects($this->at(16))
            ->method('save');

        $whitelistEntryFactory = $this->getWhitelistEntryFactory();
        $whitelistEntryFactory->expects($this->at(0))
            ->method('create')
            ->will($this->returnValue($expectedWhitelistEntry));

        $whitelistRepository = new WhitelistRepository(
            $whitelistEntryFactory,
    /**
     * @test
     * @depends testConstructor
     */
    public function deleteEntityFailsDueToNonEditable()
    {
        $entityId = 42;

        $expectedWhitelistEntry = $this->createMock(WhitelistEntry::class);
        $expectedWhitelistEntry->expects($this->once())
            ->method('load')
            ->willReturnSelf();
        $expectedWhitelistEntry->expects($this->once())
            ->method('getId')
            ->willReturn($entityId);
        $expectedWhitelistEntry->expects($this->once())
            ->method('getEditable')
            ->willReturn(false);

        $whitelistEntryFactory = $this->getWhitelistEntryFactory();
        $whitelistEntryFactory->expects($this->once())
            ->method('create')
            ->willReturn($expectedWhitelistEntry);

        $whitelistRepository = new WhitelistRepository(
            $whitelistEntryFactory,
            $this->getWhitelistEntryCollectionFactory(),
            $this->getStoreManager(),
            $this->getWhitelistEntrySearchResultInterfaceFactory()
        );
            ->willReturnSelf();
        $expectedWhitelistEntry->expects($this->at(1))
            ->method('getId')
            ->will($this->returnValue($entityId));
        $expectedWhitelistEntry->expects($this->at(2))
            ->method('getId')
            ->will($this->returnValue($entityId));
        $expectedWhitelistEntry->expects($this->at(3))
            ->method('getEditable')
            ->willReturn(false);
        $entityId = 42;
        $whitelistEntryFactory = $this->getWhitelistEntryFactory();
        $whitelistEntryFactory->expects($this->at(0))
            ->method('create')
            ->will($this->returnValue($expectedWhitelistEntry));

        $whitelistRepository = new WhitelistRepository(
            $whitelistEntryFactory,
            ->willReturn($entityId);
        $expectedWhitelistEntry->expects($this->once())
            ->method('getEditable')
            ->willReturn(true);
        $expectedWhitelistEntry->expects($this->once())
            ->method('delete');

        $whitelistEntryFactory = $this->getWhitelistEntryFactory();
        $whitelistEntryFactory->expects($this->once())
            ->method('create')
            ->willReturn($expectedWhitelistEntry);

        $whitelistRepository = new WhitelistRepository(
            $whitelistEntryFactory,
            $this->getWhitelistEntryCollectionFactory(),
            $this->getStoreManager(),
            $this->getWhitelistEntrySearchResultInterfaceFactory()
        );

        $this->assertTrue($whitelistRepository->deleteEntry($entityId));
    }

    /**
     * @test
     * @depends testConstructor
     */
    public function getCollectionSuccessfully()
        $whitelistEntryFactory = $this->getWhitelistEntryFactory();
        $whitelistEntryFactory->expects($this->once())
            ->method('create')
            ->will($this->returnValue($expectedWhitelistEntry));

        $whitelistRepository = new WhitelistRepository(
            $whitelistEntryFactory,
        $storeManager = $this->getStoreManager();
        $storeManager->expects($this->once())
            ->method('getStore')
            ->willReturn($store);

        $expectedCollection = $this->getMockBuilder(Collection::class)
            ->disableOriginalConstructor()
            ->getMock();
        $expectedCollection->expects($this->once())
            ->method('addFieldToFilter')
            ->with(
                'store_id',
                [
                    'in' => [
                        WhitelistRepositoryInterface::ROOT_STORE_ID,
                        $storeId
                    ]
                ]
            );
        $expectedCollection->expects($this->once())
            ->method('load')
            ->willReturnSelf();

        $collectionFactory = $this->getWhitelistEntryCollectionFactory();
        $collectionFactory->expects($this->once())
            ->method('create')
            ->willReturn($expectedCollection);

        $whitelistRepository = new WhitelistRepository(
        $whitelistEntryFactory = $this->getWhitelistEntryFactory();
        $whitelistEntryFactory->expects($this->once())
            ->method('create')
            ->will($this->returnValue($expectedWhitelistEntry));

        $whitelistRepository = new WhitelistRepository(
            $whitelistEntryFactory,
    }

    /**
     * @test
     * @depends testConstructor
     */
    public function getListSuccessfully()
    {
        $filter = $this->getMockBuilder(Filter::class)
            ->disableOriginalConstructor()
            ->getMock();
        $filter->expects($this->exactly(2))
            ->method('getConditionType')
            ->willReturn('foo');
        $filter->expects($this->once())
            ->method('getField')
            ->willReturn('bar');
        $filter->expects($this->once())
            ->method('getValue')
            ->willReturn('baz');

        $filterGroup = $this->getMockBuilder(FilterGroup::class)
            ->disableOriginalConstructor()
            ->getMock();
        $filterGroup->expects($this->once())
            ->method('getFilters')
            ->willReturn([$filter]);

        $searchCriteria = $this->getMockBuilder(SearchCriteria::class)
            ->disableOriginalConstructor()
            ->getMock();
        $searchCriteria->expects($this->once())
            ->method('getFilterGroups')
            ->willReturn([$filterGroup]);
        $searchCriteria->expects($this->once())
            ->method('getCurrentPage')
            ->willReturn(3);
        $searchCriteria->expects($this->once())
            ->method('getPageSize')
            ->willReturn(42);

        $expectedSearchResult = $this->getMockBuilder(WhitelistEntrySearchResultInterface::class)
            ->setMethods([
                'getItems',
                'setItems',
                'getSearchCriteria',
                'setSearchCriteria',
                'getTotalCount',
                'setTotalCount',
                'addFieldToFilter',
                'setCurPage',
                'setPageSize'
            ])
            ->getMock();
        $expectedSearchResult->expects($this->once())
            ->method('addFieldToFilter')
            ->with('bar', ['foo' => 'baz']);
        $expectedSearchResult->expects($this->once())
            ->method('setCurPage')
            ->with(3);
        $expectedSearchResult->expects($this->once())
            ->method('setPageSize')
            ->with(42);

        $searchResultFactory = $this->getWhitelistEntrySearchResultInterfaceFactory();
        $searchResultFactory->expects($this->once())
            ->method('create')
            ->willReturn($expectedSearchResult);

        $whitelistRepository = new WhitelistRepository(
            $this->getWhitelistEntryFactory(),
            $this->getWhitelistEntryCollectionFactory(),
            $this->getStoreManager(),
            $searchResultFactory
        );

        $this->assertEquals($expectedSearchResult, $whitelistRepository->getList($searchCriteria));
    }
}

<MSG> Merge pull request #157 from shochdoerfer/fix/refactor_unit_tests

Refactor unit tests
<DFF> @@ -60,7 +60,7 @@ class WhitelistRepositoryUnitTest extends TestCase
     }
 
     /**
-     * @return \BitExpert\ForceCustomerLogin\Api\Data\WhitelistEntryFactoryInterface
+     * @return WhitelistEntryFactoryInterface
      */
     private function getWhitelistEntryFactory()
     {
@@ -68,7 +68,7 @@ class WhitelistRepositoryUnitTest extends TestCase
     }
 
     /**
-     * @return \BitExpert\ForceCustomerLogin\Api\Data\Collection\WhitelistEntryCollectionFactoryInterface
+     * @return WhitelistEntryCollectionFactoryInterface
      */
     private function getWhitelistEntryCollectionFactory()
     {
@@ -76,7 +76,7 @@ class WhitelistRepositoryUnitTest extends TestCase
     }
 
     /**
-     * @return \Magento\Store\Model\StoreManager
+     * @return StoreManager
      */
     private function getStoreManager()
     {
@@ -86,7 +86,7 @@ class WhitelistRepositoryUnitTest extends TestCase
     }
 
     /**
-     * @return \BitExpert\ForceCustomerLogin\Model\WhitelistEntrySearchResultInterfaceFactory
+     * @return WhitelistEntrySearchResultInterfaceFactory
      */
     private function getWhitelistEntrySearchResultInterfaceFactory()
     {
@@ -112,14 +112,14 @@ class WhitelistRepositoryUnitTest extends TestCase
         $expectedWhitelistEntry = $this->createMock(WhitelistEntry::class);
         $expectedWhitelistEntry->expects($this->at(0))
             ->method('getId')
-            ->will($this->returnValue($entityId));
+            ->willReturn($entityId);
         $expectedWhitelistEntry->expects($this->at(1))
             ->method('load')
             ->with($label, 'label')
             ->willReturnSelf();
         $expectedWhitelistEntry->expects($this->at(2))
             ->method('getId')
-            ->will($this->returnValue($entityId));
+            ->willReturn($entityId);
         $expectedWhitelistEntry->expects($this->at(3))
             ->method('setLabel')
             ->with($label);
@@ -137,35 +137,35 @@ class WhitelistRepositoryUnitTest extends TestCase
             ->with(true);
         $expectedWhitelistEntry->expects($this->at(8))
             ->method('getLabel')
-            ->will($this->returnValue($label));
+            ->willReturn($label);
         $expectedWhitelistEntry->expects($this->at(9))
             ->method('getLabel')
-            ->will($this->returnValue($label));
+            ->willReturn($label);
         $expectedWhitelistEntry->expects($this->at(10))
             ->method('getUrlRule')
-            ->will($this->returnValue($urlRule));
+            ->willReturn($urlRule);
         $expectedWhitelistEntry->expects($this->at(11))
             ->method('getUrlRule')
-            ->will($this->returnValue($urlRule));
+            ->willReturn($urlRule);
         $expectedWhitelistEntry->expects($this->at(12))
             ->method('getStrategy')
-            ->will($this->returnValue($strategy));
+            ->willReturn($strategy);
         $expectedWhitelistEntry->expects($this->at(13))
             ->method('getStrategy')
-            ->will($this->returnValue($strategy));
+            ->willReturn($strategy);
         $expectedWhitelistEntry->expects($this->at(14))
             ->method('getEditable')
-            ->will($this->returnValue(true));
+            ->willReturn(true);
         $expectedWhitelistEntry->expects($this->at(15))
             ->method('save');
 
         $whitelistEntryFactory = $this->getWhitelistEntryFactory();
         $whitelistEntryFactory->expects($this->at(0))
             ->method('create')
-            ->will($this->returnValue($expectedWhitelistEntry));
+            ->willReturn($expectedWhitelistEntry);
         $whitelistEntryFactory->expects($this->at(1))
             ->method('create')
-            ->will($this->returnValue($expectedWhitelistEntry));
+            ->willReturn($expectedWhitelistEntry);
 
         $whitelistRepository = new WhitelistRepository(
             $whitelistEntryFactory,
@@ -200,10 +200,10 @@ class WhitelistRepositoryUnitTest extends TestCase
             ->willReturnSelf();
         $expectedWhitelistEntry->expects($this->at(1))
             ->method('getId')
-            ->will($this->returnValue($entityId));
+            ->willReturn($entityId);
         $expectedWhitelistEntry->expects($this->at(2))
             ->method('getId')
-            ->will($this->returnValue($entityId));
+            ->willReturn($entityId);
         $expectedWhitelistEntry->expects($this->at(3))
             ->method('getEditable')
             ->willReturn(true);
@@ -225,32 +225,32 @@ class WhitelistRepositoryUnitTest extends TestCase
         // validation
         $expectedWhitelistEntry->expects($this->at(9))
             ->method('getLabel')
-            ->will($this->returnValue($label));
+            ->willReturn($label);
         $expectedWhitelistEntry->expects($this->at(10))
             ->method('getLabel')
-            ->will($this->returnValue($label));
+            ->willReturn($label);
         $expectedWhitelistEntry->expects($this->at(11))
             ->method('getUrlRule')
-            ->will($this->returnValue($urlRule));
+            ->willReturn($urlRule);
         $expectedWhitelistEntry->expects($this->at(12))
             ->method('getUrlRule')
-            ->will($this->returnValue($urlRule));
+            ->willReturn($urlRule);
         $expectedWhitelistEntry->expects($this->at(13))
             ->method('getStrategy')
-            ->will($this->returnValue($strategy));
+            ->willReturn($strategy);
         $expectedWhitelistEntry->expects($this->at(14))
             ->method('getStrategy')
-            ->will($this->returnValue($strategy));
+            ->willReturn($strategy);
         $expectedWhitelistEntry->expects($this->at(15))
             ->method('getEditable')
-            ->will($this->returnValue(true));
+            ->willReturn(true);
         $expectedWhitelistEntry->expects($this->at(16))
             ->method('save');
 
         $whitelistEntryFactory = $this->getWhitelistEntryFactory();
         $whitelistEntryFactory->expects($this->at(0))
             ->method('create')
-            ->will($this->returnValue($expectedWhitelistEntry));
+            ->willReturn($expectedWhitelistEntry);
 
         $whitelistRepository = new WhitelistRepository(
             $whitelistEntryFactory,
@@ -287,10 +287,10 @@ class WhitelistRepositoryUnitTest extends TestCase
             ->willReturnSelf();
         $expectedWhitelistEntry->expects($this->at(1))
             ->method('getId')
-            ->will($this->returnValue($entityId));
+            ->willReturn($entityId);
         $expectedWhitelistEntry->expects($this->at(2))
             ->method('getId')
-            ->will($this->returnValue($entityId));
+            ->willReturn($entityId);
         $expectedWhitelistEntry->expects($this->at(3))
             ->method('getEditable')
             ->willReturn(false);
@@ -298,7 +298,7 @@ class WhitelistRepositoryUnitTest extends TestCase
         $whitelistEntryFactory = $this->getWhitelistEntryFactory();
         $whitelistEntryFactory->expects($this->at(0))
             ->method('create')
-            ->will($this->returnValue($expectedWhitelistEntry));
+            ->willReturn($expectedWhitelistEntry);
 
         $whitelistRepository = new WhitelistRepository(
             $whitelistEntryFactory,
@@ -332,7 +332,7 @@ class WhitelistRepositoryUnitTest extends TestCase
         $whitelistEntryFactory = $this->getWhitelistEntryFactory();
         $whitelistEntryFactory->expects($this->once())
             ->method('create')
-            ->will($this->returnValue($expectedWhitelistEntry));
+            ->willReturn($expectedWhitelistEntry);
 
         $whitelistRepository = new WhitelistRepository(
             $whitelistEntryFactory,
@@ -368,7 +368,7 @@ class WhitelistRepositoryUnitTest extends TestCase
         $whitelistEntryFactory = $this->getWhitelistEntryFactory();
         $whitelistEntryFactory->expects($this->once())
             ->method('create')
-            ->will($this->returnValue($expectedWhitelistEntry));
+            ->willReturn($expectedWhitelistEntry);
 
         $whitelistRepository = new WhitelistRepository(
             $whitelistEntryFactory,

Merge pull request #157 from shochdoerfer/fix/refactor_unit_tests