From 5e9d0de5d8cc295ef47120536dc719c00a0aadb1 Mon Sep 17 00:00:00 2001 From: Florian Horn Date: Wed, 25 Oct 2017 11:17:08 +0200 Subject: [PATCH] Increase test coverage --- Controller/Adminhtml/Manage/Create.php | 1 + Controller/Adminhtml/Manage/Delete.php | 1 + Controller/Adminhtml/Manage/Index.php | 1 + .../Adminhtml/Manage/RestoreDefault.php | 1 + Controller/Adminhtml/Manage/Save.php | 1 + .../Adminhtml/Manage/CreateUnitTest.php | 53 +++ .../CollectionFactoryUnitTest.php | 46 +++ .../Model/Resource/WhitelistEntryUnitTest.php | 37 ++ .../Model/WhitelistEntryFactoryUnitTest.php | 46 +++ ...rySearchResultInterfaceFactoryUnitTest.php | 49 +++ Test/Unit/Model/WhitelistEntryUnitTest.php | 95 +++++ Test/Unit/Plugin/AfterLoginPluginUnitTest.php | 4 +- .../WhitelistRepositoryUnitTest.php | 336 +++++++++++++++++- .../Listing/Column/DeleteActionUnitTest.php | 122 +++++++ .../Listing/Column/StoreNameUnitTest.php | 126 +++++++ .../Listing/Column/StrategyNameUnitTest.php | 120 +++++++ .../Unit/Validator/WhitelistEntryUnitTest.php | 237 ++++++++++++ Ui/Component/Listing/Column/StoreName.php | 1 - Ui/Component/Listing/Column/StrategyName.php | 1 - phpunit.xml | 3 + 20 files changed, 1274 insertions(+), 7 deletions(-) create mode 100644 Test/Unit/Controller/Adminhtml/Manage/CreateUnitTest.php create mode 100644 Test/Unit/Model/Resource/WhitelistEntry/CollectionFactoryUnitTest.php create mode 100644 Test/Unit/Model/Resource/WhitelistEntryUnitTest.php create mode 100644 Test/Unit/Model/WhitelistEntryFactoryUnitTest.php create mode 100644 Test/Unit/Model/WhitelistEntrySearchResultInterfaceFactoryUnitTest.php create mode 100644 Test/Unit/Model/WhitelistEntryUnitTest.php create mode 100644 Test/Unit/Ui/Component/Listing/Column/DeleteActionUnitTest.php create mode 100644 Test/Unit/Ui/Component/Listing/Column/StoreNameUnitTest.php create mode 100644 Test/Unit/Ui/Component/Listing/Column/StrategyNameUnitTest.php create mode 100644 Test/Unit/Validator/WhitelistEntryUnitTest.php diff --git a/Controller/Adminhtml/Manage/Create.php b/Controller/Adminhtml/Manage/Create.php index ba80b25..5070304 100644 --- a/Controller/Adminhtml/Manage/Create.php +++ b/Controller/Adminhtml/Manage/Create.php @@ -28,6 +28,7 @@ public function execute() /** * {@inheritdoc} + * @codeCoverageIgnore */ protected function _isAllowed() { diff --git a/Controller/Adminhtml/Manage/Delete.php b/Controller/Adminhtml/Manage/Delete.php index f2112ee..0f40b7b 100644 --- a/Controller/Adminhtml/Manage/Delete.php +++ b/Controller/Adminhtml/Manage/Delete.php @@ -97,6 +97,7 @@ public function execute() /** * {@inheritdoc} + * @codeCoverageIgnore */ protected function _isAllowed() { diff --git a/Controller/Adminhtml/Manage/Index.php b/Controller/Adminhtml/Manage/Index.php index 7a1d820..a5a0f4f 100644 --- a/Controller/Adminhtml/Manage/Index.php +++ b/Controller/Adminhtml/Manage/Index.php @@ -29,6 +29,7 @@ public function execute() /** * {@inheritdoc} + * @codeCoverageIgnore */ protected function _isAllowed() { diff --git a/Controller/Adminhtml/Manage/RestoreDefault.php b/Controller/Adminhtml/Manage/RestoreDefault.php index 904c196..e1abfe9 100644 --- a/Controller/Adminhtml/Manage/RestoreDefault.php +++ b/Controller/Adminhtml/Manage/RestoreDefault.php @@ -127,6 +127,7 @@ protected function getWhiteListEntriesIndexedByPath() /** * {@inheritdoc} + * @codeCoverageIgnore */ protected function _isAllowed() { diff --git a/Controller/Adminhtml/Manage/Save.php b/Controller/Adminhtml/Manage/Save.php index 3c6890e..a7c7cec 100644 --- a/Controller/Adminhtml/Manage/Save.php +++ b/Controller/Adminhtml/Manage/Save.php @@ -117,6 +117,7 @@ public function execute() /** * {@inheritdoc} + * @codeCoverageIgnore */ protected function _isAllowed() { diff --git a/Test/Unit/Controller/Adminhtml/Manage/CreateUnitTest.php b/Test/Unit/Controller/Adminhtml/Manage/CreateUnitTest.php new file mode 100644 index 0000000..c661c08 --- /dev/null +++ b/Test/Unit/Controller/Adminhtml/Manage/CreateUnitTest.php @@ -0,0 +1,53 @@ +createMock('\Magento\Framework\App\ViewInterface'); + $view->expects($this->once()) + ->method('loadLayout'); + $view->expects($this->once()) + ->method('renderLayout'); + + $context = $this->getContext(); + $context->expects($this->atLeastOnce()) + ->method('getView') + ->willReturn($view); + + $action = new Create( + $context + ); + $action->execute(); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Backend\App\Action\Context + */ + protected function getContext() + { + return $this->getMockBuilder('\Magento\Backend\App\Action\Context') + ->disableOriginalConstructor() + ->getMock(); + } +} diff --git a/Test/Unit/Model/Resource/WhitelistEntry/CollectionFactoryUnitTest.php b/Test/Unit/Model/Resource/WhitelistEntry/CollectionFactoryUnitTest.php new file mode 100644 index 0000000..f6875f9 --- /dev/null +++ b/Test/Unit/Model/Resource/WhitelistEntry/CollectionFactoryUnitTest.php @@ -0,0 +1,46 @@ +createMock('\bitExpert\ForceCustomerLogin\Model\ResourceModel\WhitelistEntry\Collection'); + + $om = $this->getObjectManager(); + $om->expects($this->once()) + ->method('create') + ->with('\\bitExpert\\ForceCustomerLogin\\Model\\ResourceModel\\WhitelistEntry\\Collection', ['foo' => 'bar']) + ->willReturn($expectedEntity); + + $factory = new CollectionFactory($om); + + $this->assertEquals($expectedEntity, $factory->create(['foo' => 'bar'])); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\ObjectManagerInterface + */ + protected function getObjectManager() + { + return $this->createMock('\Magento\Framework\ObjectManagerInterface'); + } +} diff --git a/Test/Unit/Model/Resource/WhitelistEntryUnitTest.php b/Test/Unit/Model/Resource/WhitelistEntryUnitTest.php new file mode 100644 index 0000000..b451007 --- /dev/null +++ b/Test/Unit/Model/Resource/WhitelistEntryUnitTest.php @@ -0,0 +1,37 @@ +getDatabaseContext()); + $this->assertEquals('whitelist_entry_id', $resource->getIdFieldName()); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Model\ResourceModel\Db\Context + */ + protected function getDatabaseContext() + { + return $this->createMock('\Magento\Framework\Model\ResourceModel\Db\Context'); + } +} diff --git a/Test/Unit/Model/WhitelistEntryFactoryUnitTest.php b/Test/Unit/Model/WhitelistEntryFactoryUnitTest.php new file mode 100644 index 0000000..daa1417 --- /dev/null +++ b/Test/Unit/Model/WhitelistEntryFactoryUnitTest.php @@ -0,0 +1,46 @@ +createMock('\bitExpert\ForceCustomerLogin\Model\WhitelistEntry'); + + $om = $this->getObjectManager(); + $om->expects($this->once()) + ->method('create') + ->with('\\bitExpert\\ForceCustomerLogin\\Model\\WhitelistEntry', ['foo' => 'bar']) + ->willReturn($expectedEntity); + + $factory = new WhitelistEntryFactory($om); + + $this->assertEquals($expectedEntity, $factory->create(['foo' => 'bar'])); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\ObjectManagerInterface + */ + protected function getObjectManager() + { + return $this->createMock('\Magento\Framework\ObjectManagerInterface'); + } +} diff --git a/Test/Unit/Model/WhitelistEntrySearchResultInterfaceFactoryUnitTest.php b/Test/Unit/Model/WhitelistEntrySearchResultInterfaceFactoryUnitTest.php new file mode 100644 index 0000000..654bf6f --- /dev/null +++ b/Test/Unit/Model/WhitelistEntrySearchResultInterfaceFactoryUnitTest.php @@ -0,0 +1,49 @@ +createMock('\bitExpert\ForceCustomerLogin\Api\Data\Collection\WhitelistEntrySearchResultInterface'); + + $om = $this->getObjectManager(); + $om->expects($this->once()) + ->method('create') + ->with( + '\\bitExpert\\ForceCustomerLogin\\Api\\Data\\Collection\\WhitelistEntrySearchResultInterface', + ['foo' => 'bar'] + ) + ->willReturn($expectedEntity); + + $factory = new WhitelistEntrySearchResultInterfaceFactory($om); + + $this->assertEquals($expectedEntity, $factory->create(['foo' => 'bar'])); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\ObjectManagerInterface + */ + protected function getObjectManager() + { + return $this->createMock('\Magento\Framework\ObjectManagerInterface'); + } +} diff --git a/Test/Unit/Model/WhitelistEntryUnitTest.php b/Test/Unit/Model/WhitelistEntryUnitTest.php new file mode 100644 index 0000000..285beac --- /dev/null +++ b/Test/Unit/Model/WhitelistEntryUnitTest.php @@ -0,0 +1,95 @@ +getResourceModel(); + $resource->expects($this->atLeastOnce()) + ->method('getIdFieldName') + ->willReturn('whitelist_entry_id'); + + $entity = new WhitelistEntry( + $this->getContext(), + $this->getRegistry(), + $resource, + $this->getDatabase(), + [] + ); + + $entity->setStoreId(42); + $this->assertEquals(42, $entity->getStoreId()); + $entity->setLabel('label'); + $this->assertEquals('label', $entity->getLabel()); + $entity->setUrlRule('url-rule'); + $this->assertEquals('url-rule', $entity->getUrlRule()); + $entity->setStrategy('strategy'); + $this->assertEquals('strategy', $entity->getStrategy()); + $entity->setEditable(false); + $this->assertFalse($entity->getEditable()); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Model\Context + */ + protected function getContext() + { + return $this->getMockBuilder('\Magento\Framework\Model\Context') + ->disableOriginalConstructor() + ->getMock(); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Registry + */ + protected function getRegistry() + { + return $this->getMockBuilder('\Magento\Framework\Registry') + ->disableOriginalConstructor() + ->getMock(); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Model\ResourceModel\AbstractResource + */ + protected function getResourceModel() + { + return $this->getMockBuilder('\Magento\Framework\Model\ResourceModel\AbstractResource') + ->disableOriginalConstructor() + ->setMethods([ + '_construct', + 'getConnection', + 'getIdFieldName' + ]) + ->getMock(); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Data\Collection\AbstractDb + */ + protected function getDatabase() + { + return $this->getMockBuilder('\Magento\Framework\Data\Collection\AbstractDb') + ->disableOriginalConstructor() + ->getMock(); + } +} diff --git a/Test/Unit/Plugin/AfterLoginPluginUnitTest.php b/Test/Unit/Plugin/AfterLoginPluginUnitTest.php index 19f3dd5..61137be 100644 --- a/Test/Unit/Plugin/AfterLoginPluginUnitTest.php +++ b/Test/Unit/Plugin/AfterLoginPluginUnitTest.php @@ -8,13 +8,13 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ -namespace bitExpert\ForceCustomerLogin\Test\Unit\Observer; +namespace bitExpert\ForceCustomerLogin\Test\Unit\Plugin; use bitExpert\ForceCustomerLogin\Plugin\AfterLoginPlugin; /** * Class AfterLoginPluginUnitTest - * @package bitExpert\ForceCustomerLogin\Test\Unit\Observer + * @package bitExpert\ForceCustomerLogin\Test\Unit\Plugin */ class AfterLoginPluginUnitTest extends \PHPUnit\Framework\TestCase { diff --git a/Test/Unit/Repository/WhitelistRepositoryUnitTest.php b/Test/Unit/Repository/WhitelistRepositoryUnitTest.php index b586c8b..20420bd 100644 --- a/Test/Unit/Repository/WhitelistRepositoryUnitTest.php +++ b/Test/Unit/Repository/WhitelistRepositoryUnitTest.php @@ -10,6 +10,9 @@ */ namespace bitExpert\ForceCustomerLogin\Test\Unit\Repository; +use bitExpert\ForceCustomerLogin\Api\Repository\WhitelistRepositoryInterface; +use bitExpert\ForceCustomerLogin\Repository\WhitelistRepository; + /** * Class WhitelistRepositoryUnitTest * @package bitExpert\ForceCustomerLogin\Test\Unit\Repository @@ -30,7 +33,7 @@ public function testClassExists() */ public function testConstructor() { - $whitelistRepository = new \bitExpert\ForceCustomerLogin\Repository\WhitelistRepository( + $whitelistRepository = new WhitelistRepository( $this->getWhitelistEntryFactory(), $this->getWhitelistEntryCollectionFactory(), $this->getStoreManager(), @@ -65,7 +68,7 @@ public function testEntryCreationWithoutExistingEntity() $expectedWhitelistEntry->expects($this->at(1)) ->method('load') ->with($label, 'label') - ->will($this->returnSelf()); + ->willReturnSelf(); $expectedWhitelistEntry->expects($this->at(2)) ->method('getId') ->will($this->returnValue($entityId)); @@ -116,7 +119,7 @@ public function testEntryCreationWithoutExistingEntity() ->method('create') ->will($this->returnValue($expectedWhitelistEntry)); - $whitelistRepository = new \bitExpert\ForceCustomerLogin\Repository\WhitelistRepository( + $whitelistRepository = new WhitelistRepository( $whitelistEntryFactory, $this->getWhitelistEntryCollectionFactory(), $this->getStoreManager(), @@ -128,6 +131,333 @@ public function testEntryCreationWithoutExistingEntity() $this->assertEquals($expectedWhitelistEntry, $resultWhitelistEntity); } + /** + * Run test of creating a new entry with existing entity + * @test + * @depends testConstructor + */ + public function testEntryCreationWithExistingEntity() + { + $entityId = 42; + $label = 'foobar'; + $urlRule = '/foobar'; + $storeId = 0; + $strategy = 'default'; + + $expectedWhitelistEntry = $this->createMock('\bitExpert\ForceCustomerLogin\Model\WhitelistEntry'); + $expectedWhitelistEntry->expects($this->at(0)) + ->method('load') + ->with($entityId) + ->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); + $expectedWhitelistEntry->expects($this->at(4)) + ->method('setLabel') + ->with($label); + $expectedWhitelistEntry->expects($this->at(5)) + ->method('setUrlRule') + ->with($urlRule); + $expectedWhitelistEntry->expects($this->at(6)) + ->method('setStrategy') + ->with($strategy); + $expectedWhitelistEntry->expects($this->at(7)) + ->method('setStoreId') + ->with($storeId); + $expectedWhitelistEntry->expects($this->at(8)) + ->method('setEditable') + ->with(true); + // 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, + $this->getWhitelistEntryCollectionFactory(), + $this->getStoreManager(), + $this->getWhitelistEntrySearchResultInterfaceFactory() + ); + + $resultWhitelistEntity = $whitelistRepository->createEntry($entityId, $label, $urlRule, $strategy, $storeId); + + $this->assertEquals($expectedWhitelistEntry, $resultWhitelistEntity); + } + + /** + * Run test of creating a new entry with existing entity being not editable + * @test + * @depends testConstructor + * @expectedException \RuntimeException + * @expectedExceptionMessage Whitelist entry not editable. + */ + public function testEntryCreationWithExistingEntityFailsDueToNonEditable() + { + $entityId = 42; + $label = 'foobar'; + $urlRule = '/foobar'; + $storeId = 0; + $strategy = 'default'; + + $expectedWhitelistEntry = $this->createMock('\bitExpert\ForceCustomerLogin\Model\WhitelistEntry'); + $expectedWhitelistEntry->expects($this->at(0)) + ->method('load') + ->with($entityId) + ->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); + + $whitelistEntryFactory = $this->getWhitelistEntryFactory(); + $whitelistEntryFactory->expects($this->at(0)) + ->method('create') + ->will($this->returnValue($expectedWhitelistEntry)); + + $whitelistRepository = new WhitelistRepository( + $whitelistEntryFactory, + $this->getWhitelistEntryCollectionFactory(), + $this->getStoreManager(), + $this->getWhitelistEntrySearchResultInterfaceFactory() + ); + + $whitelistRepository->createEntry($entityId, $label, $urlRule, $strategy, $storeId); + } + + /** + * @test + * @depends testConstructor + */ + public function deleteEntityFailsDueToNonEditable() + { + $entityId = 42; + + $expectedWhitelistEntry = $this->createMock('\bitExpert\ForceCustomerLogin\Model\WhitelistEntry'); + $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') + ->will($this->returnValue($expectedWhitelistEntry)); + + $whitelistRepository = new WhitelistRepository( + $whitelistEntryFactory, + $this->getWhitelistEntryCollectionFactory(), + $this->getStoreManager(), + $this->getWhitelistEntrySearchResultInterfaceFactory() + ); + + $this->assertFalse($whitelistRepository->deleteEntry($entityId)); + } + + /** + * @test + * @depends testConstructor + */ + public function deleteEntitySucceeds() + { + $entityId = 42; + + $expectedWhitelistEntry = $this->createMock('\bitExpert\ForceCustomerLogin\Model\WhitelistEntry'); + $expectedWhitelistEntry->expects($this->once()) + ->method('load') + ->willReturnSelf(); + $expectedWhitelistEntry->expects($this->once()) + ->method('getId') + ->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') + ->will($this->returnValue($expectedWhitelistEntry)); + + $whitelistRepository = new WhitelistRepository( + $whitelistEntryFactory, + $this->getWhitelistEntryCollectionFactory(), + $this->getStoreManager(), + $this->getWhitelistEntrySearchResultInterfaceFactory() + ); + + $this->assertTrue($whitelistRepository->deleteEntry($entityId)); + } + + /** + * @test + * @depends testConstructor + */ + public function getCollectionSuccessfully() + { + $storeId = 222; + $store = $this->createMock('\Magento\Store\Api\Data\StoreInterface'); + $store->expects($this->once()) + ->method('getId') + ->willReturn($storeId); + + $storeManager = $this->getStoreManager(); + $storeManager->expects($this->once()) + ->method('getStore') + ->willReturn($store); + + $expectedCollection = $this->getMockBuilder('\bitExpert\ForceCustomerLogin\Model\ResourceModel\WhitelistEntry\Collection') + ->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( + $this->getWhitelistEntryFactory(), + $collectionFactory, + $storeManager, + $this->getWhitelistEntrySearchResultInterfaceFactory() + ); + + $this->assertEquals($expectedCollection, $whitelistRepository->getCollection()); + } + + /** + * @test + * @depends testConstructor + */ + public function getListSuccessfully() + { + $filter = $this->getMockBuilder('\Magento\Framework\Api\Filter') + ->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('\Magento\Framework\Api\Search\FilterGroup') + ->disableOriginalConstructor() + ->getMock(); + $filterGroup->expects($this->once()) + ->method('getFilters') + ->willReturn([$filter]); + + $searchCriteria = $this->getMockBuilder('\Magento\Framework\Api\SearchCriteria') + ->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( + '\bitExpert\ForceCustomerLogin\Api\Data\Collection\WhitelistEntrySearchResultInterface' + ) + ->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)); + } + /** * @return \bitExpert\ForceCustomerLogin\Api\Data\WhitelistEntryFactoryInterface */ diff --git a/Test/Unit/Ui/Component/Listing/Column/DeleteActionUnitTest.php b/Test/Unit/Ui/Component/Listing/Column/DeleteActionUnitTest.php new file mode 100644 index 0000000..a86c156 --- /dev/null +++ b/Test/Unit/Ui/Component/Listing/Column/DeleteActionUnitTest.php @@ -0,0 +1,122 @@ +getUrl(); + $url->expects($this->once()) + ->method('getUrl') + ->with('viewurlpath', [ + 'id' => '1' + ]) + ->willReturn('some-url'); + + $action = new DeleteAction( + $this->getContext(), + $this->getUiComponentFactory(), + $url + ); + + $action->setData([ + 'config' => [ + 'idFieldName' => 'whitelist_entry_id', + 'viewUrlPath' => 'viewurlpath', + 'label' => 'some-label' + ], + 'name' => 'delete' + ]); + + $dataSource = ['foo' => 'bar']; + $this->assertEquals($dataSource, $action->prepareDataSource($dataSource)); + + $dataSource = [ + 'data' => [ + 'items' => [ + [ + 'foo' => 'bar' + ], + [ + 'whitelist_entry_id' => '1', + 'editable' => '1' + ], + [ + 'whitelist_entry_id' => '2', + 'editable' => '0' + ] + ] + ] + ]; + + $expectedDataSource = [ + 'data' => [ + 'items' => [ + [ + 'foo' => 'bar' + ], + [ + 'whitelist_entry_id' => '1', + 'editable' => '1', + 'delete' => [ + 'edit' => [ + 'href' => 'some-url', + 'label' => 'some-label' + ] + ] + ], + [ + 'whitelist_entry_id' => '2', + 'editable' => '0' + ] + ] + ] + ]; + + $this->assertEquals($expectedDataSource, $action->prepareDataSource($dataSource)); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Element\UiComponent\ContextInterface + */ + protected function getContext() + { + return $this->createMock('\Magento\Framework\View\Element\UiComponent\ContextInterface'); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Element\UiComponentFactory + */ + protected function getUiComponentFactory() + { + return $this->getMockBuilder('\Magento\Framework\View\Element\UiComponentFactory') + ->disableOriginalConstructor() + ->getMock(); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\UrlInterface + */ + protected function getUrl() + { + return $this->createMock('\Magento\Framework\UrlInterface'); + } +} diff --git a/Test/Unit/Ui/Component/Listing/Column/StoreNameUnitTest.php b/Test/Unit/Ui/Component/Listing/Column/StoreNameUnitTest.php new file mode 100644 index 0000000..029fe77 --- /dev/null +++ b/Test/Unit/Ui/Component/Listing/Column/StoreNameUnitTest.php @@ -0,0 +1,126 @@ +createMock('\Magento\Store\Api\Data\StoreInterface'); + $store = $this->createMock('\Magento\Store\Api\Data\StoreInterface'); + $store->expects($this->once()) + ->method('getId') + ->willReturn($storeId); + $store->expects($this->once()) + ->method('getName') + ->willReturn('foobar'); + + $storeManager = $this->getStoreManager(); + $storeManager->expects($this->at(0)) + ->method('getStore') + ->with(0) + ->willReturn($globalStore); + $storeManager->expects($this->at(1)) + ->method('getStore') + ->with($storeId) + ->willReturn($store); + + $action = new StoreName( + $this->getContext(), + $storeManager, + $this->getUiComponentFactory() + ); + + $action->setData([ + 'name' => 'store' + ]); + + $dataSource = ['foo' => 'bar']; + $this->assertEquals($dataSource, $action->prepareDataSource($dataSource)); + + $dataSource = [ + 'data' => [ + 'items' => [ + [ + 'foo' => 'bar' + ], + [ + 'store_id' => '0' + ], + [ + 'store_id' => '42' + ] + ] + ] + ]; + + $expectedDataSource = [ + 'data' => [ + 'items' => [ + [ + 'foo' => 'bar' + ], + [ + 'store_id' => '0', + 'store' => 'All Stores' + ], + [ + 'store_id' => '42', + 'store' => 'foobar' + ] + ] + ] + ]; + + $this->assertEquals($expectedDataSource, $action->prepareDataSource($dataSource)); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Element\UiComponent\ContextInterface + */ + protected function getContext() + { + return $this->createMock('\Magento\Framework\View\Element\UiComponent\ContextInterface'); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Element\UiComponentFactory + */ + protected function getUiComponentFactory() + { + return $this->getMockBuilder('\Magento\Framework\View\Element\UiComponentFactory') + ->disableOriginalConstructor() + ->getMock(); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\StoreManager + */ + protected function getStoreManager() + { + return $this->createMock('\Magento\Store\Model\StoreManager'); + } +} diff --git a/Test/Unit/Ui/Component/Listing/Column/StrategyNameUnitTest.php b/Test/Unit/Ui/Component/Listing/Column/StrategyNameUnitTest.php new file mode 100644 index 0000000..cb447c1 --- /dev/null +++ b/Test/Unit/Ui/Component/Listing/Column/StrategyNameUnitTest.php @@ -0,0 +1,120 @@ +createMock('\bitExpert\ForceCustomerLogin\Helper\Strategy\StrategyInterface'); + $strategy->expects($this->once()) + ->method('getName') + ->willReturn('FooBar'); + + $strategyManager = $this->getStrategyManager(); + $strategyManager->expects($this->at(0)) + ->method('has') + ->with('baz') + ->willReturn(false); + $strategyManager->expects($this->at(1)) + ->method('has') + ->with('foobar') + ->willReturn(true); + $strategyManager->expects($this->at(2)) + ->method('get') + ->with('foobar') + ->willReturn($strategy); + + $action = new StrategyName( + $this->getContext(), + $strategyManager, + $this->getUiComponentFactory() + ); + + $action->setData([ + 'name' => 'strategy' + ]); + + $dataSource = ['foo' => 'bar']; + $this->assertEquals($dataSource, $action->prepareDataSource($dataSource)); + + $dataSource = [ + 'data' => [ + 'items' => [ + [ + 'foo' => 'bar' + ], + [ + 'strategy' => 'baz' + ], + [ + 'strategy' => 'foobar' + ] + ] + ] + ]; + + $expectedDataSource = [ + 'data' => [ + 'items' => [ + [ + 'foo' => 'bar' + ], + [ + 'strategy' => 'baz' + ], + [ + 'strategy' => 'FooBar' + ] + ] + ] + ]; + + $this->assertEquals($expectedDataSource, $action->prepareDataSource($dataSource)); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Element\UiComponent\ContextInterface + */ + protected function getContext() + { + return $this->createMock('\Magento\Framework\View\Element\UiComponent\ContextInterface'); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Element\UiComponentFactory + */ + protected function getUiComponentFactory() + { + return $this->getMockBuilder('\Magento\Framework\View\Element\UiComponentFactory') + ->disableOriginalConstructor() + ->getMock(); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\bitExpert\ForceCustomerLogin\Helper\Strategy\StrategyManager + */ + protected function getStrategyManager() + { + return $this->getMockBuilder('\bitExpert\ForceCustomerLogin\Helper\Strategy\StrategyManager') + ->disableOriginalConstructor() + ->getMock(); + } +} diff --git a/Test/Unit/Validator/WhitelistEntryUnitTest.php b/Test/Unit/Validator/WhitelistEntryUnitTest.php new file mode 100644 index 0000000..45aebf7 --- /dev/null +++ b/Test/Unit/Validator/WhitelistEntryUnitTest.php @@ -0,0 +1,237 @@ +getWhitelistEntry(); + $entity->expects($this->at(0)) + ->method('getLabel') + ->willReturn(''); + + $validator = new WhitelistEntry(); + $validator->validate($entity); + } + + /** + * @test + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Label value is too long. + */ + public function validationFailsDueToLabelTooLong() + { + $entity = $this->getWhitelistEntry(); + $entity->expects($this->at(0)) + ->method('getLabel') + ->willReturn('foo'); + $entity->expects($this->at(1)) + ->method('getLabel') + ->willReturn(str_repeat('.', 256)); + + $validator = new WhitelistEntry(); + $validator->validate($entity); + } + + /** + * @test + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Url Rule value is too short. + */ + public function validationFailsDueToUrlRuleTooShort() + { + $entity = $this->getWhitelistEntry(); + $entity->expects($this->at(0)) + ->method('getLabel') + ->willReturn('foo'); + $entity->expects($this->at(1)) + ->method('getLabel') + ->willReturn(str_repeat('.', 255)); + $entity->expects($this->at(2)) + ->method('getUrlRule') + ->willReturn(''); + + $validator = new WhitelistEntry(); + $validator->validate($entity); + } + + /** + * @test + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Url Rule value is too long. + */ + public function validationFailsDueToUrlRuleTooLong() + { + $entity = $this->getWhitelistEntry(); + $entity->expects($this->at(0)) + ->method('getLabel') + ->willReturn('foo'); + $entity->expects($this->at(1)) + ->method('getLabel') + ->willReturn(str_repeat('.', 255)); + $entity->expects($this->at(2)) + ->method('getUrlRule') + ->willReturn('foo'); + $entity->expects($this->at(3)) + ->method('getUrlRule') + ->willReturn(str_repeat('.', 256)); + + $validator = new WhitelistEntry(); + $validator->validate($entity); + } + + /** + * @test + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Strategy value is too short. + */ + public function validationFailsDueToStrategyTooShort() + { + $entity = $this->getWhitelistEntry(); + $entity->expects($this->at(0)) + ->method('getLabel') + ->willReturn('foo'); + $entity->expects($this->at(1)) + ->method('getLabel') + ->willReturn(str_repeat('.', 255)); + $entity->expects($this->at(2)) + ->method('getUrlRule') + ->willReturn('foo'); + $entity->expects($this->at(3)) + ->method('getUrlRule') + ->willReturn(str_repeat('.', 255)); + $entity->expects($this->at(4)) + ->method('getStrategy') + ->willReturn(''); + + $validator = new WhitelistEntry(); + $validator->validate($entity); + } + + /** + * @test + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Strategy value is too long. + */ + public function validationFailsDueToStrategyTooLong() + { + $entity = $this->getWhitelistEntry(); + $entity->expects($this->at(0)) + ->method('getLabel') + ->willReturn('foo'); + $entity->expects($this->at(1)) + ->method('getLabel') + ->willReturn(str_repeat('.', 255)); + $entity->expects($this->at(2)) + ->method('getUrlRule') + ->willReturn('foo'); + $entity->expects($this->at(3)) + ->method('getUrlRule') + ->willReturn(str_repeat('.', 255)); + $entity->expects($this->at(4)) + ->method('getStrategy') + ->willReturn('foo'); + $entity->expects($this->at(5)) + ->method('getStrategy') + ->willReturn(str_repeat('.', 256)); + + $validator = new WhitelistEntry(); + $validator->validate($entity); + } + + /** + * @test + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Editable is no boolean value. + */ + public function validationFailsDueToEditableFalseType() + { + $entity = $this->getWhitelistEntry(); + $entity->expects($this->at(0)) + ->method('getLabel') + ->willReturn('foo'); + $entity->expects($this->at(1)) + ->method('getLabel') + ->willReturn(str_repeat('.', 255)); + $entity->expects($this->at(2)) + ->method('getUrlRule') + ->willReturn('foo'); + $entity->expects($this->at(3)) + ->method('getUrlRule') + ->willReturn(str_repeat('.', 255)); + $entity->expects($this->at(4)) + ->method('getStrategy') + ->willReturn('foo'); + $entity->expects($this->at(5)) + ->method('getStrategy') + ->willReturn(str_repeat('.', 255)); + $entity->expects($this->at(6)) + ->method('getEditable') + ->willReturn('foo'); + + $validator = new WhitelistEntry(); + $validator->validate($entity); + } + + /** + * @test + */ + public function validationSucceeds() + { + $entity = $this->getWhitelistEntry(); + $entity->expects($this->at(0)) + ->method('getLabel') + ->willReturn('foo'); + $entity->expects($this->at(1)) + ->method('getLabel') + ->willReturn(str_repeat('.', 255)); + $entity->expects($this->at(2)) + ->method('getUrlRule') + ->willReturn('foo'); + $entity->expects($this->at(3)) + ->method('getUrlRule') + ->willReturn(str_repeat('.', 255)); + $entity->expects($this->at(4)) + ->method('getStrategy') + ->willReturn('foo'); + $entity->expects($this->at(5)) + ->method('getStrategy') + ->willReturn(str_repeat('.', 255)); + $entity->expects($this->at(6)) + ->method('getEditable') + ->willReturn(false); + + $validator = new WhitelistEntry(); + $this->assertTrue($validator->validate($entity)); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|\bitExpert\ForceCustomerLogin\Model\WhitelistEntry + */ + private function getWhitelistEntry() + { + return $this->getMockBuilder('\bitExpert\ForceCustomerLogin\Model\WhitelistEntry') + ->disableOriginalConstructor() + ->getMock(); + } +} diff --git a/Ui/Component/Listing/Column/StoreName.php b/Ui/Component/Listing/Column/StoreName.php index 2f0cfc8..bcdace8 100644 --- a/Ui/Component/Listing/Column/StoreName.php +++ b/Ui/Component/Listing/Column/StoreName.php @@ -10,7 +10,6 @@ */ namespace bitExpert\ForceCustomerLogin\Ui\Component\Listing\Column; -use \Magento\Framework\UrlInterface; use \Magento\Framework\View\Element\UiComponent\ContextInterface; use \Magento\Framework\View\Element\UiComponentFactory; use \Magento\Ui\Component\Listing\Columns\Column; diff --git a/Ui/Component/Listing/Column/StrategyName.php b/Ui/Component/Listing/Column/StrategyName.php index b330c9c..e2e76bb 100644 --- a/Ui/Component/Listing/Column/StrategyName.php +++ b/Ui/Component/Listing/Column/StrategyName.php @@ -11,7 +11,6 @@ namespace bitExpert\ForceCustomerLogin\Ui\Component\Listing\Column; use bitExpert\ForceCustomerLogin\Helper\Strategy\StrategyManager; -use \Magento\Framework\UrlInterface; use \Magento\Framework\View\Element\UiComponent\ContextInterface; use \Magento\Framework\View\Element\UiComponentFactory; use \Magento\Ui\Component\Listing\Columns\Column; diff --git a/phpunit.xml b/phpunit.xml index 14b0d48..e6f52df 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -23,9 +23,12 @@ ./vendor ./var ./build + ./Model/ResourceModel/WhitelistEntry/Collection.php + ./Setup ./Test ./logs ./registration.php + ./validate_m2_package.php