diff --git a/app/code/Magento/CatalogInventory/etc/di.xml b/app/code/Magento/CatalogInventory/etc/di.xml index cfd47ea146697a6e2dffaf2c1342bc7374c08a33..eb97aa454f6e8c63583bfd9cd4b6fec32b6d67e1 100644 --- a/app/code/Magento/CatalogInventory/etc/di.xml +++ b/app/code/Magento/CatalogInventory/etc/di.xml @@ -72,6 +72,6 @@ <plugin name="catalogInventoryAfterLoad" type="\Magento\CatalogInventory\Model\Plugin\AfterProductLoad"/> </type> <type name="Magento\Catalog\Api\ProductRepositoryInterface"> - <plugin name="catalogInventoryAroundSave" type="\Magento\CatalogInventory\Model\Plugin\AroundProductRepositorySave"/> + <plugin name="catalogInventoryAroundSave" sortOrder="20" type="Magento\CatalogInventory\Model\Plugin\AroundProductRepositorySave"/> </type> </config> diff --git a/app/code/Magento/CatalogSearch/Model/Search/ReaderPlugin.php b/app/code/Magento/CatalogSearch/Model/Search/ReaderPlugin.php index 21dd2f62495b2b7bf9b41c80beeed612a07ae47b..307b34c008fb238e6e79b87ec21a6a3a4099d431 100644 --- a/app/code/Magento/CatalogSearch/Model/Search/ReaderPlugin.php +++ b/app/code/Magento/CatalogSearch/Model/Search/ReaderPlugin.php @@ -24,18 +24,17 @@ class ReaderPlugin /** * Merge reader's value with generated * - * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @param \Magento\Framework\Config\ReaderInterface $subject - * @param \Closure $proceed - * @param string $scope + * @param array $result + * @param string|null $scope * @return array + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundRead( + public function afterRead( \Magento\Framework\Config\ReaderInterface $subject, - \Closure $proceed, + array $result, $scope = null ) { - $result = $proceed($scope); $result = array_merge_recursive($result, $this->requestGenerator->generate()); return $result; } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Search/ReaderPluginTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Search/ReaderPluginTest.php index 69644eee90222f09e7a3d9e74ee23292c4801070..afae3ccb4f4b595ed04141927b1e85279689988b 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Search/ReaderPluginTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Search/ReaderPluginTest.php @@ -29,18 +29,18 @@ class ReaderPluginTest extends \PHPUnit_Framework_TestCase ); } - public function testAroundRead() + public function testAfterRead() { + $readerConfig = ['test' => 'b', 'd' => 'e']; $this->requestGenerator->expects($this->once()) ->method('generate') ->will($this->returnValue(['test' => 'a'])); - $result = $this->object->aroundRead( + $result = $this->object->afterRead( $this->getMockBuilder(\Magento\Framework\Config\ReaderInterface::class) ->disableOriginalConstructor()->getMock(), - function () { - return ['test' => 'b', 'd' => 'e']; - } + $readerConfig, + null ); $this->assertEquals(['test' => ['b', 'a'], 'd' => 'e'], $result); diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Category/Move.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Category/Move.php index 1477a10655472d3e88543e0567dd2d6733af3d3c..4175a7ddb4ef9d9fd0f05d92d0d3d5bd53cfa374 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Category/Move.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Category/Move.php @@ -14,6 +14,11 @@ class Move /** @var CategoryUrlPathGenerator */ protected $categoryUrlPathGenerator; + /** + * @var ChildrenCategoriesProvider + */ + private $childrenCategoriesProvider; + /** * @param CategoryUrlPathGenerator $categoryUrlPathGenerator * @param ChildrenCategoriesProvider $childrenCategoriesProvider @@ -27,22 +32,23 @@ class Move } /** + * Perform url updating for children categories + * * @param \Magento\Catalog\Model\ResourceModel\Category $subject - * @param callable $proceed + * @param \Magento\Catalog\Model\ResourceModel\Category $result * @param Category $category * @param Category $newParent * @param null|int $afterCategoryId - * @return callable + * @return \Magento\Catalog\Model\ResourceModel\Category * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundChangeParent( + public function afterChangeParent( \Magento\Catalog\Model\ResourceModel\Category $subject, - \Closure $proceed, - $category, - $newParent, + \Magento\Catalog\Model\ResourceModel\Category $result, + Category $category, + Category $newParent, $afterCategoryId ) { - $result = $proceed($category, $newParent, $afterCategoryId); $category->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category)); $category->getResource()->saveAttribute($category, 'url_path'); $this->updateUrlPathForChildren($category); diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php index 768660e56daf5c25b6f8953bb2477aab97659d74..740c32762690e72eb588d7387f2a71467a15e2f7 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php @@ -33,14 +33,13 @@ class Storage /** * @param \Magento\UrlRewrite\Model\StorageInterface $object - * @param callable $proceed - * @param array $urls + * @param null $result + * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] $urls * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundReplace(StorageInterface $object, \Closure $proceed, array $urls) + public function afterReplace(StorageInterface $object, $result, array $urls) { - $proceed($urls); $toSave = []; foreach ($this->filterUrls($urls) as $record) { $metadata = $record->getMetadata(); diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/Group.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/Group.php index f3c86bad49936bc044d05a9fe5b6ca07ba293dad..1b18385d4a6fd8e9da1e693cf181fad8adfbd328 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/Group.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/Group.php @@ -63,19 +63,19 @@ class Group } /** - * @param \Magento\Store\Model\ResourceModel\Group $object - * @param callable $proceed + * Perform updating url for categories and products assigned to the group + * + * @param \Magento\Store\Model\ResourceModel\Group $subject + * @param \Magento\Store\Model\ResourceModel\Group $result * @param AbstractModel $group * @return \Magento\Store\Model\ResourceModel\Group * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundSave( - \Magento\Store\Model\ResourceModel\Group $object, - \Closure $proceed, + public function afterSave( + \Magento\Store\Model\ResourceModel\Group $subject, + \Magento\Store\Model\ResourceModel\Group $result, AbstractModel $group ) { - $originGroup = $group; - $result = $proceed($originGroup); if (!$group->isObjectNew() && ($group->dataHasChangedFor('website_id') || $group->dataHasChangedFor('root_category_id')) diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/View.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/View.php index ade02f8446921f74e34cb8bd3b58bc836575fe54..ea28b20f3f8f6d58211377bd9b16034d8f04a23c 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/View.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/View.php @@ -5,7 +5,6 @@ */ namespace Magento\CatalogUrlRewrite\Model\Category\Plugin\Store; -use Magento\Catalog\Model\Category; use Magento\Catalog\Model\CategoryFactory; use Magento\Catalog\Model\ProductFactory; use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; @@ -53,19 +52,19 @@ class View } /** - * @param \Magento\Store\Model\ResourceModel\Store $object - * @param callable $proceed + * Perform updating url for categories and products assigned to the store view + * + * @param \Magento\Store\Model\ResourceModel\Store $subject + * @param \Magento\Store\Model\ResourceModel\Store $result * @param AbstractModel $store * @return \Magento\Store\Model\ResourceModel\Store * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundSave( - \Magento\Store\Model\ResourceModel\Store $object, - \Closure $proceed, + public function afterSave( + \Magento\Store\Model\ResourceModel\Store $subject, + \Magento\Store\Model\ResourceModel\Store $result, AbstractModel $store ) { - $originStore = $store; - $result = $proceed($originStore); if ($store->isObjectNew() || $store->dataHasChangedFor('group_id')) { if (!$store->isObjectNew()) { $this->urlPersist->deleteByData([UrlRewrite::STORE_ID => $store->getId()]); @@ -102,6 +101,7 @@ class View ->addCategoryIds() ->addAttributeToSelect(['name', 'url_path', 'url_key', 'visibility']) ->addWebsiteFilter($websiteIds); + foreach ($collection as $product) { $product->setStoreId($storeId); /** @var \Magento\Catalog\Model\Product $product */ @@ -110,6 +110,7 @@ class View $this->productUrlRewriteGenerator->generate($product) ); } + return $urls; } @@ -130,23 +131,26 @@ class View $this->categoryUrlRewriteGenerator->generate($category) ); } + return $urls; } /** - * @param \Magento\Store\Model\ResourceModel\Store $object - * @param callable $proceed + * Delete unused url rewrites + * + * @param \Magento\Store\Model\ResourceModel\Store $subject + * @param \Magento\Store\Model\ResourceModel\Store $result * @param AbstractModel $store - * @return mixed + * @return \Magento\Store\Model\ResourceModel\Store * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundDelete( - \Magento\Store\Model\ResourceModel\Store $object, - \Closure $proceed, + public function afterDelete( + \Magento\Store\Model\ResourceModel\Store $subject, + \Magento\Store\Model\ResourceModel\Store $result, AbstractModel $store ) { - $result = $proceed($store); $this->urlPersist->deleteByData([UrlRewrite::STORE_ID => $store->getId()]); + return $result; } } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Category/MoveTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Category/MoveTest.php new file mode 100644 index 0000000000000000000000000000000000000000..7fe042232b82d85146365136f54b02f1a1842818 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Category/MoveTest.php @@ -0,0 +1,105 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Category\Plugin\Category; + +use Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\Move as CategoryMovePlugin; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator; +use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider; +use Magento\Catalog\Model\ResourceModel\Category as CategoryResourceModel; +use Magento\Catalog\Model\Category; + +class MoveTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var ObjectManager + */ + private $objectManager; + + /** + * @var ChildrenCategoriesProvider|\PHPUnit_Framework_MockObject_MockObject + */ + private $childrenCategoriesProviderMock; + + /** + * @var CategoryUrlPathGenerator|\PHPUnit_Framework_MockObject_MockObject + */ + private $categoryUrlPathGeneratorMock; + + /** + * @var CategoryResourceModel|\PHPUnit_Framework_MockObject_MockObject + */ + private $subjectMock; + + /** + * @var Category|\PHPUnit_Framework_MockObject_MockObject + */ + private $categoryMock; + + /** + * @var CategoryMovePlugin + */ + private $plugin; + + protected function setUp() + { + $this->objectManager = new ObjectManager($this); + $this->categoryUrlPathGeneratorMock = $this->getMockBuilder(CategoryUrlPathGenerator::class) + ->disableOriginalConstructor() + ->setMethods(['getUrlPath']) + ->getMock(); + $this->childrenCategoriesProviderMock = $this->getMockBuilder(ChildrenCategoriesProvider::class) + ->disableOriginalConstructor() + ->setMethods(['getChildren']) + ->getMock(); + $this->subjectMock = $this->getMockBuilder(CategoryResourceModel::class) + ->disableOriginalConstructor() + ->getMock(); + $this->categoryMock = $this->getMockBuilder(Category::class) + ->disableOriginalConstructor() + ->setMethods(['getResource', 'setUrlPath']) + ->getMock(); + $this->plugin = $this->objectManager->getObject( + CategoryMovePlugin::class, + [ + 'categoryUrlPathGenerator' => $this->categoryUrlPathGeneratorMock, + 'childrenCategoriesProvider' => $this->childrenCategoriesProviderMock + ] + ); + } + + public function testAfterChangeParent() + { + $urlPath = 'test/path'; + $this->categoryMock->expects($this->once()) + ->method('getResource') + ->willReturn($this->subjectMock); + $this->childrenCategoriesProviderMock->expects($this->once()) + ->method('getChildren') + ->with($this->categoryMock, true) + ->willReturn([]); + $this->categoryUrlPathGeneratorMock->expects($this->once()) + ->method('getUrlPath') + ->with($this->categoryMock) + ->willReturn($urlPath); + $this->categoryMock->expects($this->once()) + ->method('getResource') + ->willReturn($this->subjectMock); + $this->categoryMock->expects($this->once()) + ->method('setUrlPath') + ->with($urlPath); + $this->assertSame( + $this->subjectMock, + $this->plugin->afterChangeParent( + $this->subjectMock, + $this->subjectMock, + $this->categoryMock, + $this->categoryMock, + null + ) + ); + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Category/RemoveTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Category/RemoveTest.php new file mode 100644 index 0000000000000000000000000000000000000000..520975871bf4b657a408b399c144b49353998959 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Category/RemoveTest.php @@ -0,0 +1,84 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Category\Plugin\Category; + +use Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\Remove as CategoryRemovePlugin; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\UrlRewrite\Model\UrlPersistInterface; +use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider; +use Magento\Catalog\Model\ResourceModel\Category as CategoryResourceModel; +use Magento\Catalog\Model\Category; + +class RemoveTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var ObjectManager + */ + private $objectManager; + + /** + * @var UrlPersistInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $urlPersistMock; + + /** + * @var ChildrenCategoriesProvider|\PHPUnit_Framework_MockObject_MockObject + */ + private $childrenCategoriesProviderMock; + + /** + * @var CategoryResourceModel|\PHPUnit_Framework_MockObject_MockObject + */ + private $subjectMock; + + /** + * @var Category|\PHPUnit_Framework_MockObject_MockObject + */ + private $objectMock; + + protected function setUp() + { + $this->objectManager = new ObjectManager($this); + $this->urlPersistMock = $this->getMockBuilder(UrlPersistInterface::class) + ->getMockForAbstractClass(); + $this->childrenCategoriesProviderMock = $this->getMockBuilder(ChildrenCategoriesProvider::class) + ->getMock(); + $this->subjectMock = $this->getMockBuilder(CategoryResourceModel::class) + ->disableOriginalConstructor() + ->getMock(); + $this->objectMock = $this->getMockBuilder(Category::class) + ->disableOriginalConstructor() + ->getMock(); + } + + public function testAroundDelete() + { + $closureSubject = $this->subjectMock; + $proceed = function () use ($closureSubject) { + return $closureSubject; + }; + $plugin = $this->objectManager->getObject( + CategoryRemovePlugin::class, + [ + 'urlPersist' => $this->urlPersistMock, + 'childrenCategoriesProvider' => $this->childrenCategoriesProviderMock + ] + ); + $this->childrenCategoriesProviderMock->expects($this->once()) + ->method('getChildrenIds') + ->with($this->objectMock, true) + ->willReturn([]); + $this->objectMock->expects($this->once()) + ->method('getId') + ->willReturn(1); + $this->urlPersistMock->expects($this->exactly(2)) + ->method('deleteByData'); + $this->assertSame( + $this->subjectMock, + $plugin->aroundDelete($this->subjectMock, $proceed, $this->objectMock) + ); + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/StorageTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/StorageTest.php new file mode 100644 index 0000000000000000000000000000000000000000..21ab961dcbdcb3b1c5eb08d5d59bd176b87c321a --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/StorageTest.php @@ -0,0 +1,106 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Category\Plugin; + +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\CatalogUrlRewrite\Model\Category\ProductFactory; +use Magento\UrlRewrite\Model\StorageInterface; +use Magento\CatalogUrlRewrite\Model\Category\Plugin\Storage as CategoryStoragePlugin; +use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; +use Magento\UrlRewrite\Model\UrlFinderInterface; +use Magento\CatalogUrlRewrite\Model\Category\Product; +use Magento\CatalogUrlRewrite\Model\ResourceModel\Category\Product as ProductResourceModel; + +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ +class StorageTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var CategoryStoragePlugin + */ + private $plugin; + + /** + * @var ProductFactory|\PHPUnit_Framework_MockObject_MockObject + */ + private $productFactory; + + /** + * @var UrlFinderInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $urlFinder; + + /** + * @var StorageInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $storage; + + /** + * @var Product|\PHPUnit_Framework_MockObject_MockObject + */ + private $product; + + /** + * @var ProductResourceModel|\PHPUnit_Framework_MockObject_MockObject + */ + private $productResourceModel; + + /** + * @var UrlRewrite|\PHPUnit_Framework_MockObject_MockObject + */ + private $urlRewrite; + + protected function setUp() + { + $this->productFactory = $this->getMockBuilder(ProductFactory::class) + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $this->storage = $this->getMockBuilder(StorageInterface::class) + ->getMockForAbstractClass(); + $this->urlFinder = $this->getMockBuilder(UrlFinderInterface::class) + ->getMockForAbstractClass(); + $this->product = $this->getMockBuilder(Product::class) + ->disableOriginalConstructor() + ->getMock(); + $this->productResourceModel = $this->getMockBuilder(ProductResourceModel::class) + ->disableOriginalConstructor() + ->getMock(); + $this->urlRewrite = $this->getMockBuilder(UrlRewrite::class) + ->disableOriginalConstructor() + ->setMethods(['getMetadata', 'getEntityType', 'getIsAutogenerated', 'getUrlRewriteId', 'getEntityId']) + ->getMock(); + + $this->plugin = (new ObjectManager($this))->getObject( + CategoryStoragePlugin::class, + [ + 'productFactory' => $this->productFactory, + 'urlFinder' => $this->urlFinder + ] + ); + } + + public function testAfterReplace() + { + $this->urlRewrite->expects(static::any())->method('getMetadata')->willReturn(['category_id' => '5']); + $this->urlRewrite->expects(static::once())->method('getEntityTYpe')->willReturn('product'); + $this->urlRewrite->expects(static::once())->method('getIsAutogenerated')->willReturn(1); + $this->urlRewrite->expects(static::once())->method('getUrlRewriteId')->willReturn('4'); + $this->urlRewrite->expects(static::once())->method('getEntityId')->willReturn('2'); + $this->urlRewrite->setData('request_path', 'test'); + $this->urlRewrite->setData('store_id', '1'); + $productUrls = ['targetPath' => $this->urlRewrite]; + + $this->urlFinder->expects(static::once())->method('findAllByData')->willReturn([$this->urlRewrite]); + + $this->productFactory->expects(static::once())->method('create')->willReturn($this->product); + $this->product->expects(static::once())->method('getResource')->willReturn($this->productResourceModel); + $this->productResourceModel->expects(static::once())->method('saveMultiple')->willReturnSelf(); + + $this->plugin->afterReplace($this->storage, null, $productUrls); + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Store/GroupTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Store/GroupTest.php new file mode 100644 index 0000000000000000000000000000000000000000..dc69597ef6db9ccd1299c6e7887239c890bb05e5 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Store/GroupTest.php @@ -0,0 +1,178 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Category\Plugin\Store; + +use Magento\CatalogUrlRewrite\Model\Category\Plugin\Store\Group as GroupPlugin; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\Framework\Model\AbstractModel; +use Magento\Store\Model\StoreManagerInterface; +use Magento\Store\Model\ResourceModel\Group; +use Magento\Catalog\Model\Category; +use Magento\Catalog\Model\CategoryFactory; +use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection; +use Magento\Catalog\Model\Product as Product; +use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; +use Magento\Catalog\Model\ProductFactory; + +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ +class GroupTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var ObjectManager + */ + private $objectManager; + + /** + * @var GroupPlugin + */ + private $plugin; + + /** + * @var AbstractModel|\PHPUnit_Framework_MockObject_MockObject + */ + private $abstractModelMock; + + /** + * @var Group|\PHPUnit_Framework_MockObject_MockObject + */ + private $subjectMock; + + /** + * @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $storeManagerMock; + + /** + * @var CategoryFactory|\PHPUnit_Framework_MockObject_MockObject + */ + private $categoryFactoryMock; + + /** + * @var Category|\PHPUnit_Framework_MockObject_MockObject + */ + private $categoryMock; + + /** + * @var ProductCollection|\PHPUnit_Framework_MockObject_MockObject + */ + private $productCollectionMock; + + /** + * @var Product|\PHPUnit_Framework_MockObject_MockObject + */ + private $productMock; + + /** + * @var ProductFactory|\PHPUnit_Framework_MockObject_MockObject + */ + private $productFactoryMock; + + /** + * @var ProductUrlRewriteGenerator|\PHPUnit_Framework_MockObject_MockObject + */ + private $productUrlRewriteGeneratorMock; + + protected function setUp() + { + $this->objectManager = new ObjectManager($this); + $this->abstractModelMock = $this->getMockBuilder(AbstractModel::class) + ->disableOriginalConstructor() + ->setMethods(['isObjectNew', 'dataHasChangedFor', 'getStoreIds']) + ->getMockForAbstractClass(); + $this->abstractModelMock->expects($this->any()) + ->method('getStoreIds') + ->willReturn([]); + $this->subjectMock = $this->getMockBuilder(Group::class) + ->disableOriginalConstructor() + ->getMock(); + $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class) + ->disableOriginalConstructor() + ->setMethods(['reinitStores']) + ->getMockForAbstractClass(); + $this->categoryMock = $this->getMockBuilder(Category::class) + ->disableOriginalConstructor() + ->setMethods(['getCategories']) + ->getMock(); + $this->categoryFactoryMock = $this->getMockBuilder(CategoryFactory::class) + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $this->productFactoryMock = $this->getMockBuilder(ProductFactory::class) + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $this->productCollectionMock = $this->getMockBuilder(ProductCollection::class) + ->disableOriginalConstructor() + ->setMethods(['addCategoryIds', 'addAttributeToSelect', 'addWebsiteFilter', 'getIterator']) + ->getMock(); + $this->productMock = $this->getMockBuilder(Product::class) + ->disableOriginalConstructor() + ->setMethods(['getCollection']) + ->getMock(); + $this->productUrlRewriteGeneratorMock = $this->getMockBuilder(ProductUrlRewriteGenerator::class) + ->disableOriginalConstructor() + ->setMethods(['generate']) + ->getMock(); + $this->plugin = $this->objectManager->getObject( + GroupPlugin::class, + [ + 'storeManager' => $this->storeManagerMock, + 'categoryFactory' => $this->categoryFactoryMock, + 'productFactory' => $this->productFactoryMock, + 'productUrlRewriteGenerator' => $this->productUrlRewriteGeneratorMock + ] + ); + } + + public function testAfterSave() + { + $this->abstractModelMock->expects($this->once()) + ->method('isObjectNew') + ->willReturn(false); + $this->abstractModelMock->expects($this->once()) + ->method('dataHasChangedFor') + ->with('website_id') + ->willReturn(true); + $this->storeManagerMock->expects($this->once()) + ->method('reinitStores'); + $this->categoryMock->expects($this->once()) + ->method('getCategories') + ->willReturn([]); + $this->categoryFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($this->categoryMock); + $this->productFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($this->productMock); + $this->productMock->expects($this->once()) + ->method('getCollection') + ->willReturn($this->productCollectionMock); + $this->productCollectionMock->expects($this->once()) + ->method('addCategoryIds') + ->willReturn($this->productCollectionMock); + $this->productCollectionMock->expects($this->once()) + ->method('addAttributeToSelect') + ->willReturn($this->productCollectionMock); + $this->productCollectionMock->expects($this->once()) + ->method('addWebsiteFilter') + ->willReturn($this->productCollectionMock); + $iterator = new \ArrayIterator([$this->productMock]); + $this->productCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn($iterator); + $this->productUrlRewriteGeneratorMock->expects($this->once()) + ->method('generate') + ->with($this->productMock) + ->willReturn([]); + + $this->assertSame( + $this->subjectMock, + $this->plugin->afterSave($this->subjectMock, $this->subjectMock, $this->abstractModelMock) + ); + } +} diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Store/ViewTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Store/ViewTest.php new file mode 100644 index 0000000000000000000000000000000000000000..d30c2dde6e033fd3f3d10300ed59aa7a6c249916 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Store/ViewTest.php @@ -0,0 +1,188 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Category\Plugin\Store; + +use Magento\CatalogUrlRewrite\Model\Category\Plugin\Store\View as StoreViewPlugin; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\Framework\Model\AbstractModel; +use Magento\Store\Model\ResourceModel\Store; +use Magento\UrlRewrite\Model\UrlPersistInterface; +use Magento\Catalog\Model\CategoryFactory; +use Magento\Catalog\Model\ProductFactory; +use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; +use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; +use Magento\Catalog\Model\Category; +use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection; +use Magento\Catalog\Model\Product; + +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ +class ViewTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var ObjectManager + */ + private $objectManager; + + /** + * @var StoreViewPlugin + */ + private $plugin; + + /** + * @var AbstractModel|\PHPUnit_Framework_MockObject_MockObject + */ + private $abstractModelMock; + + /** + * @var Store|\PHPUnit_Framework_MockObject_MockObject + */ + private $subjectMock; + + /** + * @var UrlPersistInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $urlPersistMock; + + /** + * @var CategoryFactory|\PHPUnit_Framework_MockObject_MockObject + */ + private $categoryFactoryMock; + + /** + * @var ProductFactory|\PHPUnit_Framework_MockObject_MockObject + */ + private $productFactoryMock; + + /** + * @var CategoryUrlRewriteGenerator|\PHPUnit_Framework_MockObject_MockObject + */ + private $categoryUrlRewriteGeneratorMock; + + /** + * @var ProductUrlRewriteGenerator|\PHPUnit_Framework_MockObject_MockObject + */ + private $productUrlRewriteGeneratorMock; + + /** + * @var Category|\PHPUnit_Framework_MockObject_MockObject + */ + private $categoryMock; + + /** + * @var ProductCollection|\PHPUnit_Framework_MockObject_MockObject + */ + private $productCollectionMock; + + /** + * @var Product|\PHPUnit_Framework_MockObject_MockObject + */ + private $productMock; + + protected function setUp() + { + $this->objectManager = new ObjectManager($this); + $this->abstractModelMock = $this->getMockBuilder(AbstractModel::class) + ->disableOriginalConstructor() + ->setMethods(['isObjectNew']) + ->getMockForAbstractClass(); + $this->subjectMock = $this->getMockBuilder(Store::class) + ->disableOriginalConstructor() + ->getMock(); + $this->urlPersistMock = $this->getMockBuilder(UrlPersistInterface::class) + ->setMethods(['deleteByData']) + ->getMockForAbstractClass(); + $this->categoryMock = $this->getMockBuilder(Category::class) + ->disableOriginalConstructor() + ->setMethods(['getCategories']) + ->getMock(); + $this->categoryFactoryMock = $this->getMockBuilder(CategoryFactory::class) + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $this->productFactoryMock = $this->getMockBuilder(ProductFactory::class) + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $this->categoryUrlRewriteGeneratorMock = $this->getMockBuilder(CategoryUrlRewriteGenerator::class) + ->disableOriginalConstructor() + ->getMock(); + $this->productUrlRewriteGeneratorMock = $this->getMockBuilder(ProductUrlRewriteGenerator::class) + ->disableOriginalConstructor() + ->setMethods(['generate']) + ->getMock(); + $this->productCollectionMock = $this->getMockBuilder(ProductCollection::class) + ->disableOriginalConstructor() + ->setMethods(['addCategoryIds', 'addAttributeToSelect', 'addWebsiteFilter', 'getIterator']) + ->getMock(); + $this->productMock = $this->getMockBuilder(Product::class) + ->disableOriginalConstructor() + ->setMethods(['getCollection']) + ->getMock(); + $this->plugin = $this->objectManager->getObject( + StoreViewPlugin::class, + [ + 'urlPersist' => $this->urlPersistMock, + 'categoryFactory' => $this->categoryFactoryMock, + 'productFactory' => $this->productFactoryMock, + 'categoryUrlRewriteGenerator' => $this->categoryUrlRewriteGeneratorMock, + 'productUrlRewriteGenerator' => $this->productUrlRewriteGeneratorMock + ] + ); + } + + public function testAfterSave() + { + $this->abstractModelMock->expects($this->any()) + ->method('isObjectNew') + ->willReturn(true); + $this->categoryMock->expects($this->once()) + ->method('getCategories') + ->willReturn([]); + $this->categoryFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($this->categoryMock); + $this->productFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($this->productMock); + $this->productMock->expects($this->once()) + ->method('getCollection') + ->willReturn($this->productCollectionMock); + $this->productCollectionMock->expects($this->once()) + ->method('addCategoryIds') + ->willReturn($this->productCollectionMock); + $this->productCollectionMock->expects($this->once()) + ->method('addAttributeToSelect') + ->willReturn($this->productCollectionMock); + $this->productCollectionMock->expects($this->once()) + ->method('addWebsiteFilter') + ->willReturn($this->productCollectionMock); + $iterator = new \ArrayIterator([$this->productMock]); + $this->productCollectionMock->expects($this->once()) + ->method('getIterator') + ->willReturn($iterator); + $this->productUrlRewriteGeneratorMock->expects($this->once()) + ->method('generate') + ->with($this->productMock) + ->willReturn([]); + + $this->assertSame( + $this->subjectMock, + $this->plugin->afterSave($this->subjectMock, $this->subjectMock, $this->abstractModelMock) + ); + } + + public function testAfterDelete() + { + $this->urlPersistMock->expects($this->once()) + ->method('deleteByData'); + $this->assertSame( + $this->subjectMock, + $this->plugin->afterDelete($this->subjectMock, $this->subjectMock, $this->abstractModelMock) + ); + } +} diff --git a/app/code/Magento/CmsUrlRewrite/Plugin/Cms/Model/ResourceModel/Page.php b/app/code/Magento/CmsUrlRewrite/Plugin/Cms/Model/ResourceModel/Page.php index 45579f37b0c0cdeac82c14ff40f7e81eab227c9d..c605d3977d9f2a79f1f8d9d6d5b3cd04b241d14f 100644 --- a/app/code/Magento/CmsUrlRewrite/Plugin/Cms/Model/ResourceModel/Page.php +++ b/app/code/Magento/CmsUrlRewrite/Plugin/Cms/Model/ResourceModel/Page.php @@ -9,6 +9,8 @@ use Magento\UrlRewrite\Model\UrlPersistInterface; use Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator; use Magento\CmsUrlRewrite\Model\CmsPageUrlRewriteGenerator; use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; +use Magento\Framework\Model\AbstractModel; +use Magento\Framework\Model\ResourceModel\Db\AbstractDb; /** * Before save and around delete plugin for \Magento\Cms\Model\ResourceModel\Page: @@ -63,18 +65,16 @@ class Page * On delete handler to remove related url rewrites * * @param \Magento\Cms\Model\ResourceModel\Page $subject - * @param \Closure $proceed - * @param \Magento\Framework\Model\AbstractModel $page - * @return \Magento\Cms\Model\ResourceModel\Page - * + * @param AbstractDb $result + * @param AbstractModel $page + * @return AbstractDb * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundDelete( + public function afterDelete( \Magento\Cms\Model\ResourceModel\Page $subject, - \Closure $proceed, - \Magento\Framework\Model\AbstractModel $page + AbstractDb $result, + AbstractModel $page ) { - $result = $proceed($page); if ($page->isDeleted()) { $this->urlPersist->deleteByData( [ diff --git a/app/code/Magento/CmsUrlRewrite/Test/Unit/Plugin/Cms/Model/ResourceModel/PageTest.php b/app/code/Magento/CmsUrlRewrite/Test/Unit/Plugin/Cms/Model/ResourceModel/PageTest.php index 42b1b42469d5e0ee748a9795b8d3ac91ab54adc9..277e36b94fc8360c1a4bd4aec192e7cea94cc58d 100644 --- a/app/code/Magento/CmsUrlRewrite/Test/Unit/Plugin/Cms/Model/ResourceModel/PageTest.php +++ b/app/code/Magento/CmsUrlRewrite/Test/Unit/Plugin/Cms/Model/ResourceModel/PageTest.php @@ -30,19 +30,10 @@ class PageTest extends \PHPUnit_Framework_TestCase */ protected $cmsPageResourceMock; - /** - * @var \Closure - */ - protected $closureMock; - protected function setUp() { $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->closureMock = function () { - return 'URL Rewrite Result'; - }; - $this->urlPersistMock = $this->getMockBuilder(\Magento\UrlRewrite\Model\UrlPersistInterface::class) ->getMockForAbstractClass(); @@ -62,7 +53,7 @@ class PageTest extends \PHPUnit_Framework_TestCase ); } - public function testAroundDeletePositive() + public function testAfterDeletePositive() { $productId = 100; @@ -83,17 +74,17 @@ class PageTest extends \PHPUnit_Framework_TestCase ] ); - $this->assertEquals( - 'URL Rewrite Result', - $this->pageObject->aroundDelete( + $this->assertSame( + $this->cmsPageResourceMock, + $this->pageObject->afterDelete( + $this->cmsPageResourceMock, $this->cmsPageResourceMock, - $this->closureMock, $this->cmsPageMock ) ); } - public function testAroundDeleteNegative() + public function testAfterDeleteNegative() { $this->cmsPageMock->expects($this->once()) ->method('isDeleted') @@ -102,11 +93,11 @@ class PageTest extends \PHPUnit_Framework_TestCase $this->urlPersistMock->expects($this->never()) ->method('deleteByData'); - $this->assertEquals( - 'URL Rewrite Result', - $this->pageObject->aroundDelete( + $this->assertSame( + $this->cmsPageResourceMock, + $this->pageObject->afterDelete( + $this->cmsPageResourceMock, $this->cmsPageResourceMock, - $this->closureMock, $this->cmsPageMock ) ); diff --git a/app/code/Magento/ConfigurableProduct/Controller/Adminhtml/Product/Builder/Plugin.php b/app/code/Magento/ConfigurableProduct/Controller/Adminhtml/Product/Builder/Plugin.php index ada1488e9cd9ce71e4b8b7b82afd795922845adb..354ebdb0ea7c94bcb639f0e4329d8bd2e6aa0ff7 100644 --- a/app/code/Magento/ConfigurableProduct/Controller/Adminhtml/Product/Builder/Plugin.php +++ b/app/code/Magento/ConfigurableProduct/Controller/Adminhtml/Product/Builder/Plugin.php @@ -8,6 +8,9 @@ namespace Magento\ConfigurableProduct\Controller\Adminhtml\Product\Builder; use Magento\Catalog\Model\ProductFactory; use Magento\ConfigurableProduct\Model\Product\Type; +use Magento\Catalog\Model\Product; +use Magento\Catalog\Controller\Adminhtml\Product\Builder as CatalogProductBuilder; +use Magento\Framework\App\RequestInterface; class Plugin { @@ -32,21 +35,17 @@ class Plugin } /** - * @param \Magento\Catalog\Controller\Adminhtml\Product\Builder $subject - * @param callable $proceed - * @param \Magento\Framework\App\RequestInterface $request + * Set type and data to configurable product * - * @return \Magento\Catalog\Model\Product + * @param CatalogProductBuilder $subject + * @param Product $product + * @param RequestInterface $request + * @return Product * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ - public function aroundBuild( - \Magento\Catalog\Controller\Adminhtml\Product\Builder $subject, - \Closure $proceed, - \Magento\Framework\App\RequestInterface $request - ) { - $product = $proceed($request); - + public function afterBuild(CatalogProductBuilder $subject, Product $product, RequestInterface $request) + { if ($request->has('attributes')) { $attributes = $request->getParam('attributes'); if (!empty($attributes)) { diff --git a/app/code/Magento/ConfigurableProduct/Model/Plugin/AroundProductRepositorySave.php b/app/code/Magento/ConfigurableProduct/Model/Plugin/ProductRepositorySave.php similarity index 93% rename from app/code/Magento/ConfigurableProduct/Model/Plugin/AroundProductRepositorySave.php rename to app/code/Magento/ConfigurableProduct/Model/Plugin/ProductRepositorySave.php index be12fc8da2904165f22a16b5750e20ac8c3ef37d..cfcb5e41799e8fda8239c32f2a67dd3dc0ebcd04 100644 --- a/app/code/Magento/ConfigurableProduct/Model/Plugin/AroundProductRepositorySave.php +++ b/app/code/Magento/ConfigurableProduct/Model/Plugin/ProductRepositorySave.php @@ -14,10 +14,7 @@ use Magento\ConfigurableProduct\Api\Data\OptionInterface; use Magento\Catalog\Api\ProductAttributeRepositoryInterface; use Magento\ConfigurableProduct\Model\Product\Type\Configurable; -/** - * Class AroundProductRepositorySave - */ -class AroundProductRepositorySave +class ProductRepositorySave { /** * @var ProductAttributeRepositoryInterface @@ -42,8 +39,10 @@ class AroundProductRepositorySave } /** + * Validate product links and reset configurable attributes to configurable product + * * @param ProductRepositoryInterface $subject - * @param callable $proceed + * @param ProductInterface $result * @param ProductInterface $product * @param bool $saveOptions * @return ProductInterface @@ -52,14 +51,12 @@ class AroundProductRepositorySave * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundSave( + public function afterSave( ProductRepositoryInterface $subject, - \Closure $proceed, + ProductInterface $result, ProductInterface $product, $saveOptions = false ) { - /** @var ProductInterface $result */ - $result = $proceed($product, $saveOptions); if ($product->getTypeId() !== Configurable::TYPE_CODE) { return $result; } diff --git a/app/code/Magento/ConfigurableProduct/Model/Product/Validator/Plugin.php b/app/code/Magento/ConfigurableProduct/Model/Product/Validator/Plugin.php index c0b287214ea3d32ea80beebe2dbe4cff865812d8..b5970c902743ee8c5959dca2a2922502c169a2d2 100644 --- a/app/code/Magento/ConfigurableProduct/Model/Product/Validator/Plugin.php +++ b/app/code/Magento/ConfigurableProduct/Model/Product/Validator/Plugin.php @@ -5,12 +5,12 @@ */ namespace Magento\ConfigurableProduct\Model\Product\Validator; -use Closure; use Magento\Catalog\Model\Product; use Magento\Catalog\Model\ProductFactory; use Magento\Framework\App\RequestInterface; use Magento\Framework\Event\Manager; use Magento\Framework\Json\Helper\Data; +use Magento\Framework\DataObject; /** * Configurable product validation @@ -48,27 +48,44 @@ class Plugin } /** - * Validate product data + * Set configurable type to product * * @param Product\Validator $subject - * @param Closure $proceed * @param Product $product * @param RequestInterface $request - * @param \Magento\Framework\DataObject $response - * @return bool + * @param DataObject $response + * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundValidate( + public function beforeValidate( \Magento\Catalog\Model\Product\Validator $subject, - Closure $proceed, \Magento\Catalog\Model\Product $product, - \Magento\Framework\App\RequestInterface $request, - \Magento\Framework\DataObject $response + RequestInterface $request, + DataObject $response ) { if ($request->has('attributes')) { $product->setTypeId(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE); } - $result = $proceed($product, $request, $response); + } + + /** + * Validate product data + * + * @param Product\Validator $subject + * @param bool|array $result + * @param Product $product + * @param RequestInterface $request + * @param \Magento\Framework\DataObject $response + * @return bool + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function afterValidate( + \Magento\Catalog\Model\Product\Validator $subject, + $result, + \Magento\Catalog\Model\Product $product, + RequestInterface $request, + DataObject $response + ) { $variationProducts = (array)$request->getPost('variations-matrix'); if ($variationProducts) { $validationResult = $this->_validateProductVariations($product, $variationProducts, $request); diff --git a/app/code/Magento/ConfigurableProduct/Model/Quote/Item/QuantityValidator/Initializer/Option/Plugin/ConfigurableProduct.php b/app/code/Magento/ConfigurableProduct/Model/Quote/Item/QuantityValidator/Initializer/Option/Plugin/ConfigurableProduct.php index 73e88eb5333cb4ef62eef43e2aae5d02d9e53294..1657fb8aaae12788c217cd124a25a46d97ea3b0b 100644 --- a/app/code/Magento/ConfigurableProduct/Model/Quote/Item/QuantityValidator/Initializer/Option/Plugin/ConfigurableProduct.php +++ b/app/code/Magento/ConfigurableProduct/Model/Quote/Item/QuantityValidator/Initializer/Option/Plugin/ConfigurableProduct.php @@ -13,20 +13,19 @@ class ConfigurableProduct * Initialize stock item for configurable product type * * @param \Magento\CatalogInventory\Model\Quote\Item\QuantityValidator\Initializer\Option $subject - * @param callable $proceed + * @param \Magento\CatalogInventory\Model\Stock\Item $stockItem * @param \Magento\Quote\Model\Quote\Item\Option $option * @param \Magento\Quote\Model\Quote\Item $quoteItem * * @return \Magento\CatalogInventory\Api\Data\StockItemInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundGetStockItem( + public function afterGetStockItem( \Magento\CatalogInventory\Model\Quote\Item\QuantityValidator\Initializer\Option $subject, - \Closure $proceed, + \Magento\CatalogInventory\Model\Stock\Item $stockItem, \Magento\Quote\Model\Quote\Item\Option $option, \Magento\Quote\Model\Quote\Item $quoteItem ) { - $stockItem = $proceed($option, $quoteItem); if ($quoteItem->getProductType() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) { $stockItem->setProductName($quoteItem->getName()); } diff --git a/app/code/Magento/ConfigurableProduct/Test/Unit/Controller/Adminhtml/Product/Builder/PluginTest.php b/app/code/Magento/ConfigurableProduct/Test/Unit/Controller/Adminhtml/Product/Builder/PluginTest.php index 7056224cffd6843ea3dd35729cd5add8c01dc6ea..e6c13a23541fad21e5d592d4b0b9dc7bb32e7ed0 100644 --- a/app/code/Magento/ConfigurableProduct/Test/Unit/Controller/Adminhtml/Product/Builder/PluginTest.php +++ b/app/code/Magento/ConfigurableProduct/Test/Unit/Controller/Adminhtml/Product/Builder/PluginTest.php @@ -23,12 +23,12 @@ class PluginTest extends \PHPUnit_Framework_TestCase protected $configurableTypeMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject */ protected $requestMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */ protected $productMock; @@ -48,15 +48,10 @@ class PluginTest extends \PHPUnit_Framework_TestCase protected $frontendAttrMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Catalog\Controller\Adminhtml\Product\Builder|\PHPUnit_Framework_MockObject_MockObject */ protected $subjectMock; - /** - * @var \Closure - */ - protected $closureMock; - protected function setUp() { $this->productFactoryMock = $this->getMock( @@ -76,7 +71,6 @@ class PluginTest extends \PHPUnit_Framework_TestCase $this->requestMock = $this->getMock(\Magento\Framework\App\Request\Http::class, [], [], '', false); $methods = ['setTypeId', 'getAttributes', 'addData', 'setWebsiteIds', '__wakeup']; $this->productMock = $this->getMock(\Magento\Catalog\Model\Product::class, $methods, [], '', false); - $product = $this->productMock; $attributeMethods = [ 'getId', 'getFrontend', @@ -124,9 +118,6 @@ class PluginTest extends \PHPUnit_Framework_TestCase '', false ); - $this->closureMock = function () use ($product) { - return $product; - }; $this->plugin = new \Magento\ConfigurableProduct\Controller\Adminhtml\Product\Builder\Plugin( $this->productFactoryMock, $this->configurableTypeMock @@ -137,7 +128,7 @@ class PluginTest extends \PHPUnit_Framework_TestCase * @return void * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ - public function testAroundBuild() + public function testAfterBuild() { $this->requestMock->expects($this->once())->method('has')->with('attributes')->will($this->returnValue(true)); $valueMap = [ @@ -256,11 +247,11 @@ class PluginTest extends \PHPUnit_Framework_TestCase $this->assertEquals( $this->productMock, - $this->plugin->aroundBuild($this->subjectMock, $this->closureMock, $this->requestMock) + $this->plugin->afterBuild($this->subjectMock, $this->productMock, $this->requestMock) ); } - public function testAroundBuildWhenProductNotHaveAttributeAndRequiredParameters() + public function testAfterBuildWhenProductNotHaveAttributeAndRequiredParameters() { $valueMap = [ ['attributes', null, null], @@ -283,11 +274,11 @@ class PluginTest extends \PHPUnit_Framework_TestCase $this->attributeMock->expects($this->never())->method('getAttributeCode'); $this->assertEquals( $this->productMock, - $this->plugin->aroundBuild($this->subjectMock, $this->closureMock, $this->requestMock) + $this->plugin->afterBuild($this->subjectMock, $this->productMock, $this->requestMock) ); } - public function testAroundBuildWhenAttributesAreEmpty() + public function testAfterBuildWhenAttributesAreEmpty() { $valueMap = [['popup', null, false], ['product', null, 'product'], ['id', false, false]]; $this->requestMock->expects($this->once())->method('has')->with('attributes')->will($this->returnValue(false)); @@ -299,7 +290,7 @@ class PluginTest extends \PHPUnit_Framework_TestCase $this->attributeMock->expects($this->never())->method('getAttributeCode'); $this->assertEquals( $this->productMock, - $this->plugin->aroundBuild($this->subjectMock, $this->closureMock, $this->requestMock) + $this->plugin->afterBuild($this->subjectMock, $this->productMock, $this->requestMock) ); } } diff --git a/app/code/Magento/ConfigurableProduct/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php b/app/code/Magento/ConfigurableProduct/Test/Unit/Model/Plugin/ProductRepositorySaveTest.php similarity index 87% rename from app/code/Magento/ConfigurableProduct/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php rename to app/code/Magento/ConfigurableProduct/Test/Unit/Model/Plugin/ProductRepositorySaveTest.php index dee340fca9d8688aee08904078039ad3f82b4849..3b2069296db9a4e20ae75c010fdca7000e53e403 100644 --- a/app/code/Magento/ConfigurableProduct/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php +++ b/app/code/Magento/ConfigurableProduct/Test/Unit/Model/Plugin/ProductRepositorySaveTest.php @@ -11,16 +11,17 @@ use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Catalog\Model\Product; use Magento\Catalog\Model\ProductFactory; use Magento\ConfigurableProduct\Api\Data\OptionInterface; -use Magento\ConfigurableProduct\Model\Plugin\AroundProductRepositorySave; +use Magento\ConfigurableProduct\Model\Plugin\ProductRepositorySave; use Magento\ConfigurableProduct\Model\Product\Type\Configurable; use Magento\ConfigurableProduct\Test\Unit\Model\Product\ProductExtensionAttributes; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use PHPUnit_Framework_MockObject_MockObject as MockObject; /** - * Class AroundProductRepositorySaveTest + * Class ProductRepositorySaveTest * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ -class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase +class ProductRepositorySaveTest extends \PHPUnit_Framework_TestCase { /** * @var ProductAttributeRepositoryInterface|MockObject @@ -32,11 +33,6 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase */ private $productFactory; - /** - * @var \Closure - */ - private $closure; - /** * @var Product|MockObject */ @@ -68,7 +64,7 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase private $option; /** - * @var AroundProductRepositorySave + * @var ProductRepositorySave */ private $plugin; @@ -91,10 +87,6 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase ->setMethods(['getExtensionAttributes']) ->getMock(); - $this->closure = function () { - return $this->result; - }; - $this->productRepository = $this->getMockForAbstractClass(ProductRepositoryInterface::class); $this->extensionAttributes = $this->getMockBuilder(ProductExtensionAttributes::class) @@ -106,13 +98,16 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase $this->option = $this->getMockForAbstractClass(OptionInterface::class); - $this->plugin = new AroundProductRepositorySave( - $this->productAttributeRepository, - $this->productFactory + $this->plugin = (new ObjectManager($this))->getObject( + ProductRepositorySave::class, + [ + 'productAttributeRepository' => $this->productAttributeRepository, + 'productFactory' => $this->productFactory + ] ); } - public function testAroundSaveWhenProductIsSimple() + public function testAfterSaveWhenProductIsSimple() { $this->product->expects(static::once()) ->method('getTypeId') @@ -122,11 +117,11 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase $this->assertEquals( $this->result, - $this->plugin->aroundSave($this->productRepository, $this->closure, $this->product) + $this->plugin->afterSave($this->productRepository, $this->result, $this->product) ); } - public function testAroundSaveWithoutOptions() + public function testAfterSaveWithoutOptions() { $this->product->expects(static::once()) ->method('getTypeId') @@ -148,7 +143,7 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase $this->assertEquals( $this->result, - $this->plugin->aroundSave($this->productRepository, $this->closure, $this->product) + $this->plugin->afterSave($this->productRepository, $this->result, $this->product) ); } @@ -156,7 +151,7 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase * @expectedException \Magento\Framework\Exception\InputException * @expectedExceptionMessage Products "5" and "4" have the same set of attribute values. */ - public function testAroundSaveWithLinks() + public function testAfterSaveWithLinks() { $links = [4, 5]; $this->product->expects(static::once()) @@ -191,14 +186,14 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase $product->expects(static::never()) ->method('getData'); - $this->plugin->aroundSave($this->productRepository, $this->closure, $this->product); + $this->plugin->afterSave($this->productRepository, $this->result, $this->product); } /** * @expectedException \Magento\Framework\Exception\InputException * @expectedExceptionMessage Product with id "4" does not contain required attribute "color". */ - public function testAroundSaveWithLinksWithMissingAttribute() + public function testAfterSaveWithLinksWithMissingAttribute() { $simpleProductId = 4; $links = [$simpleProductId, 5]; @@ -248,14 +243,14 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase ->with($attributeCode) ->willReturn(false); - $this->plugin->aroundSave($this->productRepository, $this->closure, $this->product); + $this->plugin->afterSave($this->productRepository, $this->result, $this->product); } /** * @expectedException \Magento\Framework\Exception\InputException * @expectedExceptionMessage Products "5" and "4" have the same set of attribute values. */ - public function testAroundSaveWithLinksWithDuplicateAttributes() + public function testAfterSaveWithLinksWithDuplicateAttributes() { $links = [4, 5]; $attributeCode = 'color'; @@ -303,6 +298,6 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase ->with($attributeCode) ->willReturn($attributeId); - $this->plugin->aroundSave($this->productRepository, $this->closure, $this->product); + $this->plugin->afterSave($this->productRepository, $this->result, $this->product); } } diff --git a/app/code/Magento/ConfigurableProduct/Test/Unit/Model/Product/Validator/PluginTest.php b/app/code/Magento/ConfigurableProduct/Test/Unit/Model/Product/Validator/PluginTest.php index d66e32aa0e7b04ad4c39d0e767182656dd9ff75b..65d3f42767de5e136ac33f9c95fe702619c16613 100644 --- a/app/code/Magento/ConfigurableProduct/Test/Unit/Model/Product/Validator/PluginTest.php +++ b/app/code/Magento/ConfigurableProduct/Test/Unit/Model/Product/Validator/PluginTest.php @@ -28,17 +28,17 @@ class PluginTest extends \PHPUnit_Framework_TestCase protected $jsonHelperMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */ protected $productMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject */ protected $requestMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject */ protected $responseMock; @@ -53,15 +53,10 @@ class PluginTest extends \PHPUnit_Framework_TestCase protected $proceedResult = [1, 2, 3]; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Catalog\Model\Product\Validator|\PHPUnit_Framework_MockObject_MockObject */ protected $subjectMock; - /** - * @var \Closure - */ - protected $closureMock; - protected function setUp() { $this->eventManagerMock = $this->getMock(\Magento\Framework\Event\Manager::class, [], [], '', false); @@ -82,14 +77,14 @@ class PluginTest extends \PHPUnit_Framework_TestCase $this->jsonHelperMock->expects($this->any())->method('jsonDecode')->will($this->returnArgument(0)); $this->productMock = $this->getMock( \Magento\Catalog\Model\Product::class, - ['getData', 'getAttributes'], + ['getData', 'getAttributes', 'setTypeId'], [], '', false ); $this->requestMock = $this->getMock( \Magento\Framework\App\Request\Http::class, - ['getPost', 'getParam', '__wakeup'], + ['getPost', 'getParam', '__wakeup', 'has'], [], '', false @@ -102,10 +97,7 @@ class PluginTest extends \PHPUnit_Framework_TestCase false ); $this->arguments = [$this->productMock, $this->requestMock, $this->responseMock]; - $proceedResult = $this->proceedResult; - $this->closureMock = function () use ($proceedResult) { - return $proceedResult; - }; + $this->subjectMock = $this->getMock(\Magento\Catalog\Model\Product\Validator::class, [], [], '', false); $this->plugin = new \Magento\ConfigurableProduct\Model\Product\Validator\Plugin( $this->eventManagerMock, @@ -114,7 +106,20 @@ class PluginTest extends \PHPUnit_Framework_TestCase ); } - public function testAroundValidateWithVariationsValid() + public function testBeforeValidate() + { + $this->requestMock->expects(static::once())->method('has')->with('attributes')->willReturn(true); + $this->productMock->expects(static::once())->method('setTypeId')->willReturnSelf(); + + $this->plugin->beforeValidate( + $this->subjectMock, + $this->productMock, + $this->requestMock, + $this->responseMock + ); + } + + public function testAfterValidateWithVariationsValid() { $matrix = ['products']; @@ -150,9 +155,9 @@ class PluginTest extends \PHPUnit_Framework_TestCase $this->assertEquals( $this->proceedResult, - $plugin->aroundValidate( + $plugin->afterValidate( $this->subjectMock, - $this->closureMock, + $this->proceedResult, $this->productMock, $this->requestMock, $this->responseMock @@ -160,7 +165,7 @@ class PluginTest extends \PHPUnit_Framework_TestCase ); } - public function testAroundValidateWithVariationsInvalid() + public function testAfterValidateWithVariationsInvalid() { $matrix = ['products']; @@ -197,9 +202,9 @@ class PluginTest extends \PHPUnit_Framework_TestCase $this->responseMock->expects($this->once())->method('setAttributes')->will($this->returnSelf()); $this->assertEquals( $this->proceedResult, - $plugin->aroundValidate( + $plugin->afterValidate( $this->subjectMock, - $this->closureMock, + $this->proceedResult, $this->productMock, $this->requestMock, $this->responseMock @@ -207,7 +212,7 @@ class PluginTest extends \PHPUnit_Framework_TestCase ); } - public function testAroundValidateIfVariationsNotExist() + public function testAfterValidateIfVariationsNotExist() { $this->requestMock->expects( $this->once() @@ -219,16 +224,16 @@ class PluginTest extends \PHPUnit_Framework_TestCase $this->returnValue(null) ); $this->eventManagerMock->expects($this->never())->method('dispatch'); - $this->plugin->aroundValidate( + $this->plugin->afterValidate( $this->subjectMock, - $this->closureMock, + $this->proceedResult, $this->productMock, $this->requestMock, $this->responseMock ); } - public function testAroundValidateWithVariationsAndRequiredAttributes() + public function testAfterValidateWithVariationsAndRequiredAttributes() { $matrix = [ ['data1', 'data2', 'configurable_attribute' => ['data1']], @@ -313,9 +318,9 @@ class PluginTest extends \PHPUnit_Framework_TestCase $this->responseMock->expects($this->never())->method('setError'); - $result = $this->plugin->aroundValidate( + $result = $this->plugin->afterValidate( $this->subjectMock, - $this->closureMock, + $this->proceedResult, $this->productMock, $this->requestMock, $this->responseMock diff --git a/app/code/Magento/ConfigurableProduct/Test/Unit/Model/Quote/Item/QuantityValidator/Initializer/Option/Plugin/ConfigurableProductTest.php b/app/code/Magento/ConfigurableProduct/Test/Unit/Model/Quote/Item/QuantityValidator/Initializer/Option/Plugin/ConfigurableProductTest.php index 7d10cb4511e13e60ff0c4d75cbf844d02c5d42ab..6b162bbde85c4ca46e2bdf27c4328b7b2deef144 100644 --- a/app/code/Magento/ConfigurableProduct/Test/Unit/Model/Quote/Item/QuantityValidator/Initializer/Option/Plugin/ConfigurableProductTest.php +++ b/app/code/Magento/ConfigurableProduct/Test/Unit/Model/Quote/Item/QuantityValidator/Initializer/Option/Plugin/ConfigurableProductTest.php @@ -3,20 +3,20 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - -// @codingStandardsIgnoreFile - namespace Magento\ConfigurableProduct\Test\Unit\Model\Quote\Item\QuantityValidator\Initializer\Option\Plugin; +use \Magento\ConfigurableProduct\Model\Quote\Item\QuantityValidator\Initializer\Option\Plugin\ConfigurableProduct + as InitializerOptionPlugin; + class ConfigurableProductTest extends \PHPUnit_Framework_TestCase { /** * @param array $data - * @dataProvider aroundGetStockItemDataProvider + * @dataProvider afterGetStockItemDataProvider */ - public function testAroundGetStockItem(array $data) + public function testAfterGetStockItem(array $data) { - $subjectMock = $this->getMock( + $subjectMock = $this->getMock( \Magento\CatalogInventory\Model\Quote\Item\QuantityValidator\Initializer\Option::class, [], [], @@ -24,36 +24,44 @@ class ConfigurableProductTest extends \PHPUnit_Framework_TestCase false ); - $quoteItemMock = $this->getMock( - \Magento\Quote\Model\Quote\Item::class, ['getProductType', '__wakeup'], [], '', false + $quoteItemMock = $this->getMock( + \Magento\Quote\Model\Quote\Item::class, + ['getProductType', '__wakeup'], + [], + '', + false ); $quoteItemMock->expects($this->once()) ->method('getProductType') ->will($this->returnValue($data['product_type'])); - $stockItemMock = $this->getMock( - \Magento\CatalogInventory\Model\Stock\Item::class, ['setProductName', '__wakeup'], [], '', false + $stockItemMock = $this->getMock( + \Magento\CatalogInventory\Model\Stock\Item::class, + ['setProductName', '__wakeup'], + [], + '', + false ); $matcherMethod = $data['matcher_method']; $stockItemMock->expects($this->$matcherMethod()) ->method('setProductName'); - $optionMock = $this->getMock( - \Magento\Quote\Model\Quote\Item\Option::class, ['getProduct', '__wakeup'], [], '', false + $optionMock = $this->getMock( + \Magento\Quote\Model\Quote\Item\Option::class, + ['getProduct', '__wakeup'], + [], + '', + false ); - $proceed = function () use ($stockItemMock) { - return $stockItemMock; - }; - - $model = new \Magento\ConfigurableProduct\Model\Quote\Item\QuantityValidator\Initializer\Option\Plugin\ConfigurableProduct(); - $model->aroundGetStockItem($subjectMock, $proceed, $optionMock, $quoteItemMock, 0); + $model = new InitializerOptionPlugin(); + $model->afterGetStockItem($subjectMock, $stockItemMock, $optionMock, $quoteItemMock, 0); } /** * @return array */ - public function aroundGetStockItemDataProvider() + public function afterGetStockItemDataProvider() { return [ [ diff --git a/app/code/Magento/ConfigurableProduct/etc/di.xml b/app/code/Magento/ConfigurableProduct/etc/di.xml index e32286764f56ec959044cb11a5e65c13928f4d7c..c3be4aaea189a63edb9091df2712d063eb8e09e1 100644 --- a/app/code/Magento/ConfigurableProduct/etc/di.xml +++ b/app/code/Magento/ConfigurableProduct/etc/di.xml @@ -59,7 +59,7 @@ </arguments> </type> <type name="Magento\Catalog\Api\ProductRepositoryInterface"> - <plugin name="configurableProductSaveOptions" type="\Magento\ConfigurableProduct\Model\Plugin\AroundProductRepositorySave"/> + <plugin name="configurableProductSaveOptions" sortOrder="10" type="Magento\ConfigurableProduct\Model\Plugin\ProductRepositorySave"/> </type> <type name="Magento\Catalog\Model\Product"> <plugin name="configurable_identity" type="Magento\ConfigurableProduct\Plugin\Model\Product" /> diff --git a/app/code/Magento/Customer/Controller/Plugin/Account.php b/app/code/Magento/Customer/Controller/Plugin/Account.php index 0a19f87b5050c4b188be70f2f0f23850bc1bf61e..5da79b8aa46a21cb5847f1eb916994938e4698d4 100644 --- a/app/code/Magento/Customer/Controller/Plugin/Account.php +++ b/app/code/Magento/Customer/Controller/Plugin/Account.php @@ -8,6 +8,9 @@ namespace Magento\Customer\Controller\Plugin; use Magento\Customer\Model\Session; use Magento\Framework\App\ActionInterface; use Magento\Framework\App\RequestInterface; +use Magento\Framework\App\ResponseInterface; +use Magento\Framework\App\Action\AbstractAction; +use Magento\Framework\Controller\ResultInterface; class Account { @@ -36,16 +39,12 @@ class Account /** * Dispatch actions allowed for not authorized users * - * @param ActionInterface $subject - * @param \Closure $proceed + * @param AbstractAction $subject * @param RequestInterface $request - * @return mixed + * @return void */ - public function aroundDispatch( - ActionInterface $subject, - \Closure $proceed, - RequestInterface $request - ) { + public function beforeDispatch(AbstractAction $subject, RequestInterface $request) + { $action = strtolower($request->getActionName()); $pattern = '/^(' . implode('|', $this->allowedActions) . ')$/i'; @@ -56,8 +55,19 @@ class Account } else { $this->session->setNoReferer(true); } + } - $result = $proceed($request); + /** + * Remove No-referer flag from customer session + * + * @param AbstractAction $subject + * @param ResponseInterface|ResultInterface $result + * @param RequestInterface $request + * @return ResponseInterface|ResultInterface + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function afterDispatch(AbstractAction $subject, $result, RequestInterface $request) + { $this->session->unsNoReferer(false); return $result; } diff --git a/app/code/Magento/Customer/Model/App/Action/ContextPlugin.php b/app/code/Magento/Customer/Model/App/Action/ContextPlugin.php index f102664df537b846aa5376f198a04f8b06a2d5fe..7518c4b4783a04a488256d15fd6d107b13da2cc9 100644 --- a/app/code/Magento/Customer/Model/App/Action/ContextPlugin.php +++ b/app/code/Magento/Customer/Model/App/Action/ContextPlugin.php @@ -8,6 +8,10 @@ namespace Magento\Customer\Model\App\Action; use Magento\Customer\Model\Context; use Magento\Customer\Model\GroupManagement; +use Magento\Framework\App\Action\AbstractAction; +use Magento\Framework\App\RequestInterface; +use Magento\Customer\Model\Session; +use Magento\Framework\App\Http\Context as HttpContext; /** * Class ContextPlugin @@ -15,39 +19,35 @@ use Magento\Customer\Model\GroupManagement; class ContextPlugin { /** - * @var \Magento\Customer\Model\Session + * @var Session */ protected $customerSession; /** - * @var \Magento\Framework\App\Http\Context + * @var HttpContext */ protected $httpContext; /** - * @param \Magento\Customer\Model\Session $customerSession - * @param \Magento\Framework\App\Http\Context $httpContext + * @param Session $customerSession + * @param HttpContext $httpContext */ - public function __construct( - \Magento\Customer\Model\Session $customerSession, - \Magento\Framework\App\Http\Context $httpContext - ) { + public function __construct(Session $customerSession, HttpContext $httpContext) + { $this->customerSession = $customerSession; $this->httpContext = $httpContext; } /** - * @param \Magento\Framework\App\ActionInterface $subject - * @param callable $proceed - * @param \Magento\Framework\App\RequestInterface $request - * @return mixed + * Set customer group and customer session id to HTTP context + * + * @param AbstractAction $subject + * @param RequestInterface $request + * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundDispatch( - \Magento\Framework\App\ActionInterface $subject, - \Closure $proceed, - \Magento\Framework\App\RequestInterface $request - ) { + public function beforeDispatch(AbstractAction $subject, RequestInterface $request) + { $this->httpContext->setValue( Context::CONTEXT_GROUP, $this->customerSession->getCustomerGroupId(), @@ -58,6 +58,5 @@ class ContextPlugin $this->customerSession->isLoggedIn(), false ); - return $proceed($request); } } diff --git a/app/code/Magento/Customer/Test/Unit/Controller/Plugin/AccountTest.php b/app/code/Magento/Customer/Test/Unit/Controller/Plugin/AccountTest.php index b175e55f1d3be90b4b6b886c3db22eb361e8e904..b9c05d4005be5e6664f9a36b76db7814140a55b8 100644 --- a/app/code/Magento/Customer/Test/Unit/Controller/Plugin/AccountTest.php +++ b/app/code/Magento/Customer/Test/Unit/Controller/Plugin/AccountTest.php @@ -9,7 +9,10 @@ use Magento\Customer\Controller\Plugin\Account; use Magento\Customer\Model\Session; use Magento\Framework\App\ActionFlag; use Magento\Framework\App\ActionInterface; +use Magento\Framework\App\Action\AbstractAction; use Magento\Framework\App\Request\Http; +use Magento\Framework\Controller\ResultInterface; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; class AccountTest extends \PHPUnit_Framework_TestCase { @@ -29,12 +32,7 @@ class AccountTest extends \PHPUnit_Framework_TestCase protected $session; /** - * @var \Closure - */ - protected $proceed; - - /** - * @var ActionInterface | \PHPUnit_Framework_MockObject_MockObject + * @var AbstractAction | \PHPUnit_Framework_MockObject_MockObject */ protected $subject; @@ -48,6 +46,11 @@ class AccountTest extends \PHPUnit_Framework_TestCase */ protected $actionFlag; + /** + * @var ResultInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $resultInterface; + protected function setUp() { $this->session = $this->getMockBuilder(\Magento\Customer\Model\Session::class) @@ -59,16 +62,13 @@ class AccountTest extends \PHPUnit_Framework_TestCase ]) ->getMock(); - $this->subject = $this->getMockBuilder(\Magento\Framework\App\ActionInterface::class) + $this->subject = $this->getMockBuilder(AbstractAction::class) ->setMethods([ 'getActionFlag', ]) + ->disableOriginalConstructor() ->getMockForAbstractClass(); - $this->proceed = function () { - return self::EXPECTED_VALUE; - }; - $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class) ->disableOriginalConstructor() ->setMethods([ @@ -76,6 +76,9 @@ class AccountTest extends \PHPUnit_Framework_TestCase ]) ->getMock(); + $this->resultInterface = $this->getMockBuilder(ResultInterface::class) + ->getMockForAbstractClass(); + $this->actionFlag = $this->getMockBuilder(\Magento\Framework\App\ActionFlag::class) ->disableOriginalConstructor() ->getMock(); @@ -87,9 +90,9 @@ class AccountTest extends \PHPUnit_Framework_TestCase * @param boolean $isActionAllowed * @param boolean $isAuthenticated * - * @dataProvider dataProviderAroundDispatch + * @dataProvider beforeDispatchDataProvider */ - public function testAroundDispatch( + public function testBeforeDispatch( $action, $allowedActions, $isActionAllowed, @@ -99,11 +102,6 @@ class AccountTest extends \PHPUnit_Framework_TestCase ->method('getActionName') ->willReturn($action); - $this->session->expects($this->once()) - ->method('unsNoReferer') - ->with(false) - ->willReturnSelf(); - if ($isActionAllowed) { $this->session->expects($this->once()) ->method('setNoReferer') @@ -126,13 +124,13 @@ class AccountTest extends \PHPUnit_Framework_TestCase } $plugin = new Account($this->session, $allowedActions); - $this->assertEquals( - self::EXPECTED_VALUE, - $plugin->aroundDispatch($this->subject, $this->proceed, $this->request) - ); + $plugin->beforeDispatch($this->subject, $this->request); } - public function dataProviderAroundDispatch() + /** + * @return array + */ + public function beforeDispatchDataProvider() { return [ [ @@ -167,4 +165,24 @@ class AccountTest extends \PHPUnit_Framework_TestCase ], ]; } + + public function testAfterDispatch() + { + $this->session->expects($this->once()) + ->method('unsNoReferer') + ->with(false) + ->willReturnSelf(); + + $plugin = (new ObjectManager($this))->getObject( + Account::class, + [ + 'session' => $this->session, + 'allowedActions' => ['testaction'] + ] + ); + $this->assertSame( + $this->resultInterface, + $plugin->afterDispatch($this->subject, $this->resultInterface, $this->request) + ); + } } diff --git a/app/code/Magento/Customer/Test/Unit/Model/App/Action/ContextPluginTest.php b/app/code/Magento/Customer/Test/Unit/Model/App/Action/ContextPluginTest.php index c5ab448f976474dac910b7d090fe84a448f385a4..985e2ecb7efd3a25c333dc9ecd24fe7e9b1dc11d 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/App/Action/ContextPluginTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/App/Action/ContextPluginTest.php @@ -28,11 +28,6 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase */ protected $httpContextMock; - /** - * @var \Closure - */ - protected $closureMock; - /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -62,9 +57,6 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase '', false ); - $this->closureMock = function () { - return 'ExpectedValue'; - }; $this->subjectMock = $this->getMock(\Magento\Framework\App\Action\Action::class, [], [], '', false); $this->requestMock = $this->getMock(\Magento\Framework\App\RequestInterface::class); $this->plugin = new \Magento\Customer\Model\App\Action\ContextPlugin( @@ -76,7 +68,7 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase /** * Test aroundDispatch */ - public function testAroundDispatch() + public function testBeforeDispatch() { $this->customerSessionMock->expects($this->once()) ->method('getCustomerGroupId') @@ -94,9 +86,6 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase ] ) ); - $this->assertEquals( - 'ExpectedValue', - $this->plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock) - ); + $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock); } } diff --git a/app/code/Magento/GiftMessage/Block/Message/Multishipping/Plugin/ItemsBox.php b/app/code/Magento/GiftMessage/Block/Message/Multishipping/Plugin/ItemsBox.php index e79052a1fb3e3826977f625e167384bd8127fdb7..acc5fb484ada7a2ccd76b1d426b9d1eed5a398ae 100644 --- a/app/code/Magento/GiftMessage/Block/Message/Multishipping/Plugin/ItemsBox.php +++ b/app/code/Magento/GiftMessage/Block/Message/Multishipping/Plugin/ItemsBox.php @@ -5,6 +5,10 @@ */ namespace Magento\GiftMessage\Block\Message\Multishipping\Plugin; +use Magento\Multishipping\Block\Checkout\Shipping as ShippingBlock; +use Magento\GiftMessage\Helper\Message as MessageHelper; +use Magento\Framework\DataObject; + /** * Multishipping items box plugin */ @@ -13,16 +17,16 @@ class ItemsBox /** * Gift message helper * - * @var \Magento\GiftMessage\Helper\Message + * @var MessageHelper */ protected $helper; /** * Construct * - * @param \Magento\GiftMessage\Helper\Message $helper + * @param MessageHelper $helper */ - public function __construct(\Magento\GiftMessage\Helper\Message $helper) + public function __construct(MessageHelper $helper) { $this->helper = $helper; } @@ -30,19 +34,15 @@ class ItemsBox /** * Get items box message text for multishipping * - * @param \Magento\Multishipping\Block\Checkout\Shipping $subject - * @param callable $proceed - * @param \Magento\Framework\DataObject $addressEntity + * @param ShippingBlock $subject + * @param string $itemsBoxText + * @param DataObject $addressEntity * * @return string * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundGetItemsBoxTextAfter( - \Magento\Multishipping\Block\Checkout\Shipping $subject, - \Closure $proceed, - \Magento\Framework\DataObject $addressEntity - ) { - $itemsBoxText = $proceed($addressEntity); + public function afterGetItemsBoxTextAfter(ShippingBlock $subject, $itemsBoxText, DataObject $addressEntity) + { return $itemsBoxText . $this->helper->getInline('multishipping_address', $addressEntity); } } diff --git a/app/code/Magento/GiftMessage/Model/Plugin/QuoteItem.php b/app/code/Magento/GiftMessage/Model/Plugin/QuoteItem.php index 0faee488baea061b83e1180a715dd213a5ceec07..6367fd7ced4052e06cd7b787c98de861ed8b166a 100644 --- a/app/code/Magento/GiftMessage/Model/Plugin/QuoteItem.php +++ b/app/code/Magento/GiftMessage/Model/Plugin/QuoteItem.php @@ -5,40 +5,42 @@ */ namespace Magento\GiftMessage\Model\Plugin; -use Closure; -use Magento\Sales\Model\Order\Item; +use Magento\Sales\Api\Data\OrderItemInterface; +use Magento\GiftMessage\Helper\Message as MessageHelper; +use Magento\Quote\Model\Quote\Item\ToOrderItem; +use Magento\Quote\Model\Quote\Item\AbstractItem; class QuoteItem { /** - * @var \Magento\GiftMessage\Helper\Message + * @var MessageHelper */ protected $_helper; /** - * @param \Magento\GiftMessage\Helper\Message $helper + * @param MessageHelper $helper */ - public function __construct(\Magento\GiftMessage\Helper\Message $helper) + public function __construct(MessageHelper $helper) { $this->_helper = $helper; } /** - * @param \Magento\Quote\Model\Quote\Item\ToOrderItem $subject - * @param callable $proceed - * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item + * Apply gift message per every item in order if available + * + * @param ToOrderItem $subject + * @param OrderItemInterface $orderItem + * @param AbstractItem $item * @param array $additional - * @return Item + * @return OrderItemInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundConvert( - \Magento\Quote\Model\Quote\Item\ToOrderItem $subject, - Closure $proceed, - \Magento\Quote\Model\Quote\Item\AbstractItem $item, + public function afterConvert( + ToOrderItem $subject, + OrderItemInterface $orderItem, + AbstractItem $item, $additional = [] ) { - /** @var $orderItem Item */ - $orderItem = $proceed($item, $additional); $isAvailable = $this->_helper->isMessagesAllowed('item', $item, $item->getStoreId()); $orderItem->setGiftMessageId($item->getGiftMessageId()); diff --git a/app/code/Magento/GiftMessage/Test/Unit/Model/Plugin/QuoteItemTest.php b/app/code/Magento/GiftMessage/Test/Unit/Model/Plugin/QuoteItemTest.php index ad099bebc8847d7556414d6118687c1da108c8d5..cfb8f1463d6eb0d8aba41e60a271ebc4ccfa33f7 100644 --- a/app/code/Magento/GiftMessage/Test/Unit/Model/Plugin/QuoteItemTest.php +++ b/app/code/Magento/GiftMessage/Test/Unit/Model/Plugin/QuoteItemTest.php @@ -68,7 +68,7 @@ class QuoteItemTest extends \PHPUnit_Framework_TestCase $this->model = new \Magento\GiftMessage\Model\Plugin\QuoteItem($this->helperMock); } - public function testAroundItemToOrderItem() + public function testAfterItemToOrderItem() { $storeId = 1; $giftMessageId = 1; @@ -99,7 +99,7 @@ class QuoteItemTest extends \PHPUnit_Framework_TestCase $this->assertSame( $this->orderItemMock, - $this->model->aroundConvert($this->subjectMock, $this->closureMock, $this->quoteItemMock, []) + $this->model->afterConvert($this->subjectMock, $this->orderItemMock, $this->quoteItemMock, []) ); } } diff --git a/app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Link/RelationPersister.php b/app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Link/RelationPersister.php index f83b34ec3b30d694207511fc096a87a1b90ff90c..f431bf3af952b2109b60e44436944f9040e16049 100644 --- a/app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Link/RelationPersister.php +++ b/app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Link/RelationPersister.php @@ -9,6 +9,7 @@ namespace Magento\GroupedProduct\Model\ResourceModel\Product\Link; use Magento\Catalog\Model\ProductLink\LinkFactory; use Magento\Catalog\Model\ResourceModel\Product\Link; use Magento\Catalog\Model\ResourceModel\Product\Relation; +use Magento\GroupedProduct\Model\ResourceModel\Product\Link as GroupedLink; class RelationPersister { @@ -38,17 +39,16 @@ class RelationPersister * Save grouped products to product relation table * * @param Link $subject - * @param \Closure $proceed + * @param Link $result * @param int $parentId * @param array $data * @param int $typeId * @return Link * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundSaveProductLinks(Link $subject, \Closure $proceed, $parentId, $data, $typeId) + public function afterSaveProductLinks(Link $subject, Link $result, $parentId, $data, $typeId) { - $result = $proceed($parentId, $data, $typeId); - if ($typeId == \Magento\GroupedProduct\Model\ResourceModel\Product\Link::LINK_TYPE_GROUPED) { + if ($typeId == GroupedLink::LINK_TYPE_GROUPED) { foreach ($data as $linkData) { $this->relationProcessor->addRelation( $parentId, @@ -73,7 +73,7 @@ class RelationPersister $link = $this->linkFactory->create(); $subject->load($link, $linkId, $subject->getIdFieldName()); $result = $proceed($linkId); - if ($link->getLinkTypeId() == \Magento\GroupedProduct\Model\ResourceModel\Product\Link::LINK_TYPE_GROUPED) { + if ($link->getLinkTypeId() == GroupedLink::LINK_TYPE_GROUPED) { $this->relationProcessor->removeRelations( $link->getProductId(), $link->getLinkedProductId() diff --git a/app/code/Magento/GroupedProduct/Model/Sales/AdminOrder/Product/Quote/Plugin/Initializer.php b/app/code/Magento/GroupedProduct/Model/Sales/AdminOrder/Product/Quote/Plugin/Initializer.php index f1204efa714a502cfec8563e408df353e4f89b53..2ab99856df306f9af5b2d951bb75785e64107c95 100644 --- a/app/code/Magento/GroupedProduct/Model/Sales/AdminOrder/Product/Quote/Plugin/Initializer.php +++ b/app/code/Magento/GroupedProduct/Model/Sales/AdminOrder/Product/Quote/Plugin/Initializer.php @@ -18,7 +18,7 @@ class Initializer { /** * @param \Magento\Sales\Model\AdminOrder\Product\Quote\Initializer $subject - * @param callable $proceed + * @param \Magento\Quote\Model\Quote\Item|string $item * @param \Magento\Quote\Model\Quote $quote * @param \Magento\Catalog\Model\Product $product * @param \Magento\Framework\DataObject $config @@ -26,15 +26,13 @@ class Initializer * @return \Magento\Quote\Model\Quote\Item|string * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundInit( + public function afterInit( \Magento\Sales\Model\AdminOrder\Product\Quote\Initializer $subject, - \Closure $proceed, + $item, \Magento\Quote\Model\Quote $quote, \Magento\Catalog\Model\Product $product, \Magento\Framework\DataObject $config ) { - $item = $proceed($quote, $product, $config); - if (is_string($item) && $product->getTypeId() != Grouped::TYPE_CODE) { $item = $quote->addProduct( $product, diff --git a/app/code/Magento/GroupedProduct/Test/Unit/Model/ResourceModel/Product/Link/RelationPersisterTest.php b/app/code/Magento/GroupedProduct/Test/Unit/Model/ResourceModel/Product/Link/RelationPersisterTest.php index d11539945b0e84f7437b43975de4abc6db00dcc3..49bcd76cc2b83d0f838438e333924eaca62b912e 100644 --- a/app/code/Magento/GroupedProduct/Test/Unit/Model/ResourceModel/Product/Link/RelationPersisterTest.php +++ b/app/code/Magento/GroupedProduct/Test/Unit/Model/ResourceModel/Product/Link/RelationPersisterTest.php @@ -3,13 +3,14 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\GroupedProduct\Test\Unit\Model\ResourceModel\Product\Link; use Magento\GroupedProduct\Model\ResourceModel\Product\Link\RelationPersister; use Magento\Catalog\Model\ProductLink\LinkFactory; use Magento\Catalog\Model\Product\Link; use Magento\Catalog\Model\ResourceModel\Product\Relation; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\Catalog\Model\ResourceModel\Product\Link as LinkResourceModel; class RelationPersisterTest extends \PHPUnit_Framework_TestCase { @@ -22,12 +23,29 @@ class RelationPersisterTest extends \PHPUnit_Framework_TestCase /** @var Relation */ private $relationProcessor; + /** + * @var ObjectManager + */ + private $objectManager; + + /** + * @var LinkFactory|\PHPUnit_Framework_MockObject_MockObject + */ + private $linkFactory; + + /** + * @var LinkResourceModel|\PHPUnit_Framework_MockObject_MockObject + */ + private $subject; + /** * @inheritDoc */ protected function setUp() { - $linkFactory = $this->getMockBuilder(LinkFactory::class) + $this->objectManager = new ObjectManager($this); + + $this->linkFactory = $this->getMockBuilder(LinkFactory::class) ->setMethods(['create']) ->disableOriginalConstructor() ->getMock(); @@ -41,23 +59,27 @@ class RelationPersisterTest extends \PHPUnit_Framework_TestCase ->disableOriginalConstructor() ->getMock(); - $linkFactory->expects($this->any())->method('create')->willReturn($this->link); + $this->linkFactory->expects($this->any())->method('create')->willReturn($this->link); - $this->object = new RelationPersister( - $this->relationProcessor, - $linkFactory + $this->subject = $this->getMockBuilder(LinkResourceModel::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->object = $this->objectManager->getObject( + RelationPersister::class, + [ + 'relationProcessor' => $this->relationProcessor, + 'linkFactory' => $this->linkFactory + ] ); } - public function testAroundSaveProductLinks() + public function testAfterSaveProductLinks() { - $subject = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Link::class) - ->disableOriginalConstructor() - ->getMock(); $this->relationProcessor->expects($this->once())->method('addRelation')->with(2, 10); - $this->assertEquals($subject, $this->object->aroundSaveProductLinks( - $subject, - function() use ($subject) { return $subject; }, + $this->assertEquals($this->subject, $this->object->afterSaveProductLinks( + $this->subject, + $this->subject, 2, [['product_id' => 10]], 3 @@ -87,10 +109,11 @@ class RelationPersisterTest extends \PHPUnit_Framework_TestCase $subject, $this->object->aroundDeleteProductLink( $subject, - function() use ($subject) { return $subject; }, + function () use ($subject) { + return $subject; + }, 155 ) ); - } } diff --git a/app/code/Magento/GroupedProduct/Test/Unit/Model/Sales/AdminOrder/Product/Quote/Plugin/InitializerTest.php b/app/code/Magento/GroupedProduct/Test/Unit/Model/Sales/AdminOrder/Product/Quote/Plugin/InitializerTest.php new file mode 100644 index 0000000000000000000000000000000000000000..43d7aa93dd0df9d6030640114241dfe64273209a --- /dev/null +++ b/app/code/Magento/GroupedProduct/Test/Unit/Model/Sales/AdminOrder/Product/Quote/Plugin/InitializerTest.php @@ -0,0 +1,87 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\GroupedProduct\Test\Unit\Model\Sales\AdminOrder\Product\Quote\Plugin; + +use Magento\GroupedProduct\Model\Sales\AdminOrder\Product\Quote\Plugin\Initializer as QuoteInitializerPlugin; +use Magento\Sales\Model\AdminOrder\Product\Quote\Initializer as QuoteInitializer; +use Magento\Quote\Model\Quote; +use Magento\Catalog\Model\Product; +use Magento\Quote\Model\Quote\Item as QuoteItem; +use Magento\Framework\DataObject; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; + +class InitializerTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var ObjectManagerHelper + */ + private $objectManagerHelper; + + /** + * @var QuoteInitializerPlugin|\PHPUnit_Framework_MockObject_MockObject + */ + private $plugin; + + /** + * @var QuoteInitializer|\PHPUnit_Framework_MockObject_MockObject + */ + private $initializer; + + /** + * @var Quote|\PHPUnit_Framework_MockObject_MockObject + */ + private $quote; + + /** + * @var QuoteItem|\PHPUnit_Framework_MockObject_MockObject + */ + private $quoteItem; + + /** + * @var Product|\PHPUnit_Framework_MockObject_MockObject + */ + private $product; + + /** + * @var DataObject|\PHPUnit_Framework_MockObject_MockObject + */ + private $config; + + protected function setUp() + { + $this->objectManagerHelper = new ObjectManagerHelper($this); + + $this->initializer = $this->getMockBuilder(QuoteInitializer::class) + ->disableOriginalConstructor() + ->getMock(); + $this->quote = $this->getMockBuilder(Quote::class) + ->setMethods(['addProduct']) + ->disableOriginalConstructor() + ->getMock(); + $this->product = $this->getMockBuilder(Product::class) + ->disableOriginalConstructor() + ->setMethods(['getTypeId']) + ->getMock(); + $this->quoteItem = $this->getMockBuilder(QuoteItem::class) + ->disableOriginalConstructor() + ->getMock(); + $this->config = $this->getMockBuilder(DataObject::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->plugin = $this->objectManagerHelper->getObject( + QuoteInitializerPlugin::class + ); + } + + public function testAfterInit() + { + $this->assertSame( + $this->quoteItem, + $this->plugin->afterInit($this->initializer, $this->quoteItem, $this->quote, $this->product, $this->config) + ); + } +} diff --git a/app/code/Magento/MediaStorage/Model/Asset/Plugin/CleanMergedJsCss.php b/app/code/Magento/MediaStorage/Model/Asset/Plugin/CleanMergedJsCss.php index 0100c9887f1537081af83a477da9c78c107615a9..4cd6d67a01073b76b396c29c3ad933022c4ee94f 100644 --- a/app/code/Magento/MediaStorage/Model/Asset/Plugin/CleanMergedJsCss.php +++ b/app/code/Magento/MediaStorage/Model/Asset/Plugin/CleanMergedJsCss.php @@ -35,15 +35,13 @@ class CleanMergedJsCss * Clean files in database on cleaning merged assets * * @param \Magento\Framework\View\Asset\MergeService $subject - * @param callable $proceed + * @param void $result * * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundCleanMergedJsCss(\Magento\Framework\View\Asset\MergeService $subject, \Closure $proceed) + public function afterCleanMergedJsCss(\Magento\Framework\View\Asset\MergeService $subject, $result) { - $proceed(); - /** @var \Magento\Framework\Filesystem\Directory\ReadInterface $pubStaticDirectory */ $pubStaticDirectory = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW); $mergedDir = $pubStaticDirectory->getAbsolutePath() . '/' diff --git a/app/code/Magento/MediaStorage/Test/Unit/Model/Asset/Plugin/CleanMergedJsCssTest.php b/app/code/Magento/MediaStorage/Test/Unit/Model/Asset/Plugin/CleanMergedJsCssTest.php index aa6578ebc7effec18df4e88a0f99784aed580ad3..9ff391185952255ccc216f376bf7c65e595bd21a 100644 --- a/app/code/Magento/MediaStorage/Test/Unit/Model/Asset/Plugin/CleanMergedJsCssTest.php +++ b/app/code/Magento/MediaStorage/Test/Unit/Model/Asset/Plugin/CleanMergedJsCssTest.php @@ -3,9 +3,6 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - -// @codingStandardsIgnoreFile - namespace Magento\MediaStorage\Test\Unit\Model\Asset\Plugin; use Magento\Framework\App\Filesystem\DirectoryList; @@ -22,11 +19,6 @@ class CleanMergedJsCssTest extends \Magento\Framework\TestFramework\Unit\BaseTes */ private $filesystemMock; - /** - * @var bool - */ - private $hasBeenCalled = false; - /** * @var \Magento\MediaStorage\Model\Asset\Plugin\CleanMergedJsCss */ @@ -46,11 +38,8 @@ class CleanMergedJsCssTest extends \Magento\Framework\TestFramework\Unit\BaseTes ); } - public function testAroundCleanMergedJsCss() + public function testAfterCleanMergedJsCss() { - $callable = function () { - $this->hasBeenCalled = true; - }; $readDir = 'read directory'; $mergedDir = $readDir . '/' . \Magento\Framework\View\Asset\Merged::getRelativeDir(); @@ -65,11 +54,9 @@ class CleanMergedJsCssTest extends \Magento\Framework\TestFramework\Unit\BaseTes ->with(DirectoryList::STATIC_VIEW) ->willReturn($readDirectoryMock); - $this->model->aroundCleanMergedJsCss( + $this->model->afterCleanMergedJsCss( $this->basicMock(\Magento\Framework\View\Asset\MergeService::class), - $callable + null ); - - $this->assertTrue($this->hasBeenCalled); } } diff --git a/app/code/Magento/Newsletter/Model/Plugin/CustomerPlugin.php b/app/code/Magento/Newsletter/Model/Plugin/CustomerPlugin.php index d785b3a82aff76cf200a5c662ad1070acc83d944..3cae720825a04efae6277123f8ba56d62325a08f 100644 --- a/app/code/Magento/Newsletter/Model/Plugin/CustomerPlugin.php +++ b/app/code/Magento/Newsletter/Model/Plugin/CustomerPlugin.php @@ -31,46 +31,27 @@ class CustomerPlugin /** * Plugin after create customer that updates any newsletter subscription that may have existed. * + * If we have extension attribute (is_subscribed) we need to subscribe that customer + * * @param CustomerRepository $subject + * @param CustomerInterface $result * @param CustomerInterface $customer * @return CustomerInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterSave(CustomerRepository $subject, CustomerInterface $customer) + public function afterSave(CustomerRepository $subject, CustomerInterface $result, CustomerInterface $customer) { - $this->subscriberFactory->create()->updateSubscription($customer->getId()); - return $customer; - } - - /** - * Plugin around customer repository save. If we have extension attribute (is_subscribed) we need to subscribe that customer - * - * @param CustomerRepository $subject - * @param \Closure $proceed - * @param CustomerInterface $customer - * @param null $passwordHash - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function aroundSave( - CustomerRepository $subject, - \Closure $proceed, - CustomerInterface $customer, - $passwordHash = null - ) { - /** @var CustomerInterface $savedCustomer */ - $savedCustomer = $proceed($customer, $passwordHash); - - if ($savedCustomer->getId() && $customer->getExtensionAttributes()) { + $this->subscriberFactory->create()->updateSubscription($result->getId()); + if ($result->getId() && $customer->getExtensionAttributes()) { if ($customer->getExtensionAttributes()->getIsSubscribed() === true) { - $this->subscriberFactory->create()->subscribeCustomerById($savedCustomer->getId()); + $this->subscriberFactory->create()->subscribeCustomerById($result->getId()); } elseif ($customer->getExtensionAttributes()->getIsSubscribed() === false) { - $this->subscriberFactory->create()->unsubscribeCustomerById($savedCustomer->getId()); + $this->subscriberFactory->create()->unsubscribeCustomerById($result->getId()); } } - - return $savedCustomer; + return $result; } - + /** * Plugin around delete customer that updates any newsletter subscription that may have existed. * @@ -96,21 +77,16 @@ class CustomerPlugin } /** - * Plugin around delete customer that updates any newsletter subscription that may have existed. + * Plugin after delete customer that updates any newsletter subscription that may have existed. * * @param CustomerRepository $subject - * @param callable $deleteCustomer Function we are wrapping around - * @param CustomerInterface $customer Input to the function + * @param bool $result + * @param CustomerInterface $customer * @return bool * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundDelete( - CustomerRepository $subject, - callable $deleteCustomer, - $customer - ) { - $result = $deleteCustomer($customer); - /** @var \Magento\Newsletter\Model\Subscriber $subscriber */ + public function afterDelete(CustomerRepository $subject, $result, CustomerInterface $customer) + { $subscriber = $this->subscriberFactory->create(); $subscriber->loadByEmail($customer->getEmail()); if ($subscriber->getId()) { diff --git a/app/code/Magento/Newsletter/Test/Unit/Model/Plugin/CustomerPluginTest.php b/app/code/Magento/Newsletter/Test/Unit/Model/Plugin/CustomerPluginTest.php index b4bc13043bf97409bfc6ceb7d8db47a5440fadca..458d6ea22b009323da9aee75dfe7a1a416833d19 100644 --- a/app/code/Magento/Newsletter/Test/Unit/Model/Plugin/CustomerPluginTest.php +++ b/app/code/Magento/Newsletter/Test/Unit/Model/Plugin/CustomerPluginTest.php @@ -60,26 +60,11 @@ class CustomerPluginTest extends \PHPUnit_Framework_TestCase ); } - public function testAfterSave() + public function testAfterSaveWithoutIsSubscribed() { - $customerId = 1; - $subject = $this->getMock(\Magento\Customer\Api\CustomerRepositoryInterface::class); - $customer = $this->getMock(\Magento\Customer\Api\Data\CustomerInterface::class); - $customer->expects($this->once())->method('getId')->willReturn($customerId); - $this->subscriber->expects($this->once())->method('updateSubscription')->with($customerId)->willReturnSelf(); - - $this->assertEquals($customer, $this->plugin->afterSave($subject, $customer)); - } - - public function testAroundSaveWithoutIsSubscribed() - { - $passwordHash = null; $customerId = 1; /** @var CustomerInterface | \PHPUnit_Framework_MockObject_MockObject $customer */ $customer = $this->getMock(\Magento\Customer\Api\Data\CustomerInterface::class); - $proceed = function (CustomerInterface $customer, $passwordHash = null) use ($customer) { - return $customer; - }; /** @var CustomerRepository | \PHPUnit_Framework_MockObject_MockObject $subject */ $subject = $this->getMock(\Magento\Customer\Api\CustomerRepositoryInterface::class); @@ -87,26 +72,27 @@ class CustomerPluginTest extends \PHPUnit_Framework_TestCase ->method("getId") ->willReturn($customerId); - $this->assertEquals($customer, $this->plugin->aroundSave($subject, $proceed, $customer, $passwordHash)); + $this->assertEquals($customer, $this->plugin->afterSave($subject, $customer, $customer)); } /** * @return array */ - public function provideExtensionAttributeDataForAroundSave() + public function afterSaveExtensionAttributeDataProvider() { return [ - [true, true] , + [true, true], [false, false] ]; } /** - * @dataProvider provideExtensionAttributeDataForAroundSave + * @param boolean $isSubscribed + * @param boolean $subscribeIsCreated + * @dataProvider afterSaveExtensionAttributeDataProvider */ - public function testAroundSaveWithIsSubscribed($isSubscribed, $subscribeIsCreated) + public function testAfterSaveWithIsSubscribed($isSubscribed, $subscribeIsCreated) { - $passwordHash = null; $customerId = 1; /** @var CustomerInterface | \PHPUnit_Framework_MockObject_MockObject $customer */ $customer = $this->getMock(\Magento\Customer\Api\Data\CustomerInterface::class); @@ -134,9 +120,6 @@ class CustomerPluginTest extends \PHPUnit_Framework_TestCase ->with($customerId); } - $proceed = function (CustomerInterface $customer, $passwordHash = null) use ($customer) { - return $customer; - }; /** @var CustomerRepository | \PHPUnit_Framework_MockObject_MockObject $subject */ $subject = $this->getMock(\Magento\Customer\Api\CustomerRepositoryInterface::class); @@ -144,14 +127,11 @@ class CustomerPluginTest extends \PHPUnit_Framework_TestCase ->method("getId") ->willReturn($customerId); - $this->assertEquals($customer, $this->plugin->aroundSave($subject, $proceed, $customer, $passwordHash)); + $this->assertEquals($customer, $this->plugin->afterSave($subject, $customer, $customer)); } - public function testAroundDelete() + public function testAfterDelete() { - $deleteCustomer = function () { - return true; - }; $subject = $this->getMock(\Magento\Customer\Api\CustomerRepositoryInterface::class); $customer = $this->getMock(\Magento\Customer\Api\Data\CustomerInterface::class); $customer->expects($this->once())->method('getEmail')->willReturn('test@test.com'); @@ -159,7 +139,7 @@ class CustomerPluginTest extends \PHPUnit_Framework_TestCase $this->subscriber->expects($this->once())->method('getId')->willReturn(1); $this->subscriber->expects($this->once())->method('delete')->willReturnSelf(); - $this->assertEquals(true, $this->plugin->aroundDelete($subject, $deleteCustomer, $customer)); + $this->assertEquals(true, $this->plugin->afterDelete($subject, true, $customer)); } public function testAroundDeleteById() diff --git a/app/code/Magento/PageCache/Model/App/FrontController/VarnishPlugin.php b/app/code/Magento/PageCache/Model/App/FrontController/VarnishPlugin.php index 1d716991f9d3f9dca62b76f12c0b1060d36ea31f..08b68447c9d4d8dd4f3612692e5488dd204d1e15 100644 --- a/app/code/Magento/PageCache/Model/App/FrontController/VarnishPlugin.php +++ b/app/code/Magento/PageCache/Model/App/FrontController/VarnishPlugin.php @@ -5,61 +5,67 @@ */ namespace Magento\PageCache\Model\App\FrontController; +use Magento\PageCache\Model\Config; +use Magento\Framework\App\PageCache\Version; +use Magento\Framework\App\State as AppState; +use Magento\Framework\App\FrontControllerInterface; +use Magento\Framework\App\ResponseInterface; +use Magento\Framework\App\Response\Http as ResponseHttp; +use Magento\Framework\Controller\ResultInterface; + /** * Varnish for processing builtin cache */ class VarnishPlugin { /** - * @var \Magento\Framework\App\Config\ScopeConfigInterface + * @var Config */ - protected $config; + private $config; /** - * @var \Magento\Framework\App\PageCache\Version + * @var Version */ - protected $version; + private $version; /** - * @var \Magento\Framework\App\State + * @var AppState */ - protected $state; + private $state; /** - * @param \Magento\PageCache\Model\Config $config - * @param \Magento\Framework\App\PageCache\Version $version - * @param \Magento\Framework\App\State $state + * @param Config $config + * @param Version $version + * @param AppState $state */ - public function __construct( - \Magento\PageCache\Model\Config $config, - \Magento\Framework\App\PageCache\Version $version, - \Magento\Framework\App\State $state - ) { + public function __construct(Config $config, Version $version, AppState $state) + { $this->config = $config; $this->version = $version; $this->state = $state; } /** - * @param \Magento\Framework\App\FrontControllerInterface $subject - * @param callable $proceed - * @param \Magento\Framework\App\RequestInterface $request - * @return false|\Magento\Framework\App\Response\Http|\Magento\Framework\Controller\ResultInterface + * Perform response postprocessing + * + * @param FrontControllerInterface $subject + * @param ResponseInterface|ResultInterface $result + * @return ResponseHttp|ResultInterface + * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundDispatch( - \Magento\Framework\App\FrontControllerInterface $subject, - \Closure $proceed, - \Magento\Framework\App\RequestInterface $request - ) { - $response = $proceed($request); - if ($this->config->getType() == \Magento\PageCache\Model\Config::VARNISH && $this->config->isEnabled() - && $response instanceof \Magento\Framework\App\Response\Http) { + public function afterDispatch(FrontControllerInterface $subject, $result) + { + if ($this->config->getType() == Config::VARNISH && $this->config->isEnabled() + && $result instanceof ResponseHttp + ) { $this->version->process(); - if ($this->state->getMode() == \Magento\Framework\App\State::MODE_DEVELOPER) { - $response->setHeader('X-Magento-Debug', 1); + + if ($this->state->getMode() == AppState::MODE_DEVELOPER) { + $result->setHeader('X-Magento-Debug', 1); } } - return $response; + + return $result; } } diff --git a/app/code/Magento/PageCache/Model/Controller/Result/BuiltinPlugin.php b/app/code/Magento/PageCache/Model/Controller/Result/BuiltinPlugin.php index 0fcad45130d3ebb0dfe94ae8eeafbd885fb5e38b..d2ef015fb69272586576a335ce7df3c9d81acf59 100644 --- a/app/code/Magento/PageCache/Model/Controller/Result/BuiltinPlugin.php +++ b/app/code/Magento/PageCache/Model/Controller/Result/BuiltinPlugin.php @@ -5,47 +5,50 @@ */ namespace Magento\PageCache\Model\Controller\Result; +use Magento\PageCache\Model\Config; +use Magento\Framework\App\PageCache\Kernel; +use Magento\Framework\App\State as AppState; +use Magento\Framework\Registry; +use Magento\Framework\Controller\ResultInterface; use Magento\Framework\App\Response\Http as ResponseHttp; +use Zend\Http\Header\HeaderInterface as HttpHeaderInterface; +use Magento\PageCache\Model\Cache\Type as CacheType; /** * Plugin for processing builtin cache + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class BuiltinPlugin { /** - * @var \Magento\Framework\App\Config\ScopeConfigInterface + * @var Config */ - protected $config; + private $config; /** - * @var \Magento\Framework\App\PageCache\Kernel + * @var Kernel */ - protected $kernel; + private $kernel; /** - * @var \Magento\Framework\App\State + * @var AppState */ - protected $state; + private $state; /** - * @var \Magento\Framework\Registry + * @var Registry */ - protected $registry; + private $registry; /** - * Constructor - * - * @param \Magento\PageCache\Model\Config $config - * @param \Magento\Framework\App\PageCache\Kernel $kernel - * @param \Magento\Framework\App\State $state - * @param \Magento\Framework\Registry $registry + * @param Config $config + * @param Kernel $kernel + * @param AppState $state + * @param Registry $registry */ - public function __construct( - \Magento\PageCache\Model\Config $config, - \Magento\Framework\App\PageCache\Kernel $kernel, - \Magento\Framework\App\State $state, - \Magento\Framework\Registry $registry - ) { + public function __construct(Config $config, Kernel $kernel, AppState $state, Registry $registry) + { $this->config = $config; $this->kernel = $kernel; $this->state = $state; @@ -53,43 +56,45 @@ class BuiltinPlugin } /** - * @param \Magento\Framework\Controller\ResultInterface $subject - * @param callable $proceed + * Perform result postprocessing + * + * @param ResultInterface $subject + * @param ResultInterface $result * @param ResponseHttp $response - * @return \Magento\Framework\Controller\ResultInterface + * @return ResultInterface + * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundRenderResult( - \Magento\Framework\Controller\ResultInterface $subject, - \Closure $proceed, - ResponseHttp $response - ) { - $result = $proceed($response); + public function afterRenderResult(ResultInterface $subject, ResultInterface $result, ResponseHttp $response) + { $usePlugin = $this->registry->registry('use_page_cache_plugin'); - if (!$usePlugin || !$this->config->isEnabled() - || $this->config->getType() != \Magento\PageCache\Model\Config::BUILT_IN - ) { + + if (!$usePlugin || !$this->config->isEnabled() || $this->config->getType() != Config::BUILT_IN) { return $result; } - if ($this->state->getMode() == \Magento\Framework\App\State::MODE_DEVELOPER) { + if ($this->state->getMode() == AppState::MODE_DEVELOPER) { $cacheControlHeader = $response->getHeader('Cache-Control'); - if ($cacheControlHeader instanceof \Zend\Http\Header\HeaderInterface) { + + if ($cacheControlHeader instanceof HttpHeaderInterface) { $response->setHeader('X-Magento-Cache-Control', $cacheControlHeader->getFieldValue()); } + $response->setHeader('X-Magento-Cache-Debug', 'MISS', true); } $tagsHeader = $response->getHeader('X-Magento-Tags'); $tags = []; + if ($tagsHeader) { $tags = explode(',', $tagsHeader->getFieldValue()); $response->clearHeader('X-Magento-Tags'); } - $tags = array_unique(array_merge($tags, [\Magento\PageCache\Model\Cache\Type::CACHE_TAG])); - $response->setHeader('X-Magento-Tags', implode(',', $tags)); + $tags = array_unique(array_merge($tags, [CacheType::CACHE_TAG])); + $response->setHeader('X-Magento-Tags', implode(',', $tags)); $this->kernel->process($response); + return $result; } } diff --git a/app/code/Magento/PageCache/Model/Controller/Result/VarnishPlugin.php b/app/code/Magento/PageCache/Model/Controller/Result/VarnishPlugin.php index 19868a2261f1fd7d486fab066b1e4cc02704a6c5..368a6db80c7e01d7fdaafca710beb3470d870e33 100644 --- a/app/code/Magento/PageCache/Model/Controller/Result/VarnishPlugin.php +++ b/app/code/Magento/PageCache/Model/Controller/Result/VarnishPlugin.php @@ -5,7 +5,12 @@ */ namespace Magento\PageCache\Model\Controller\Result; +use Magento\PageCache\Model\Config; +use Magento\Framework\App\PageCache\Version; +use Magento\Framework\App\State as AppState; +use Magento\Framework\Registry; use Magento\Framework\App\Response\Http as ResponseHttp; +use Magento\Framework\Controller\ResultInterface; /** * Plugin for processing varnish cache @@ -13,74 +18,61 @@ use Magento\Framework\App\Response\Http as ResponseHttp; class VarnishPlugin { /** - * @var \Magento\Framework\App\Config\ScopeConfigInterface + * @var Config */ - protected $config; + private $config; /** - * @var \Magento\Framework\App\PageCache\Version + * @var Version */ - protected $version; + private $version; /** - * @var \Magento\Framework\App\PageCache\Kernel + * @var AppState */ - protected $kernel; + private $state; /** - * @var \Magento\Framework\App\State + * @var Registry */ - protected $state; + private $registry; /** - * @var \Magento\Framework\Registry + * @param Config $config + * @param Version $version + * @param AppState $state + * @param Registry $registry */ - protected $registry; - - /** - * Constructor - * - * @param \Magento\PageCache\Model\Config $config - * @param \Magento\Framework\App\PageCache\Version $version - * @param \Magento\Framework\App\PageCache\Kernel $kernel - * @param \Magento\Framework\App\State $state - * @param \Magento\Framework\Registry $registry - */ - public function __construct( - \Magento\PageCache\Model\Config $config, - \Magento\Framework\App\PageCache\Version $version, - \Magento\Framework\App\PageCache\Kernel $kernel, - \Magento\Framework\App\State $state, - \Magento\Framework\Registry $registry - ) { + public function __construct(Config $config, Version $version, AppState $state, Registry $registry) + { $this->config = $config; $this->version = $version; - $this->kernel = $kernel; $this->state = $state; $this->registry = $registry; } /** - * @param \Magento\Framework\Controller\ResultInterface $subject - * @param callable $proceed + * Perform result postprocessing + * + * @param ResultInterface $subject + * @param ResultInterface $result * @param ResponseHttp $response - * @return \Magento\Framework\Controller\ResultInterface + * @return ResultInterface + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundRenderResult( - \Magento\Framework\Controller\ResultInterface $subject, - \Closure $proceed, - ResponseHttp $response - ) { - $proceed($response); + public function afterRenderResult(ResultInterface $subject, ResultInterface $result, ResponseHttp $response) + { $usePlugin = $this->registry->registry('use_page_cache_plugin'); - if ($this->config->getType() == \Magento\PageCache\Model\Config::VARNISH && $this->config->isEnabled() - && $usePlugin) { + + if ($this->config->getType() == Config::VARNISH && $this->config->isEnabled() && $usePlugin) { $this->version->process(); - if ($this->state->getMode() == \Magento\Framework\App\State::MODE_DEVELOPER) { + + if ($this->state->getMode() == AppState::MODE_DEVELOPER) { $response->setHeader('X-Magento-Debug', 1); } } - return $subject; + return $result; } } diff --git a/app/code/Magento/PageCache/Test/Unit/Model/App/FrontController/VarnishPluginTest.php b/app/code/Magento/PageCache/Test/Unit/Model/App/FrontController/VarnishPluginTest.php index 32328d7d027930b768683f9773366f6be9b92f84..a9b0e10307ae37d556f9c4cc96224f01fcd2bb9d 100644 --- a/app/code/Magento/PageCache/Test/Unit/Model/App/FrontController/VarnishPluginTest.php +++ b/app/code/Magento/PageCache/Test/Unit/Model/App/FrontController/VarnishPluginTest.php @@ -3,148 +3,169 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\PageCache\Test\Unit\Model\App\FrontController; use Magento\PageCache\Model\App\FrontController\VarnishPlugin; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Magento\PageCache\Model\Config; +use Magento\Framework\App\PageCache\Version; +use Magento\Framework\App\State as AppState; +use Magento\Framework\App\FrontControllerInterface; +use Magento\Framework\App\Response\Http as ResponseHttp; +use Magento\Framework\Controller\ResultInterface; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class VarnishPluginTest extends \PHPUnit_Framework_TestCase { /** * @var VarnishPlugin */ - protected $plugin; + private $plugin; /** - * @var \Magento\PageCache\Model\Config|\PHPUnit_Framework_MockObject_MockObject + * @var ObjectManagerHelper */ - protected $configMock; + private $objectManagerHelper; /** - * @var \Magento\Framework\App\PageCache\Version|\PHPUnit_Framework_MockObject_MockObject + * @var Config|\PHPUnit_Framework_MockObject_MockObject */ - protected $versionMock; + private $configMock; /** - * @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject + * @var Version|\PHPUnit_Framework_MockObject_MockObject */ - protected $stateMock; + private $versionMock; /** - * @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject + * @var AppState|\PHPUnit_Framework_MockObject_MockObject */ - protected $responseMock; + private $stateMock; /** - * @var \Magento\Framework\App\FrontControllerInterface|\PHPUnit_Framework_MockObject_MockObject + * @var FrontControllerInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $frontControllerMock; + private $frontControllerMock; /** - * @var \Closure + * @var ResponseHttp|\PHPUnit_Framework_MockObject_MockObject */ - protected $closure; + private $responseMock; /** - * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject + * @var ResultInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $requestMock; + private $resultMock; - /** - * SetUp - */ protected function setUp() { - $this->configMock = $this->getMock(\Magento\PageCache\Model\Config::class, [], [], '', false); - $this->versionMock = $this->getMock(\Magento\Framework\App\PageCache\Version::class, [], [], '', false); - $this->stateMock = $this->getMock(\Magento\Framework\App\State::class, [], [], '', false); - $this->frontControllerMock = $this->getMock( - \Magento\Framework\App\FrontControllerInterface::class, - [], - [], - '', - false - ); - $this->requestMock = $this->getMock(\Magento\Framework\App\RequestInterface::class, [], [], '', false); - $this->responseMock = $this->getMock(\Magento\Framework\App\Response\Http::class, [], [], '', false); - $response = $this->responseMock; - $this->closure = function () use ($response) { - return $response; - }; - $this->plugin = new \Magento\PageCache\Model\App\FrontController\VarnishPlugin( - $this->configMock, - $this->versionMock, - $this->stateMock + $this->configMock = $this->getMockBuilder(Config::class) + ->disableOriginalConstructor() + ->getMock(); + $this->versionMock = $this->getMockBuilder(Version::class) + ->disableOriginalConstructor() + ->getMock(); + $this->stateMock = $this->getMockBuilder(AppState::class) + ->disableOriginalConstructor() + ->getMock(); + $this->frontControllerMock = $this->getMockBuilder(FrontControllerInterface::class) + ->getMockForAbstractClass(); + $this->responseMock = $this->getMockBuilder(ResponseHttp::class) + ->disableOriginalConstructor() + ->getMock(); + $this->resultMock = $this->getMockBuilder(ResultInterface::class) + ->getMockForAbstractClass(); + + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->plugin = $this->objectManagerHelper->getObject( + VarnishPlugin::class, + [ + 'config' => $this->configMock, + 'version' => $this->versionMock, + 'state' => $this->stateMock + ] ); } /** - * @dataProvider dataProvider + * @param string $state + * @param int $countHeader + * + * @dataProvider afterDispatchDataProvider */ - public function testAroundDispatchReturnsCache($state, $countHeader, $countProcess, $countGetMode, $response) + public function testAfterDispatchReturnsCache($state, $countHeader) { - $this->configMock - ->expects($this->once()) + $this->configMock->expects(static::once()) ->method('isEnabled') - ->will($this->returnValue(true)); - $this->configMock - ->expects($this->once()) + ->willReturn(true); + $this->configMock->expects(static::once()) ->method('getType') - ->will($this->returnValue(\Magento\PageCache\Model\Config::VARNISH)); - $this->versionMock - ->expects($countProcess) + ->willReturn(Config::VARNISH); + $this->versionMock->expects(static::once()) ->method('process'); - $this->stateMock->expects($countGetMode) + $this->stateMock->expects(static::once()) ->method('getMode') - ->will($this->returnValue($state)); - $response->expects($countHeader) + ->willReturn($state); + $this->responseMock->expects(static::exactly($countHeader)) ->method('setHeader') ->with('X-Magento-Debug'); - $this->closure = function () use ($response) { - return $response; - }; + $this->assertSame( + $this->responseMock, + $this->plugin->afterDispatch($this->frontControllerMock, $this->responseMock) + ); + } - $this->plugin->aroundDispatch($this->frontControllerMock, $this->closure, $this->requestMock); + public function testAfterDispatchNotResponse() + { + $this->configMock->expects(static::once()) + ->method('isEnabled') + ->willReturn(true); + $this->configMock->expects(static::once()) + ->method('getType') + ->willReturn(Config::VARNISH); + $this->versionMock->expects(static::never()) + ->method('process'); + $this->stateMock->expects(static::never()) + ->method('getMode'); + $this->resultMock->expects(static::never()) + ->method('setHeader'); + + $this->assertSame( + $this->resultMock, + $this->plugin->afterDispatch($this->frontControllerMock, $this->resultMock) + ); } - /** - * @dataProvider dataProvider - */ - public function testAroundDispatchDisabled($state) + public function testAfterDispatchDisabled() { - $this->configMock - ->expects($this->any()) + $this->configMock->expects(static::any()) ->method('getType') - ->will($this->returnValue(null)); - $this->versionMock - ->expects($this->never()) + ->willReturn(null); + $this->versionMock->expects(static::never()) ->method('process'); - $this->stateMock->expects($this->any()) + $this->stateMock->expects(static::any()) ->method('getMode') - ->will($this->returnValue($state)); - $this->responseMock->expects($this->never()) + ->willReturn(AppState::MODE_DEVELOPER); + $this->responseMock->expects(static::never()) ->method('setHeader'); - $this->plugin->aroundDispatch($this->frontControllerMock, $this->closure, $this->requestMock); + + $this->assertSame( + $this->responseMock, + $this->plugin->afterDispatch($this->frontControllerMock, $this->responseMock) + ); } - public function dataProvider() + /** + * @return array + */ + public function afterDispatchDataProvider() { return [ - 'developer_mode' => [ - \Magento\Framework\App\State::MODE_DEVELOPER, - $this->once(), - $this->once(), - $this->once(), - $this->getMock(\Magento\Framework\App\Response\Http::class, [], [], '', false), - ], - 'production' => [ - \Magento\Framework\App\State::MODE_PRODUCTION, - $this->never(), - $this->never(), - $this->never(), - $this->getMock(\Magento\Framework\Controller\ResultInterface::class, [], [], '', false), - ], + 'developer_mode' => [AppState::MODE_DEVELOPER, 1], + 'production' => [AppState::MODE_PRODUCTION, 0] ]; } } diff --git a/app/code/Magento/PageCache/Test/Unit/Model/Controller/Result/BuiltinPluginTest.php b/app/code/Magento/PageCache/Test/Unit/Model/Controller/Result/BuiltinPluginTest.php index 15934f3104b2305c00a8a0f2b23c3ab8717daba7..f0ba7b22dad2d5126b6b1481f8776f5bacc7843f 100644 --- a/app/code/Magento/PageCache/Test/Unit/Model/Controller/Result/BuiltinPluginTest.php +++ b/app/code/Magento/PageCache/Test/Unit/Model/Controller/Result/BuiltinPluginTest.php @@ -3,141 +3,195 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - -// @codingStandardsIgnoreFile - namespace Magento\PageCache\Test\Unit\Model\Controller\Result; +use Magento\PageCache\Model\Controller\Result\BuiltinPlugin; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Magento\PageCache\Model\Config; +use Magento\Framework\App\PageCache\Kernel; +use Magento\Framework\App\State as AppState; +use Magento\Framework\Registry; +use Magento\Framework\Controller\ResultInterface; +use Magento\Framework\App\Response\Http as ResponseHttp; +use Zend\Http\Header\HeaderInterface as HttpHeaderInterface; +use Magento\PageCache\Model\Cache\Type as CacheType; + +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class BuiltinPluginTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\PageCache\Model\Controller\Result\BuiltinPlugin + * @var BuiltinPlugin + */ + private $plugin; + + /** + * @var ObjectManagerHelper */ - protected $plugin; + private $objectManagerHelper; /** - * @var \Magento\Framework\Controller\ResultInterface + * @var Config|\PHPUnit_Framework_MockObject_MockObject */ - protected $subject; + private $configMock; /** - * @var \Closure + * @var Kernel|\PHPUnit_Framework_MockObject_MockObject */ - protected $closure; + private $kernelMock; /** - * @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject + * @var AppState|\PHPUnit_Framework_MockObject_MockObject */ - protected $response; + private $stateMock; /** - * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject + * @var Registry|\PHPUnit_Framework_MockObject_MockObject */ - protected $registry; + private $registryMock; /** - * @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject + * @var ResultInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $state; + private $resultMock; /** - * @var \Zend\Http\Header\HeaderInterface|\PHPUnit_Framework_MockObject_MockObject + * @var ResponseHttp|\PHPUnit_Framework_MockObject_MockObject */ - protected $header; + private $responseMock; /** - * @var \Magento\Framework\App\PageCache\Kernel|\PHPUnit_Framework_MockObject_MockObject + * @var HttpHeaderInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $kernel; + private $httpHeaderMock; protected function setUp() { - $result = $this->getMock(\Magento\Framework\Controller\ResultInterface::class, [], [], '', false); - $this->closure = function() use ($result) { - return $result; - }; - - $this->header = $this->getMock(\Zend\Http\Header\HeaderInterface::class, [], [], '', false); - $this->subject = $this->getMock(\Magento\Framework\Controller\ResultInterface::class, [], [], '', false); - $this->response = $this->getMock( - \Magento\Framework\App\Response\Http::class, - ['getHeader', 'clearHeader', 'setHeader'], - [], - '', - false - ); - $this->response->expects($this->any())->method('getHeader')->willReturnMap( + $this->configMock = $this->getMockBuilder(Config::class) + ->disableOriginalConstructor() + ->getMock(); + $this->kernelMock = $this->getMockBuilder(Kernel::class) + ->disableOriginalConstructor() + ->getMock(); + $this->stateMock = $this->getMockBuilder(AppState::class) + ->disableOriginalConstructor() + ->getMock(); + $this->registryMock = $this->getMockBuilder(Registry::class) + ->disableOriginalConstructor() + ->getMock(); + $this->resultMock = $this->getMockBuilder(ResultInterface::class) + ->getMockForAbstractClass(); + $this->responseMock = $this->getMockBuilder(ResponseHttp::class) + ->disableOriginalConstructor() + ->getMock(); + $this->httpHeaderMock = $this->getMockBuilder(HttpHeaderInterface::class) + ->getMockForAbstractClass(); + + $this->responseMock->expects(static::any()) + ->method('getHeader') + ->willReturnMap( + [ + ['X-Magento-Tags', $this->httpHeaderMock], + ['Cache-Control', $this->httpHeaderMock] + ] + ); + $this->configMock->expects(static::any()) + ->method('isEnabled') + ->willReturn(true); + $this->configMock->expects(static::any()) + ->method('getType') + ->willReturn(Config::BUILT_IN); + + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->plugin = $this->objectManagerHelper->getObject( + BuiltinPlugin::class, [ - ['X-Magento-Tags', $this->header], - ['Cache-Control', $this->header] - ] - ); - - $this->registry = $this->getMock(\Magento\Framework\Registry::class, [], [], '', false); - - $config = $this->getMock(\Magento\PageCache\Model\Config::class, ['isEnabled', 'getType'], [], '', false); - $config->expects($this->any())->method('isEnabled')->willReturn(true); - $config->expects($this->any())->method('getType')->willReturn(\Magento\PageCache\Model\Config::BUILT_IN); - - $this->kernel = $this->getMock(\Magento\Framework\App\PageCache\Kernel::class, [], [], '', false); - - $this->state = $this->getMock(\Magento\Framework\App\State::class, [], [], '', false); - $this->plugin = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject( - \Magento\PageCache\Model\Controller\Result\BuiltinPlugin::class, - [ - 'registry' => $this->registry, - 'config' => $config, - 'kernel' => $this->kernel, - 'state' => $this->state + 'registry' => $this->registryMock, + 'config' => $this->configMock, + 'kernel' => $this->kernelMock, + 'state' => $this->stateMock ] ); } - public function testAroundResultWithoutPlugin() + public function testAfterResultWithoutPlugin() { - $this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin')->willReturn(false); - $this->kernel->expects($this->never())->method('process')->with($this->response); + $this->registryMock->expects(static::once()) + ->method('registry') + ->with('use_page_cache_plugin') + ->willReturn(false); + $this->kernelMock->expects(static::never()) + ->method('process') + ->with($this->responseMock); + $this->assertSame( - call_user_func($this->closure), - $this->plugin->aroundRenderResult($this->subject, $this->closure, $this->response) + $this->resultMock, + $this->plugin->afterRenderResult($this->resultMock, $this->resultMock, $this->responseMock) ); } - public function testAroundResultWithPlugin() + public function testAfterResultWithPlugin() { - $this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin')->willReturn(true); - $this->state->expects($this->once())->method('getMode')->willReturn(null); - $this->header->expects($this->any())->method('getFieldValue')->willReturn('tag,tag'); - $this->response->expects($this->once())->method('clearHeader')->with('X-Magento-Tags'); - $this->response->expects($this->once())->method('setHeader')->with( - 'X-Magento-Tags', - 'tag,' . \Magento\PageCache\Model\Cache\Type::CACHE_TAG + $this->registryMock->expects(static::once()) + ->method('registry') + ->with('use_page_cache_plugin') + ->willReturn(true); + $this->stateMock->expects(static::once()) + ->method('getMode') + ->willReturn(null); + $this->httpHeaderMock->expects(static::any()) + ->method('getFieldValue') + ->willReturn('tag,tag'); + $this->responseMock->expects(static::once()) + ->method('clearHeader') + ->with('X-Magento-Tags'); + $this->responseMock->expects(static::once()) + ->method('setHeader') + ->with('X-Magento-Tags', 'tag,' . CacheType::CACHE_TAG); + $this->kernelMock->expects(static::once()) + ->method('process') + ->with($this->responseMock); + + $this->assertSame( + $this->resultMock, + $this->plugin->afterRenderResult($this->resultMock, $this->resultMock, $this->responseMock) ); - $this->kernel->expects($this->once())->method('process')->with($this->response); - $result = call_user_func($this->closure); - $this->assertSame($result, $this->plugin->aroundRenderResult($this->subject, $this->closure, $this->response)); } - public function testAroundResultWithPluginDeveloperMode() + public function testAfterResultWithPluginDeveloperMode() { - $this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin')->willReturn(true); - $this->state->expects($this->once())->method('getMode') - ->willReturn(\Magento\Framework\App\State::MODE_DEVELOPER); - - $this->header->expects($this->any())->method('getFieldValue')->willReturnOnConsecutiveCalls('test', 'tag,tag2'); + $this->registryMock->expects(static::once()) + ->method('registry') + ->with('use_page_cache_plugin') + ->willReturn(true); + $this->stateMock->expects(static::once()) + ->method('getMode') + ->willReturn(AppState::MODE_DEVELOPER); + $this->httpHeaderMock->expects(static::any()) + ->method('getFieldValue') + ->willReturnOnConsecutiveCalls('test', 'tag,tag2'); + $this->responseMock->expects(static::any()) + ->method('setHeader') + ->withConsecutive( + ['X-Magento-Cache-Control', 'test'], + ['X-Magento-Cache-Debug', 'MISS', true], + ['X-Magento-Tags', 'tag,tag2,' . CacheType::CACHE_TAG] + ); + $this->responseMock->expects(static::once()) + ->method('clearHeader') + ->with('X-Magento-Tags'); + $this->registryMock->expects(static::once()) + ->method('registry') + ->with('use_page_cache_plugin') + ->willReturn(true); + $this->kernelMock->expects(static::once()) + ->method('process') + ->with($this->responseMock); - $this->response->expects($this->any())->method('setHeader')->withConsecutive( - ['X-Magento-Cache-Control', 'test'], - ['X-Magento-Cache-Debug', 'MISS', true], - ['X-Magento-Tags', 'tag,tag2,' . \Magento\PageCache\Model\Cache\Type::CACHE_TAG] + $this->assertSame( + $this->resultMock, + $this->plugin->afterRenderResult($this->resultMock, $this->resultMock, $this->responseMock) ); - - $this->response->expects($this->once())->method('clearHeader')->with('X-Magento-Tags'); - $this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin') - ->will($this->returnValue(true)); - $this->kernel->expects($this->once())->method('process')->with($this->response); - - $result = call_user_func($this->closure); - $this->assertSame($result, $this->plugin->aroundRenderResult($this->subject, $this->closure, $this->response)); } } diff --git a/app/code/Magento/PageCache/Test/Unit/Model/Controller/Result/VarnishPluginTest.php b/app/code/Magento/PageCache/Test/Unit/Model/Controller/Result/VarnishPluginTest.php index e58aae1668ff8dbaeda016465d126b63c1fc4924..a54dc5bacb5a6dc31dc317622c74a9f7b18ba2d0 100644 --- a/app/code/Magento/PageCache/Test/Unit/Model/Controller/Result/VarnishPluginTest.php +++ b/app/code/Magento/PageCache/Test/Unit/Model/Controller/Result/VarnishPluginTest.php @@ -5,72 +5,135 @@ */ namespace Magento\PageCache\Test\Unit\Model\Controller\Result; +use Magento\PageCache\Model\Controller\Result\VarnishPlugin; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Magento\PageCache\Model\Config; +use Magento\Framework\App\PageCache\Version; +use Magento\Framework\App\State as AppState; +use Magento\Framework\Registry; +use Magento\Framework\Controller\ResultInterface; +use Magento\Framework\App\Response\Http as ResponseHttp; + +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class VarnishPluginTest extends \PHPUnit_Framework_TestCase { /** - * @param bool $usePlugin - * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $setCacheDebugHeaderCount - * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $getModeCount - * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $processCount - * @dataProvider dataProvider + * @var VarnishPlugin */ - public function testAroundResult($usePlugin, $setCacheDebugHeaderCount, $getModeCount, $processCount) - { - /** @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject $response */ - $response = $this->getMock(\Magento\Framework\App\Response\Http::class, [], [], '', false); - $response->expects($setCacheDebugHeaderCount)->method('setHeader') - ->with('X-Magento-Debug', 1); + private $plugin; + + /** + * @var ObjectManagerHelper + */ + private $objectManagerHelper; + + /** + * @var Config|\PHPUnit_Framework_MockObject_MockObject + */ + private $configMock; - /** @var \Magento\Framework\Controller\ResultInterface $result */ - $result = $this->getMock(\Magento\Framework\Controller\ResultInterface::class, [], [], '', false); - $closure = function () use ($result) { - return $result; - }; + /** + * @var Version|\PHPUnit_Framework_MockObject_MockObject + */ + private $versionMock; - /** @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject $registry */ - $registry = $this->getMock(\Magento\Framework\Registry::class, [], [], '', false); - $registry->expects($this->once())->method('registry')->with('use_page_cache_plugin') - ->will($this->returnValue($usePlugin)); + /** + * @var AppState|\PHPUnit_Framework_MockObject_MockObject + */ + private $appStateMock; - /** @var \Magento\PageCache\Model\Config|\PHPUnit_Framework_MockObject_MockObject $config */ - $config = $this->getMock(\Magento\PageCache\Model\Config::class, [], [], '', false); - $config->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); - $config->expects($this->once())->method('getType') - ->will($this->returnValue(\Magento\PageCache\Model\Config::VARNISH)); + /** + * @var Registry|\PHPUnit_Framework_MockObject_MockObject + */ + private $registryMock; - /** @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject $state */ - $state = $this->getMock(\Magento\Framework\App\State::class, [], [], '', false); - $state->expects($getModeCount)->method('getMode') - ->will($this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER)); + /** + * @var ResultInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $resultMock; - /** @var \Magento\Framework\Controller\ResultInterface|\PHPUnit_Framework_MockObject_MockObject $subject */ - $subject = $this->getMock(\Magento\Framework\Controller\ResultInterface::class, [], [], '', false); + /** + * @var ResponseHttp|\PHPUnit_Framework_MockObject_MockObject + */ + private $responseMock; - /** @var \Magento\Framework\App\PageCache\Version|\PHPUnit_Framework_MockObject_MockObject $version */ - $version = $this->getMock(\Magento\Framework\App\PageCache\Version::class, [], [], '', false); - $version->expects($processCount)->method('process'); + protected function setUp() + { + $this->configMock = $this->getMockBuilder(Config::class) + ->disableOriginalConstructor() + ->getMock(); + $this->versionMock = $this->getMockBuilder(Version::class) + ->disableOriginalConstructor() + ->getMock(); + $this->appStateMock = $this->getMockBuilder(AppState::class) + ->disableOriginalConstructor() + ->getMock(); + $this->registryMock = $this->getMockBuilder(Registry::class) + ->disableOriginalConstructor() + ->getMock(); + $this->resultMock = $this->getMockBuilder(ResultInterface::class) + ->getMockForAbstractClass(); + $this->responseMock = $this->getMockBuilder(ResponseHttp::class) + ->disableOriginalConstructor() + ->getMock(); - /** @var \Magento\PageCache\Model\Controller\Result\VarnishPlugin $plugin */ - $plugin = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject( - \Magento\PageCache\Model\Controller\Result\VarnishPlugin::class, + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->plugin = $this->objectManagerHelper->getObject( + VarnishPlugin::class, [ - 'registry' => $registry, - 'config' => $config, - 'state' => $state, - 'version' => $version + 'registry' => $this->registryMock, + 'config' => $this->configMock, + 'state' => $this->appStateMock, + 'version' => $this->versionMock ] ); - $this->assertSame($subject, $plugin->aroundRenderResult($subject, $closure, $response)); + } + + /** + * @param bool $usePlugin + * @param int $setCacheDebugHeaderCount + * @param int $getModeCount + * @param int $processCount + * + * @dataProvider afterRenderResultDataProvider + */ + public function testAfterRenderResult($usePlugin, $setCacheDebugHeaderCount, $getModeCount, $processCount) + { + $this->responseMock->expects(static::exactly($setCacheDebugHeaderCount)) + ->method('setHeader') + ->with('X-Magento-Debug', 1); + $this->registryMock->expects(static::once()) + ->method('registry') + ->with('use_page_cache_plugin') + ->willReturn($usePlugin); + $this->configMock->expects(static::once()) + ->method('isEnabled') + ->willReturn(true); + $this->configMock->expects(static::once()) + ->method('getType') + ->willReturn(Config::VARNISH); + $this->appStateMock->expects(static::exactly($getModeCount)) + ->method('getMode') + ->willReturn(AppState::MODE_DEVELOPER); + $this->versionMock->expects(static::exactly($processCount)) + ->method('process'); + + $this->assertSame( + $this->resultMock, + $this->plugin->afterRenderResult($this->resultMock, $this->resultMock, $this->responseMock) + ); } /** * @return array */ - public function dataProvider() + public function afterRenderResultDataProvider() { return [ - [true, $this->once(), $this->once(), $this->once()], - [false, $this->never(), $this->never(), $this->never()] + [true, 1, 1, 1], + [false, 0, 0, 0] ]; } } diff --git a/app/code/Magento/Paypal/Block/Adminhtml/Store/SwitcherPlugin.php b/app/code/Magento/Paypal/Block/Adminhtml/Store/SwitcherPlugin.php index 3af4493e61e1f869a9c5288c076f609add10b4fe..23b7fa31fb578056dba1e4e9ea440dd0ec53bc63 100644 --- a/app/code/Magento/Paypal/Block/Adminhtml/Store/SwitcherPlugin.php +++ b/app/code/Magento/Paypal/Block/Adminhtml/Store/SwitcherPlugin.php @@ -5,26 +5,28 @@ */ namespace Magento\Paypal\Block\Adminhtml\Store; +use Magento\Backend\Block\Store\Switcher as StoreSwitcherBlock; +use Magento\Paypal\Model\Config\StructurePlugin as ConfigStructurePlugin; + +/** + * Plugin for \Magento\Backend\Block\Store\Switcher + */ class SwitcherPlugin { /** * Remove country request param from url * - * @param \Magento\Backend\Block\Store\Switcher $subject - * @param \Closure $proceed + * @param StoreSwitcherBlock $subject * @param string $route * @param array $params - * @return string + * @return array */ - public function aroundGetUrl( - \Magento\Backend\Block\Store\Switcher $subject, - \Closure $proceed, - $route = '', - $params = [] - ) { - if ($subject->getRequest()->getParam(\Magento\Paypal\Model\Config\StructurePlugin::REQUEST_PARAM_COUNTRY)) { - $params[\Magento\Paypal\Model\Config\StructurePlugin::REQUEST_PARAM_COUNTRY] = null; + public function beforeGetUrl(StoreSwitcherBlock $subject, $route = '', $params = []) + { + if ($subject->getRequest()->getParam(ConfigStructurePlugin::REQUEST_PARAM_COUNTRY)) { + $params[ConfigStructurePlugin::REQUEST_PARAM_COUNTRY] = null; } - return $proceed($route, $params); + + return [$route, $params]; } } diff --git a/app/code/Magento/Paypal/Model/Config/Structure/Element/FieldPlugin.php b/app/code/Magento/Paypal/Model/Config/Structure/Element/FieldPlugin.php index 2d49ddfcb3d01955fefd089052f4b444d9c91134..a6288b8fc685c45aa823e807433c1df896c5dc9f 100644 --- a/app/code/Magento/Paypal/Model/Config/Structure/Element/FieldPlugin.php +++ b/app/code/Magento/Paypal/Model/Config/Structure/Element/FieldPlugin.php @@ -5,39 +5,45 @@ */ namespace Magento\Paypal\Model\Config\Structure\Element; +use Magento\Framework\App\RequestInterface; +use Magento\Config\Model\Config\Structure\Element\Field as FieldConfigStructure; +use Magento\Paypal\Model\Config\StructurePlugin as ConfigStructurePlugin; + +/** + * Plugin for \Magento\Config\Model\Config\Structure\Element\Field + */ class FieldPlugin { /** - * @var \Magento\Framework\App\RequestInterface + * @var RequestInterface */ - protected $_request; + private $request; /** - * @param \Magento\Framework\App\RequestInterface $request + * @param RequestInterface $request */ - public function __construct(\Magento\Framework\App\RequestInterface $request) + public function __construct(RequestInterface $request) { - $this->_request = $request; + $this->request = $request; } /** * Get original configPath (not changed by PayPal configuration inheritance) * - * @param \Magento\Config\Model\Config\Structure\Element\Field $subject - * @param \Closure $proceed + * @param FieldConfigStructure $subject + * @param string|null $result * @return string|null */ - public function aroundGetConfigPath( - \Magento\Config\Model\Config\Structure\Element\Field $subject, - \Closure $proceed - ) { - $configPath = $proceed(); - if (!isset($configPath) && $this->_request->getParam('section') == 'payment') { - $configPath = preg_replace('@^(' . implode( - '|', - \Magento\Paypal\Model\Config\StructurePlugin::getPaypalConfigCountries(true) - ) . ')/@', 'payment/', $subject->getPath()); + public function afterGetConfigPath(FieldConfigStructure $subject, $result) + { + if (!$result && $this->request->getParam('section') == 'payment') { + $result = preg_replace( + '@^(' . implode('|', ConfigStructurePlugin::getPaypalConfigCountries(true)) . ')/@', + 'payment/', + $subject->getPath() + ); } - return $configPath; + + return $result; } } diff --git a/app/code/Magento/Paypal/Model/Config/StructurePlugin.php b/app/code/Magento/Paypal/Model/Config/StructurePlugin.php index 52f46a8411c0257d1f894ce0693d6b77d8dd35c0..f45a7948906253ec53a4dd710b37825363f39877 100644 --- a/app/code/Magento/Paypal/Model/Config/StructurePlugin.php +++ b/app/code/Magento/Paypal/Model/Config/StructurePlugin.php @@ -11,6 +11,9 @@ use Magento\Config\Model\Config\Structure\Element\Section; use Magento\Config\Model\Config\Structure\ElementInterface; use Magento\Paypal\Helper\Backend as BackendHelper; +/** + * Plugin for \Magento\Config\Model\Config\Structure + */ class StructurePlugin { /** @@ -21,17 +24,17 @@ class StructurePlugin /** * @var BackendHelper */ - protected $_helper; + private $backendHelper; /** * @var ScopeDefiner */ - protected $_scopeDefiner; + private $scopeDefiner; /** * @var string[] */ - private static $_paypalConfigCountries = [ + private static $paypalConfigCountries = [ 'payment_us', 'payment_ca', 'payment_au', @@ -49,12 +52,10 @@ class StructurePlugin * @param ScopeDefiner $scopeDefiner * @param BackendHelper $helper */ - public function __construct( - ScopeDefiner $scopeDefiner, - BackendHelper $helper - ) { - $this->_scopeDefiner = $scopeDefiner; - $this->_helper = $helper; + public function __construct(ScopeDefiner $scopeDefiner, BackendHelper $helper) + { + $this->scopeDefiner = $scopeDefiner; + $this->backendHelper = $helper; } /** @@ -65,10 +66,12 @@ class StructurePlugin */ public static function getPaypalConfigCountries($addOther = false) { - $countries = self::$_paypalConfigCountries; + $countries = self::$paypalConfigCountries; + if ($addOther) { $countries[] = 'payment_other'; } + return $countries; } @@ -78,39 +81,45 @@ class StructurePlugin * @param Structure $subject * @param \Closure $proceed * @param array $pathParts - * @return ElementInterface + * @return ElementInterface|null + * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundGetElementByPathParts( - Structure $subject, - \Closure $proceed, - array $pathParts - ) { + public function aroundGetElementByPathParts(Structure $subject, \Closure $proceed, array $pathParts) + { $isSectionChanged = $pathParts[0] == 'payment'; + if ($isSectionChanged) { - $requestedCountrySection = 'payment_' . strtolower($this->_helper->getConfigurationCountryCode()); + $requestedCountrySection = 'payment_' . strtolower($this->backendHelper->getConfigurationCountryCode()); + if (in_array($requestedCountrySection, self::getPaypalConfigCountries())) { $pathParts[0] = $requestedCountrySection; } else { $pathParts[0] = 'payment_other'; } } - /** @var ElementInterface $result */ + $result = $proceed($pathParts); - if ($isSectionChanged && isset($result)) { + + if ($isSectionChanged && $result) { if ($result instanceof Section) { $this->restructurePayments($result); - $result->setData(array_merge( - $result->getData(), - ['showInDefault' => true, 'showInWebsite' => true, 'showInStore' => true] - ), $this->_scopeDefiner->getScope()); + $result->setData( + array_merge( + $result->getData(), + ['showInDefault' => true, 'showInWebsite' => true, 'showInStore' => true] + ), + $this->scopeDefiner->getScope() + ); } } + return $result; } /** - * Changes payment config structure. + * Change payment config structure + * * Groups which have `displayIn` element, transfer to appropriate group. * Groups without `displayIn` transfer to other payment methods group. * @@ -139,7 +148,7 @@ class StructurePlugin } $configuration['children'] = $sectionMap; - $result->setData($configuration, $this->_scopeDefiner->getScope()); + $result->setData($configuration, $this->scopeDefiner->getScope()); } /** diff --git a/app/code/Magento/Paypal/Model/Method/Checks/SpecificationPlugin.php b/app/code/Magento/Paypal/Model/Method/Checks/SpecificationPlugin.php index a171cd5628f0fb90dcb57ec521d5c47533f9ccc7..f2b650b95e9e8cdfaa86f2bf7e398f9fad74abb2 100644 --- a/app/code/Magento/Paypal/Model/Method/Checks/SpecificationPlugin.php +++ b/app/code/Magento/Paypal/Model/Method/Checks/SpecificationPlugin.php @@ -11,49 +11,54 @@ use Magento\Paypal\Model\Config; use Magento\Paypal\Model\Billing\AgreementFactory; use Magento\Quote\Model\Quote; +/** + * Plugin for \Magento\Payment\Model\Checks\Composite + */ class SpecificationPlugin { /** * @var AgreementFactory */ - protected $_agreementFactory; + private $agreementFactory; /** * @param AgreementFactory $agreementFactory */ public function __construct(AgreementFactory $agreementFactory) { - $this->_agreementFactory = $agreementFactory; + $this->agreementFactory = $agreementFactory; } /** * Override check for Billing Agreements * * @param SpecificationInterface $specification - * @param \Closure $proceed + * @param bool $result * @param MethodInterface $paymentMethod * @param Quote $quote * @return bool + * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundIsApplicable( + public function afterIsApplicable( SpecificationInterface $specification, - \Closure $proceed, + $result, MethodInterface $paymentMethod, Quote $quote ) { - $originallyIsApplicable = $proceed($paymentMethod, $quote); - if (!$originallyIsApplicable) { + if (!$result) { return false; } if ($paymentMethod->getCode() == Config::METHOD_BILLING_AGREEMENT) { if ($quote->getCustomerId()) { - $availableBA = $this->_agreementFactory->create()->getAvailableCustomerBillingAgreements( + $availableBA = $this->agreementFactory->create()->getAvailableCustomerBillingAgreements( $quote->getCustomerId() ); + return count($availableBA) > 0; } + return false; } diff --git a/app/code/Magento/Paypal/Test/Unit/Block/Adminhtml/Store/SwitcherPluginTest.php b/app/code/Magento/Paypal/Test/Unit/Block/Adminhtml/Store/SwitcherPluginTest.php index 320e89d567b9cac8155c45885671d20ed569ef99..a97fb0ad2a6fcc5723635b980571559e82742e70 100644 --- a/app/code/Magento/Paypal/Test/Unit/Block/Adminhtml/Store/SwitcherPluginTest.php +++ b/app/code/Magento/Paypal/Test/Unit/Block/Adminhtml/Store/SwitcherPluginTest.php @@ -5,42 +5,72 @@ */ namespace Magento\Paypal\Test\Unit\Block\Adminhtml\Store; +use Magento\Paypal\Block\Adminhtml\Store\SwitcherPlugin as StoreSwitcherBlockPlugin; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Magento\Backend\Block\Store\Switcher as StoreSwitcherBlock; +use Magento\Framework\App\RequestInterface; +use Magento\Paypal\Model\Config\StructurePlugin as ConfigStructurePlugin; + class SwitcherPluginTest extends \PHPUnit_Framework_TestCase { /** - * @var SwitcherPlugin + * @var StoreSwitcherBlockPlugin + */ + private $plugin; + + /** + * @var ObjectManagerHelper + */ + private $objectManagerHelper; + + /** + * @var StoreSwitcherBlock|\PHPUnit_Framework_MockObject_MockObject */ - protected $_model; + private $subjectMock; + + /** + * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $requestMock; protected function setUp() { - $this->_model = new \Magento\Paypal\Block\Adminhtml\Store\SwitcherPlugin(); + $this->subjectMock = $this->getMockBuilder(StoreSwitcherBlock::class) + ->disableOriginalConstructor() + ->getMock(); + $this->requestMock = $this->getMockBuilder(RequestInterface::class) + ->getMockForAbstractClass(); + + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->plugin = $this->objectManagerHelper->getObject(StoreSwitcherBlockPlugin::class); } /** - * @param null|string $countryParam + * @param string|null $countryParam * @param array $getUrlParams - * @dataProvider aroundGetUrlDataProvider + * + * @dataProvider beforeGetUrlDataProvider */ - public function testAroundGetUrl($countryParam, $getUrlParams) + public function testBeforeGetUrl($countryParam, $getUrlParams) { - $subjectRequest = $this->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class); - $subjectRequest->expects($this->once()) + $this->requestMock->expects(static::once()) ->method('getParam') - ->with(\Magento\Paypal\Model\Config\StructurePlugin::REQUEST_PARAM_COUNTRY) - ->will($this->returnValue($countryParam)); - $subject = $this->getMock(\Magento\Backend\Block\Store\Switcher::class, ['getRequest'], [], '', false); - $subject->expects($this->any())->method('getRequest')->will($this->returnValue($subjectRequest)); - $getUrl = function ($route, $params) { - return [$route, $params]; - }; - $this->assertEquals(['', $getUrlParams], $this->_model->aroundGetUrl($subject, $getUrl, '', [])); + ->with(ConfigStructurePlugin::REQUEST_PARAM_COUNTRY) + ->willReturn($countryParam); + $this->subjectMock->expects(static::any()) + ->method('getRequest') + ->willReturn($this->requestMock); + + $this->assertEquals(['', $getUrlParams], $this->plugin->beforeGetUrl($this->subjectMock, '', [])); } - public function aroundGetUrlDataProvider() + /** + * @return array + */ + public function beforeGetUrlDataProvider() { return [ - ['any value', [\Magento\Paypal\Model\Config\StructurePlugin::REQUEST_PARAM_COUNTRY => null]], + ['any value', [ConfigStructurePlugin::REQUEST_PARAM_COUNTRY => null]], [null, []] ]; } diff --git a/app/code/Magento/Paypal/Test/Unit/Model/Config/Structure/Element/FieldPluginTest.php b/app/code/Magento/Paypal/Test/Unit/Model/Config/Structure/Element/FieldPluginTest.php index e630e3c57207c113e502da623dd77f116de6bc53..48314c28ceb983b5ca660aa6e3e34345cc0d1cd3 100644 --- a/app/code/Magento/Paypal/Test/Unit/Model/Config/Structure/Element/FieldPluginTest.php +++ b/app/code/Magento/Paypal/Test/Unit/Model/Config/Structure/Element/FieldPluginTest.php @@ -3,80 +3,96 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\Paypal\Test\Unit\Model\Config\Structure\Element; +use Magento\Paypal\Model\Config\Structure\Element\FieldPlugin as FieldConfigStructurePlugin; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Magento\Framework\App\RequestInterface; +use Magento\Config\Model\Config\Structure\Element\Field as FieldConfigStructureMock; + class FieldPluginTest extends \PHPUnit_Framework_TestCase { - /** @var FieldPlugin */ - protected $model; + /** + * @var FieldConfigStructurePlugin + */ + private $plugin; - /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $request; + /** + * @var ObjectManagerHelper + */ + private $objectManagerHelper; + + /** + * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $requestMock; - /** @var \Magento\Config\Model\Config\Structure\Element\Field|\PHPUnit_Framework_MockObject_MockObject */ - protected $subject; + /** + * @var FieldConfigStructureMock|\PHPUnit_Framework_MockObject_MockObject + */ + private $subjectMock; protected function setUp() { - $this->request = $this->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class); - $this->subject = $this->getMock(\Magento\Config\Model\Config\Structure\Element\Field::class, [], [], '', false); + $this->requestMock = $this->getMockBuilder(RequestInterface::class) + ->getMockForAbstractClass(); + $this->subjectMock = $this->getMockBuilder(FieldConfigStructureMock::class) + ->disableOriginalConstructor() + ->getMock(); - $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->model = $helper->getObject( - \Magento\Paypal\Model\Config\Structure\Element\FieldPlugin::class, - ['request' => $this->request] + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->plugin = $this->objectManagerHelper->getObject( + FieldConfigStructurePlugin::class, + ['request' => $this->requestMock] ); } public function testAroundGetConfigPathHasResult() { $someResult = 'some result'; - $callback = function () use ($someResult) { - return $someResult; - }; - $this->assertEquals($someResult, $this->model->aroundGetConfigPath($this->subject, $callback)); + + $this->assertEquals($someResult, $this->plugin->afterGetConfigPath($this->subjectMock, $someResult)); } public function testAroundGetConfigPathNonPaymentSection() { - $callback = function () { - return null; - }; - $this->request->expects($this->once()) + $this->requestMock->expects(static::once()) ->method('getParam') ->with('section') - ->will($this->returnValue('non-payment')); - $this->assertNull($this->model->aroundGetConfigPath($this->subject, $callback)); + ->willReturn('non-payment'); + + $this->assertNull($this->plugin->afterGetConfigPath($this->subjectMock, null)); } /** * @param string $subjectPath * @param string $expectedConfigPath - * @dataProvider aroundGetConfigPathDataProvider + * + * @dataProvider afterGetConfigPathDataProvider */ public function testAroundGetConfigPath($subjectPath, $expectedConfigPath) { - $callback = function () { - return null; - }; - $this->request->expects($this->once()) + $this->requestMock->expects(static::once()) ->method('getParam') ->with('section') - ->will($this->returnValue('payment')); - $this->subject->expects($this->once()) + ->willReturn('payment'); + $this->subjectMock->expects(static::once()) ->method('getPath') - ->will($this->returnValue($subjectPath)); - $this->assertEquals($expectedConfigPath, $this->model->aroundGetConfigPath($this->subject, $callback)); + ->willReturn($subjectPath); + + $this->assertEquals($expectedConfigPath, $this->plugin->afterGetConfigPath($this->subjectMock, null)); } - public function aroundGetConfigPathDataProvider() + /** + * @return array + */ + public function afterGetConfigPathDataProvider() { return [ ['payment_us/group/field', 'payment/group/field'], ['payment_other/group/field', 'payment/group/field'], ['payment_us', 'payment_us'], - ['payment_wrong_country/group/field', 'payment_wrong_country/group/field'], + ['payment_wrong_country/group/field', 'payment_wrong_country/group/field'] ]; } } diff --git a/app/code/Magento/Paypal/Test/Unit/Model/Config/StructurePluginTest.php b/app/code/Magento/Paypal/Test/Unit/Model/Config/StructurePluginTest.php index 4479f0a1233515039e499f73cc4595c5369007f7..63abcd660425abd7173fbd2528e047498c7cce62 100644 --- a/app/code/Magento/Paypal/Test/Unit/Model/Config/StructurePluginTest.php +++ b/app/code/Magento/Paypal/Test/Unit/Model/Config/StructurePluginTest.php @@ -3,45 +3,80 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\Paypal\Test\Unit\Model\Config; -use Magento\Paypal\Model\Config\StructurePlugin; +use Magento\Paypal\Model\Config\StructurePlugin as ConfigStructurePlugin; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Magento\Config\Model\Config\ScopeDefiner as ConfigScopeDefiner; +use Magento\Paypal\Helper\Backend as BackendHelper; +use Magento\Config\Model\Config\Structure as ConfigStructure; +use Magento\Config\Model\Config\Structure\ElementInterface as ElementConfigStructure; class StructurePluginTest extends \PHPUnit_Framework_TestCase { - /** @var \Magento\Paypal\Model\Config\StructurePlugin */ - protected $_model; + /** + * @var ConfigStructurePlugin + */ + private $plugin; + + /** + * @var ObjectManagerHelper + */ + private $objectManagerHelper; + + /** + * @var ConfigScopeDefiner|\PHPUnit_Framework_MockObject_MockObject + */ + private $configScopeDefinerMock; + + /** + * @var BackendHelper|\PHPUnit_Framework_MockObject_MockObject + */ + private $backendHelperMock; - /** @var \Magento\Config\Model\Config\ScopeDefiner|\PHPUnit_Framework_MockObject_MockObject */ - protected $_scopeDefiner; + /** + * @var ConfigStructure|\PHPUnit_Framework_MockObject_MockObject + */ + private $configStructureMock; - /** @var \Magento\Paypal\Helper\Backend|\PHPUnit_Framework_MockObject_MockObject */ - protected $_helper; + /** + * @var ElementConfigStructure|\PHPUnit_Framework_MockObject_MockObject + */ + private $elementConfigStructureMock; protected function setUp() { - $this->_scopeDefiner = $this->getMock(\Magento\Config\Model\Config\ScopeDefiner::class, [], [], '', false); - $this->_helper = $this->getMock(\Magento\Paypal\Helper\Backend::class, [], [], '', false); - - $objectManagerHelper = new ObjectManagerHelper($this); - $this->_model = $objectManagerHelper->getObject( - \Magento\Paypal\Model\Config\StructurePlugin::class, - ['scopeDefiner' => $this->_scopeDefiner, 'helper' => $this->_helper] + $this->configScopeDefinerMock = $this->getMockBuilder(ConfigScopeDefiner::class) + ->disableOriginalConstructor() + ->getMock(); + $this->backendHelperMock = $this->getMockBuilder(BackendHelper::class) + ->disableOriginalConstructor() + ->getMock(); + $this->configStructureMock = $this->getMockBuilder(ConfigStructure::class) + ->disableOriginalConstructor() + ->getMock(); + $this->elementConfigStructureMock = $this->getMockBuilder(ElementConfigStructure::class) + ->getMockForAbstractClass(); + + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->plugin = $this->objectManagerHelper->getObject( + ConfigStructurePlugin::class, + ['scopeDefiner' => $this->configScopeDefinerMock, 'helper' => $this->backendHelperMock] ); } public function testGetPaypalConfigCountriesWithOther() { - $countries = StructurePlugin::getPaypalConfigCountries(true); + $countries = ConfigStructurePlugin::getPaypalConfigCountries(true); + $this->assertContains('payment_us', $countries); $this->assertContains('payment_other', $countries); } public function testGetPaypalConfigCountries() { - $countries = StructurePlugin::getPaypalConfigCountries(false); + $countries = ConfigStructurePlugin::getPaypalConfigCountries(false); + $this->assertContains('payment_us', $countries); $this->assertNotContains('payment_other', $countries); } @@ -49,20 +84,25 @@ class StructurePluginTest extends \PHPUnit_Framework_TestCase /** * @param array $pathParts * @param bool $returnResult + * * @dataProvider aroundGetElementByPathPartsNonPaymentDataProvider */ public function testAroundGetElementByPathPartsNonPayment($pathParts, $returnResult) { - $result = $returnResult - ? $this->getMockForAbstractClass(\Magento\Config\Model\Config\Structure\ElementInterface::class) - : null; - $this->_aroundGetElementByPathPartsAssertResult( + $result = $returnResult ? $this->elementConfigStructureMock : null; + $proceed = function () use ($result) { + return $result; + }; + + $this->assertSame( $result, - $this->_getElementByPathPartsCallback($pathParts, $result), - $pathParts + $this->plugin->aroundGetElementByPathParts($this->configStructureMock, $proceed, $pathParts) ); } + /** + * @return array + */ public function aroundGetElementByPathPartsNonPaymentDataProvider() { return [ @@ -76,146 +116,62 @@ class StructurePluginTest extends \PHPUnit_Framework_TestCase /** * @param array $pathParts * @param string $countryCode - * @param array $expectedPathParts + * * @dataProvider aroundGetElementByPathPartsDataProvider */ - public function testAroundGetElementByPathPartsNoResult($pathParts, $countryCode, $expectedPathParts) + public function testAroundGetElementByPathPartsNoResult($pathParts, $countryCode) { - $this->_getElementByPathPartsPrepareHelper($countryCode); - $this->_aroundGetElementByPathPartsAssertResult( + $proceed = function () { + return null; + }; + + $this->backendHelperMock->expects(static::once()) + ->method('getConfigurationCountryCode') + ->willReturn($countryCode); + + $this->assertEquals( null, - $this->_getElementByPathPartsCallback($expectedPathParts, null), - $pathParts + $this->plugin->aroundGetElementByPathParts($this->configStructureMock, $proceed, $pathParts) ); } /** * @param array $pathParts * @param string $countryCode - * @param array $expectedPathParts + * * @dataProvider aroundGetElementByPathPartsDataProvider */ - public function testAroundGetElementByPathParts($pathParts, $countryCode, $expectedPathParts) + public function testAroundGetElementByPathParts($pathParts, $countryCode) { - $this->_getElementByPathPartsPrepareHelper($countryCode); - $result = $this->getMockForAbstractClass(\Magento\Config\Model\Config\Structure\ElementInterface::class); - $this->_aroundGetElementByPathPartsAssertResult( - $result, - $this->_getElementByPathPartsCallback($expectedPathParts, $result), - $pathParts + $result = $this->elementConfigStructureMock; + $proceed = function () use ($result) { + return $result; + }; + + $this->backendHelperMock->expects(static::once()) + ->method('getConfigurationCountryCode') + ->willReturn($countryCode); + + $this->assertSame( + $this->elementConfigStructureMock, + $this->plugin->aroundGetElementByPathParts($this->configStructureMock, $proceed, $pathParts) ); } + /** + * @return array + */ public function aroundGetElementByPathPartsDataProvider() { return [ [ ['payment', 'group1', 'group2', 'field'], 'any', - ['payment_other', 'group1', 'group2', 'field'], ], [ ['payment', 'group1', 'group2', 'field'], 'DE', - ['payment_de', 'group1', 'group2', 'field'] - ], + ] ]; } - - /** - * @param array $pathParts - * @param string $countryCode - * @param array $expectedPathParts - * @dataProvider aroundGetSectionByPathPartsDataProvider - */ - public function testAroundGetSectionByPathParts($pathParts, $countryCode, $expectedPathParts) - { - $this->_getElementByPathPartsPrepareHelper($countryCode); - $result = $this->getMock(\Magento\Config\Model\Config\Structure\Element\Section::class, [], [], '', false); - $self = $this; - $getElementByPathParts = function ($pathParts) use ($self, $expectedPathParts, $result) { - $self->assertEquals($expectedPathParts, $pathParts); - $scope = 'any scope'; - $sectionMap = [ - 'account' => [], - 'recommended_solutions' => [], - 'other_paypal_payment_solutions' => [], - 'other_payment_methods' => [] - ]; - $self->_scopeDefiner->expects($self->any()) - ->method('getScope') - ->will($self->returnValue($scope)); - $result->expects($self->at(0)) - ->method('getData') - ->will($self->returnValue(['children' => []])); - $result->expects($self->at(2)) - ->method('getData') - ->will($self->returnValue(['children' => $sectionMap])); - $result->expects($self->at(1)) - ->method('setData') - ->with(['children' => $sectionMap], $scope) - ->will($self->returnSelf()); - $result->expects($self->at(3)) - ->method('setData') - ->with(['children' => $sectionMap, - 'showInDefault' => true, - 'showInWebsite' => true, - 'showInStore' => true], $scope) - ->will($self->returnSelf()); - return $result; - }; - $this->_aroundGetElementByPathPartsAssertResult($result, $getElementByPathParts, $pathParts); - } - - public function aroundGetSectionByPathPartsDataProvider() - { - return [ - [['payment'], 'GB', ['payment_gb']], - [['payment'], 'any', ['payment_other']], - ]; - } - - /** - * Assert result of aroundGetElementByPathParts method - * - * @param \PHPUnit_Framework_MockObject_MockObject|null $result - * @param \Closure $getElementByPathParts - * @param array $pathParts - */ - private function _aroundGetElementByPathPartsAssertResult($result, $getElementByPathParts, $pathParts) - { - $this->assertEquals($result, $this->_model->aroundGetElementByPathParts( - $this->getMock(\Magento\Config\Model\Config\Structure::class, [], [], '', false), - $getElementByPathParts, - $pathParts - )); - } - - /** - * Get callback for aroundGetElementByPathParts method - * - * @param array $expectedPathParts - * @param \PHPUnit_Framework_MockObject_MockObject|null $result - * @return \Closure - */ - private function _getElementByPathPartsCallback($expectedPathParts, $result) - { - $self = $this; - return function ($pathParts) use ($self, $expectedPathParts, $result) { - $self->assertEquals($expectedPathParts, $pathParts); - return $result; - }; - } - - /** - * Prepare helper for test - * - * @param string $countryCode - */ - private function _getElementByPathPartsPrepareHelper($countryCode) - { - $this->_helper->expects($this->once()) - ->method('getConfigurationCountryCode') - ->will($this->returnValue($countryCode)); - } } diff --git a/app/code/Magento/Paypal/Test/Unit/Model/Method/Checks/SpecificationPluginTest.php b/app/code/Magento/Paypal/Test/Unit/Model/Method/Checks/SpecificationPluginTest.php index eb8eb1091b083d70d0ee7b1c69d26da87525d113..0994088d84a259f3596065e1ffdc9bb423110b52 100644 --- a/app/code/Magento/Paypal/Test/Unit/Model/Method/Checks/SpecificationPluginTest.php +++ b/app/code/Magento/Paypal/Test/Unit/Model/Method/Checks/SpecificationPluginTest.php @@ -3,155 +3,179 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\Paypal\Test\Unit\Model\Method\Checks; +use Magento\Paypal\Model\Method\Checks\SpecificationPlugin; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Magento\Paypal\Model\Billing\AgreementFactory as BillingAgreementFactory; +use Magento\Payment\Model\Checks\SpecificationInterface; use Magento\Payment\Model\MethodInterface; -use Magento\Paypal\Model\Billing\AgreementFactory; use Magento\Quote\Model\Quote; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Magento\Paypal\Model\ResourceModel\Billing\Agreement\Collection as BillingAgreementCollection; +use Magento\Paypal\Model\Billing\Agreement as BillingAgreement; class SpecificationPluginTest extends \PHPUnit_Framework_TestCase { - /** @var SpecificationPlugin */ - protected $model; + /** + * @var SpecificationPlugin + */ + private $plugin; + + /** + * @var ObjectManagerHelper + */ + private $objectManagerHelper; - /** @var AgreementFactory|\PHPUnit_Framework_MockObject_MockObject */ - protected $agreementFactory; + /** + * @var BillingAgreementFactory|\PHPUnit_Framework_MockObject_MockObject + */ + private $billingAgreementFactoryMock; + + /** + * @var SpecificationInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $specificationMock; + + /** + * @var MethodInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $paymentMethodMock; + + /** + * @var Quote|\PHPUnit_Framework_MockObject_MockObject + */ + private $quoteMock; + + /** + * @var BillingAgreementCollection|\PHPUnit_Framework_MockObject_MockObject + */ + private $billingAgreementCollectionMock; + + /** + * @var BillingAgreement|\PHPUnit_Framework_MockObject_MockObject + */ + private $billingAgreementMock; protected function setUp() { - $this->agreementFactory = $this->getMockBuilder(\Magento\Paypal\Model\Billing\AgreementFactory::class) + $this->billingAgreementFactoryMock = $this->getMockBuilder(BillingAgreementFactory::class) ->disableOriginalConstructor() ->setMethods(['create']) ->getMock(); + $this->specificationMock = $this->getMockBuilder(SpecificationInterface::class) + ->getMockForAbstractClass(); + $this->paymentMethodMock = $this->getMockBuilder(MethodInterface::class) + ->getMockForAbstractClass(); + $this->quoteMock = $this->getMockBuilder(Quote::class) + ->disableOriginalConstructor() + ->setMethods(['getCustomerId']) + ->getMock(); + $this->billingAgreementCollectionMock = $this->getMockBuilder(BillingAgreementCollection::class) + ->disableOriginalConstructor() + ->getMock(); + $this->billingAgreementMock = $this->getMockBuilder(BillingAgreement::class) + ->disableOriginalConstructor() + ->getMock(); - $objectManagerHelper = new ObjectManagerHelper($this); - $this->model = $objectManagerHelper->getObject( - \Magento\Paypal\Model\Method\Checks\SpecificationPlugin::class, + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->plugin = $this->objectManagerHelper->getObject( + SpecificationPlugin::class, [ - 'agreementFactory' => $this->agreementFactory + 'agreementFactory' => $this->billingAgreementFactoryMock ] ); } - public function testAroundIsApplicableNotOriginallyApplicable() + public function testAfterIsApplicableNotOriginallyApplicable() { - $paymentMethod = $this->getPaymentMethod('any'); - $quote = $this->getQuote('any'); - $proceed = $this->getProceedClosure(false, $paymentMethod, $quote); - $this->assertFalse($this->callAroundIsApplicable($proceed, $paymentMethod, $quote)); + $this->setExpectations('any', 'any'); + + $this->assertFalse( + $this->plugin->afterIsApplicable( + $this->specificationMock, + false, + $this->paymentMethodMock, + $this->quoteMock + ) + ); } - public function testAroundIsApplicableNotAgreement() + public function testAfterIsApplicableNotAgreement() { - $paymentMethod = $this->getPaymentMethod('not_agreement'); - $quote = $this->getQuote('any'); - $proceed = $this->getProceedClosure(true, $paymentMethod, $quote); - $this->assertTrue($this->callAroundIsApplicable($proceed, $paymentMethod, $quote)); + $this->setExpectations('not_agreement', 'any'); + + $this->assertTrue( + $this->plugin->afterIsApplicable( + $this->specificationMock, + true, + $this->paymentMethodMock, + $this->quoteMock + ) + ); } - public function testAroundIsApplicableNoCustomerId() + public function testAfterIsApplicableNoCustomerId() { - $paymentMethod = $this->getPaymentMethod('paypal_billing_agreement'); - $quote = $this->getQuote(null); - $proceed = $this->getProceedClosure(true, $paymentMethod, $quote); - $this->assertFalse($this->callAroundIsApplicable($proceed, $paymentMethod, $quote)); + $this->setExpectations('paypal_billing_agreement', null); + + $this->assertFalse( + $this->plugin->afterIsApplicable( + $this->specificationMock, + true, + $this->paymentMethodMock, + $this->quoteMock + ) + ); } /** * @param int $count - * @dataProvider aroundIsApplicableDataProvider + * + * @dataProvider afterIsApplicableDataProvider */ - public function testAroundIsApplicable($count) + public function testAfterIsApplicable($count) { - $paymentMethod = $this->getPaymentMethod('paypal_billing_agreement'); - $quote = $this->getQuote(1); - $proceed = $this->getProceedClosure(true, $paymentMethod, $quote); - $agreementCollection = $this->getMock( - \Magento\Paypal\Model\ResourceModel\Billing\Agreement\Collection::class, - [], - [], - '', - false - ); - $agreementCollection->expects($this->once()) - ->method('count') - ->will($this->returnValue($count)); - $agreement = $this->getMock(\Magento\Paypal\Model\Billing\Agreement::class, [], [], '', false); - $agreement->expects($this->once()) + $this->setExpectations('paypal_billing_agreement', 1); + + $this->billingAgreementFactoryMock->expects(static::once()) + ->method('create') + ->willReturn($this->billingAgreementMock); + $this->billingAgreementMock->expects(static::once()) ->method('getAvailableCustomerBillingAgreements') ->with(1) - ->will($this->returnValue($agreementCollection)); - $this->agreementFactory->expects($this->once()) - ->method('create') - ->will($this->returnValue($agreement)); - $this->assertEquals($count > 0, $this->callAroundIsApplicable($proceed, $paymentMethod, $quote)); - } - - public function aroundIsApplicableDataProvider() - { - return [[0], [1], [2]]; - } + ->willReturn($this->billingAgreementCollectionMock); + $this->billingAgreementCollectionMock->expects(static::once()) + ->method('count') + ->willReturn($count); - /** - * @param bool $result - * @param MethodInterface $paymentMethod - * @param Quote $quote - * @return \Closure - */ - private function getProceedClosure($result, MethodInterface $paymentMethod, Quote $quote) - { - $self = $this; - return function ($parameter1, $parameter2) use ($result, $paymentMethod, $quote, $self) { - $self->assertSame($paymentMethod, $parameter1); - $self->assertSame($quote, $parameter2); - return $result; - }; + $this->assertEquals( + $count > 0, + $this->plugin->afterIsApplicable($this->specificationMock, true, $this->paymentMethodMock, $this->quoteMock) + ); } /** - * Get payment method parameter - * - * @param string $code - * @return MethodInterface|\PHPUnit_Framework_MockObject_MockObject + * @return array */ - private function getPaymentMethod($code) + public function afterIsApplicableDataProvider() { - $paymentMethod = $this->getMockForAbstractClass(\Magento\Payment\Model\MethodInterface::class); - $paymentMethod->expects($this->any()) - ->method('getCode') - ->will($this->returnValue($code)); - return $paymentMethod; + return [[0], [1], [2]]; } /** - * Get quote parameter + * Set expectations * + * @param mixed $paymentMethodCode * @param mixed $customerId - * @return Quote|\PHPUnit_Framework_MockObject_MockObject + * @return void */ - private function getQuote($customerId) + private function setExpectations($paymentMethodCode, $customerId) { - $quote = $this->getMock(\Magento\Quote\Model\Quote::class, ['__wakeup'], [], '', false); - $quote->setCustomerId($customerId); - return $quote; - } - - /** - * Call aroundIsApplicable method - * - * @param \Closure $proceed - * @param MethodInterface $paymentMethod - * @param Quote $quote - * @return bool - */ - private function callAroundIsApplicable( - \Closure $proceed, - MethodInterface $paymentMethod, - Quote $quote - ) { - $specification = $this->getMockForAbstractClass(\Magento\Payment\Model\Checks\SpecificationInterface::class); - return $this->model->aroundIsApplicable($specification, $proceed, $paymentMethod, $quote); + $this->paymentMethodMock->expects(static::any()) + ->method('getCode') + ->willReturn($paymentMethodCode); + $this->quoteMock->expects(static::any()) + ->method('getCustomerId') + ->willReturn($customerId); } } diff --git a/app/code/Magento/Sales/Model/ResourceModel/Order/Plugin/Authorization.php b/app/code/Magento/Sales/Model/ResourceModel/Order/Plugin/Authorization.php index a44a7b78717975a0945ab23a7976c1fc009e371e..f6b459ba4638d0d5db59fe8fb06d3054f5299ec3 100644 --- a/app/code/Magento/Sales/Model/ResourceModel/Order/Plugin/Authorization.php +++ b/app/code/Magento/Sales/Model/ResourceModel/Order/Plugin/Authorization.php @@ -8,6 +8,8 @@ namespace Magento\Sales\Model\ResourceModel\Order\Plugin; use Magento\Authorization\Model\UserContextInterface; use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Sales\Model\Order; +use Magento\Sales\Model\ResourceModel\Order as ResourceOrder; class Authorization { @@ -20,33 +22,28 @@ class Authorization * @param UserContextInterface $userContext */ public function __construct( - \Magento\Authorization\Model\UserContextInterface $userContext + UserContextInterface $userContext ) { $this->userContext = $userContext; } /** - * Checks if order is allowed - * - * @param \Magento\Sales\Model\ResourceModel\Order $subject - * @param callable $proceed + * @param ResourceOrder $subject + * @param ResourceOrder $result * @param \Magento\Framework\Model\AbstractModel $order - * @param mixed $value - * @param null|string $field - * @return \Magento\Sales\Model\Order + * @return ResourceOrder * @throws NoSuchEntityException * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundLoad( - \Magento\Sales\Model\ResourceModel\Order $subject, - \Closure $proceed, - \Magento\Framework\Model\AbstractModel $order, - $value, - $field = null + public function afterLoad( + ResourceOrder $subject, + ResourceOrder $result, + \Magento\Framework\Model\AbstractModel $order ) { - $result = $proceed($order, $value, $field); - if (!$this->isAllowed($order)) { - throw NoSuchEntityException::singleField('orderId', $order->getId()); + if ($order instanceof Order) { + if (!$this->isAllowed($order)) { + throw NoSuchEntityException::singleField('orderId', $order->getId()); + } } return $result; } @@ -57,7 +54,7 @@ class Authorization * @param \Magento\Sales\Model\Order $order * @return bool */ - protected function isAllowed(\Magento\Sales\Model\Order $order) + protected function isAllowed(Order $order) { return $this->userContext->getUserType() == UserContextInterface::USER_TYPE_CUSTOMER ? $order->getCustomerId() == $this->userContext->getUserId() diff --git a/app/code/Magento/Sales/Test/Unit/Model/ResourceModel/Order/Plugin/AuthorizationTest.php b/app/code/Magento/Sales/Test/Unit/Model/ResourceModel/Order/Plugin/AuthorizationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..eeee0b2b910ea3d4d793952471fa1958de618f30 --- /dev/null +++ b/app/code/Magento/Sales/Test/Unit/Model/ResourceModel/Order/Plugin/AuthorizationTest.php @@ -0,0 +1,91 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Sales\Test\Unit\Model\ResourceModel\Order\Plugin; + +use Magento\Authorization\Model\UserContextInterface; +use Magento\Sales\Model\ResourceModel\Order as ResourceOrder; +use Magento\Sales\Model\Order; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\Sales\Model\ResourceModel\Order\Plugin\Authorization; + +class AuthorizationTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var ObjectManager + */ + private $objectManager; + + /** + * @var UserContextInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $userContextMock; + + /** + * @var ResourceOrder|\PHPUnit_Framework_MockObject_MockObject + */ + private $subjectMock; + + /** + * @var Order|\PHPUnit_Framework_MockObject_MockObject + */ + private $orderMock; + + /** + * @var Authorization + */ + private $plugin; + + protected function setUp() + { + $this->objectManager = new ObjectManager($this); + $this->userContextMock = $this->getMockBuilder(UserContextInterface::class) + ->setMethods(['getUserType', 'getUserId']) + ->getMockForAbstractClass(); + $this->subjectMock = $this->getMockBuilder(ResourceOrder::class) + ->disableOriginalConstructor() + ->getMock(); + $this->orderMock = $this->getMockBuilder(Order::class) + ->disableOriginalConstructor() + ->setMethods(['getCustomerId', 'getId']) + ->getMock(); + $this->plugin = $this->objectManager->getObject( + Authorization::class, + ['userContext' => $this->userContextMock] + ); + } + + public function testAfterLoad() + { + $this->userContextMock->expects($this->once()) + ->method('getUserType') + ->willReturn('testType'); + $this->assertEquals( + $this->subjectMock, + $this->plugin->afterLoad($this->subjectMock, $this->subjectMock, $this->orderMock) + ); + } + + /** + * @expectedException \Magento\Framework\Exception\NoSuchEntityException + * @expectedExceptionMessage No such entity with orderId = 1 + */ + public function testAfterLoadWithException() + { + $this->userContextMock->expects($this->once()) + ->method('getUserType') + ->willReturn(UserContextInterface::USER_TYPE_CUSTOMER); + $this->orderMock->expects($this->once()) + ->method('getCustomerId') + ->willReturn(1); + $this->userContextMock->expects($this->once()) + ->method('getUserId') + ->willReturn(2); + $this->orderMock->expects($this->once()) + ->method('getId') + ->willReturn(1); + $this->plugin->afterLoad($this->subjectMock, $this->subjectMock, $this->orderMock); + } +} diff --git a/app/code/Magento/Store/App/Action/Plugin/Context.php b/app/code/Magento/Store/App/Action/Plugin/Context.php index e56b301733cca65b6cb7cfdfce4a8b870f75ee47..1b4545384255c3154212ce84cc81c1a83fa6dcbb 100644 --- a/app/code/Magento/Store/App/Action/Plugin/Context.php +++ b/app/code/Magento/Store/App/Action/Plugin/Context.php @@ -11,6 +11,8 @@ use Magento\Framework\Phrase; use Magento\Store\Api\StoreCookieManagerInterface; use Magento\Store\Api\StoreResolverInterface; use Magento\Store\Model\StoreManagerInterface; +use Magento\Framework\App\Action\AbstractAction; +use Magento\Framework\App\RequestInterface; /** * Class ContextPlugin @@ -27,11 +29,6 @@ class Context */ protected $httpContext; - /** - * @var \Magento\Framework\App\Request\Http - */ - protected $httpRequest; - /** * @var \Magento\Store\Model\StoreManagerInterface */ @@ -45,40 +42,35 @@ class Context /** * @param \Magento\Framework\Session\SessionManagerInterface $session * @param \Magento\Framework\App\Http\Context $httpContext - * @param \Magento\Framework\App\Request\Http $httpRequest * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param StoreCookieManagerInterface $storeCookieManager */ public function __construct( \Magento\Framework\Session\SessionManagerInterface $session, \Magento\Framework\App\Http\Context $httpContext, - \Magento\Framework\App\Request\Http $httpRequest, \Magento\Store\Model\StoreManagerInterface $storeManager, StoreCookieManagerInterface $storeCookieManager ) { $this->session = $session; $this->httpContext = $httpContext; - $this->httpRequest = $httpRequest; $this->storeManager = $storeManager; $this->storeCookieManager = $storeCookieManager; } /** - * @param \Magento\Framework\App\ActionInterface $subject - * @param callable $proceed - * @param \Magento\Framework\App\RequestInterface $request - * @return mixed + * Set store and currency to http context + * + * @param AbstractAction $subject + * @param RequestInterface $request + * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundDispatch( - \Magento\Framework\App\ActionInterface $subject, - \Closure $proceed, - \Magento\Framework\App\RequestInterface $request - ) { + public function beforeDispatch(AbstractAction $subject, RequestInterface $request) + { /** @var \Magento\Store\Model\Store $defaultStore */ $defaultStore = $this->storeManager->getWebsite()->getDefaultStore(); - $storeCode = $this->httpRequest->getParam( + $storeCode = $request->getParam( StoreResolverInterface::PARAM_NAME, $this->storeCookieManager->getStoreCodeFromCookie() ); @@ -103,6 +95,5 @@ class Context $this->session->getCurrencyCode() ?: $currentStore->getDefaultCurrencyCode(), $defaultStore->getDefaultCurrencyCode() ); - return $proceed($request); } } diff --git a/app/code/Magento/Store/App/Action/Plugin/StoreCheck.php b/app/code/Magento/Store/App/Action/Plugin/StoreCheck.php index 56a7157e7004c2b9ec87460b45dcf68d082557b5..fcb9316220793669b93630eb36b879de7be9a426 100644 --- a/app/code/Magento/Store/App/Action/Plugin/StoreCheck.php +++ b/app/code/Magento/Store/App/Action/Plugin/StoreCheck.php @@ -23,17 +23,14 @@ class StoreCheck } /** - * @param \Magento\Framework\App\ActionInterface $subject - * @param callable $proceed + * @param \Magento\Framework\App\Action\AbstractAction $subject * @param \Magento\Framework\App\RequestInterface $request - * - * @return \Magento\Framework\App\ResponseInterface + * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @throws \Magento\Framework\Exception\State\InitException */ - public function aroundDispatch( - \Magento\Framework\App\ActionInterface $subject, - \Closure $proceed, + public function beforeDispatch( + \Magento\Framework\App\Action\AbstractAction $subject, \Magento\Framework\App\RequestInterface $request ) { if (!$this->_storeManager->getStore()->isActive()) { @@ -41,6 +38,5 @@ class StoreCheck __('Current store is not active.') ); } - return $proceed($request); } } diff --git a/app/code/Magento/Store/Test/Unit/App/Action/Plugin/ContextTest.php b/app/code/Magento/Store/Test/Unit/App/Action/Plugin/ContextTest.php index 32aeb0b515124f1d552624d2a7a09b2ad520a85b..2411475b203818c28af71de117172236a5b321bf 100644 --- a/app/code/Magento/Store/Test/Unit/App/Action/Plugin/ContextTest.php +++ b/app/code/Magento/Store/Test/Unit/App/Action/Plugin/ContextTest.php @@ -3,14 +3,13 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - -// @codingStandardsIgnoreFile - namespace Magento\Store\Test\Unit\App\Action\Plugin; use Magento\Framework\App\Http\Context; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Store\Model\StoreManagerInterface; +use Magento\Framework\App\Action\AbstractAction; +use Magento\Framework\App\RequestInterface; /** * Class ContextPluginTest @@ -38,11 +37,6 @@ class ContextTest extends \PHPUnit_Framework_TestCase */ protected $httpContextMock; - /** - * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject - */ - protected $httpRequestMock; - /** * @var \Magento\Store\Model\StoreManager|\PHPUnit_Framework_MockObject_MockObject */ @@ -69,17 +63,12 @@ class ContextTest extends \PHPUnit_Framework_TestCase protected $websiteMock; /** - * @var \Closure - */ - protected $closureMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var AbstractAction|\PHPUnit_Framework_MockObject_MockObject */ protected $subjectMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $requestMock; @@ -88,67 +77,53 @@ class ContextTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { - $this->sessionMock = $this->getMock( + $this->sessionMock = $this->getMock( \Magento\Framework\Session\Generic::class, ['getCurrencyCode'], [], '', false ); - $this->httpContextMock = $this->getMock( + $this->httpContextMock = $this->getMock( \Magento\Framework\App\Http\Context::class, [], [], '', false ); - $this->httpRequestMock = $this->getMock( - \Magento\Framework\App\Request\Http::class, - ['getParam'], - [], - '', - false - ); $this->storeManager = $this->getMock(\Magento\Store\Model\StoreManagerInterface::class); $this->storeCookieManager = $this->getMock(\Magento\Store\Api\StoreCookieManagerInterface::class); - $this->storeMock = $this->getMock( + $this->storeMock = $this->getMock( \Magento\Store\Model\Store::class, [], [], '', false ); - $this->currentStoreMock = $this->getMock( + $this->currentStoreMock = $this->getMock( \Magento\Store\Model\Store::class, [], [], '', false ); - $this->websiteMock = $this->getMock( + $this->websiteMock = $this->getMock( \Magento\Store\Model\Website::class, ['getDefaultStore', '__wakeup'], [], '', false ); - $this->closureMock = function () { - return 'ExpectedValue'; - }; - $this->subjectMock = $this->getMock( - \Magento\Framework\App\Action\Action::class, - [], - [], - '', - false - ); - $this->requestMock = $this->getMock(\Magento\Framework\App\RequestInterface::class); + $this->requestMock = $this->getMockBuilder(RequestInterface::class)->getMockForAbstractClass(); + $this->subjectMock = $this->getMockBuilder(AbstractAction::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); - $this->plugin = (new ObjectManager($this))->getObject(\Magento\Store\App\Action\Plugin\Context::class, + $this->plugin = (new ObjectManager($this))->getObject( + \Magento\Store\App\Action\Plugin\Context::class, [ 'session' => $this->sessionMock, 'httpContext' => $this->httpContextMock, - 'httpRequest' => $this->httpRequestMock, 'storeManager' => $this->storeManager, 'storeCookieManager' => $this->storeCookieManager, ] @@ -171,7 +146,7 @@ class ContextTest extends \PHPUnit_Framework_TestCase ->will($this->returnValue(self::CURRENCY_CURRENT_STORE)); } - public function testAroundDispatchCurrencyFromSession() + public function testBeforeDispatchCurrencyFromSession() { $this->storeMock->expects($this->once()) ->method('getDefaultCurrencyCode') @@ -184,7 +159,7 @@ class ContextTest extends \PHPUnit_Framework_TestCase ->method('getCode') ->willReturn('custom_store'); - $this->httpRequestMock->expects($this->once()) + $this->requestMock->expects($this->once()) ->method('getParam') ->with($this->equalTo('___store')) ->will($this->returnValue('default')); @@ -205,10 +180,7 @@ class ContextTest extends \PHPUnit_Framework_TestCase ->method('setValue') ->with(Context::CONTEXT_CURRENCY, self::CURRENCY_SESSION, self::CURRENCY_DEFAULT); - $this->assertEquals( - 'ExpectedValue', - $this->plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock) - ); + $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock); } public function testDispatchCurrentStoreCurrency() @@ -224,7 +196,7 @@ class ContextTest extends \PHPUnit_Framework_TestCase ->method('getCode') ->willReturn('custom_store'); - $this->httpRequestMock->expects($this->once()) + $this->requestMock->expects($this->once()) ->method('getParam') ->with($this->equalTo('___store')) ->will($this->returnValue('default')); @@ -241,10 +213,7 @@ class ContextTest extends \PHPUnit_Framework_TestCase ->method('setValue') ->with(Context::CONTEXT_CURRENCY, self::CURRENCY_CURRENT_STORE, self::CURRENCY_DEFAULT); - $this->assertEquals( - 'ExpectedValue', - $this->plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock) - ); + $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock); } public function testDispatchStoreParameterIsArray() @@ -266,7 +235,7 @@ class ContextTest extends \PHPUnit_Framework_TestCase ] ]; - $this->httpRequestMock->expects($this->once()) + $this->requestMock->expects($this->once()) ->method('getParam') ->with($this->equalTo('___store')) ->will($this->returnValue($store)); @@ -284,11 +253,7 @@ class ContextTest extends \PHPUnit_Framework_TestCase ->method('setValue') ->with(Context::CONTEXT_CURRENCY, self::CURRENCY_CURRENT_STORE, self::CURRENCY_DEFAULT); - $result = $this->plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock); - $this->assertEquals( - 'ExpectedValue', - $result - ); + $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock); } /** @@ -314,10 +279,10 @@ class ContextTest extends \PHPUnit_Framework_TestCase ] ]; - $this->httpRequestMock->expects($this->once()) + $this->requestMock->expects($this->once()) ->method('getParam') ->with($this->equalTo('___store')) ->will($this->returnValue($store)); - $this->plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock); + $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock); } } diff --git a/app/code/Magento/Store/Test/Unit/App/Action/Plugin/StoreCheckTest.php b/app/code/Magento/Store/Test/Unit/App/Action/Plugin/StoreCheckTest.php index 1170377a6b27c8abf96648c5491ef68d6b35fa5e..7e443a05c1b25b2edfcce4499036d65010c215bf 100644 --- a/app/code/Magento/Store/Test/Unit/App/Action/Plugin/StoreCheckTest.php +++ b/app/code/Magento/Store/Test/Unit/App/Action/Plugin/StoreCheckTest.php @@ -3,9 +3,6 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - -// @codingStandardsIgnoreFile - namespace Magento\Store\Test\Unit\App\Action\Plugin; class StoreCheckTest extends \PHPUnit_Framework_TestCase @@ -26,17 +23,12 @@ class StoreCheckTest extends \PHPUnit_Framework_TestCase protected $_storeMock; /** - * @var \Closure - */ - protected $closureMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\App\Action\AbstractAction|\PHPUnit_Framework_MockObject_MockObject */ protected $subjectMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $requestMock; @@ -51,11 +43,10 @@ class StoreCheckTest extends \PHPUnit_Framework_TestCase )->will( $this->returnValue($this->_storeMock) ); - $this->subjectMock = $this->getMock(\Magento\Framework\App\Action\Action::class, [], [], '', false); - $this->closureMock = function () { - return 'Expected'; - }; $this->requestMock = $this->getMock(\Magento\Framework\App\RequestInterface::class); + $this->subjectMock = $this->getMockBuilder(\Magento\Framework\App\Action\AbstractAction::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); $this->_plugin = new \Magento\Store\App\Action\Plugin\StoreCheck($this->_storeManagerMock); } @@ -64,22 +55,15 @@ class StoreCheckTest extends \PHPUnit_Framework_TestCase * @expectedException \Magento\Framework\Exception\State\InitException * @expectedExceptionMessage Current store is not active. */ - public function testAroundDispatchWhenStoreNotActive() + public function testBeforeDispatchWhenStoreNotActive() { $this->_storeMock->expects($this->any())->method('isActive')->will($this->returnValue(false)); - $this->assertEquals( - 'Expected', - $this->_plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock) - ); + $this->_plugin->beforeDispatch($this->subjectMock, $this->requestMock); } - public function testAroundDispatchWhenStoreIsActive() + public function testBeforeDispatchWhenStoreIsActive() { $this->_storeMock->expects($this->any())->method('isActive')->will($this->returnValue(true)); - $this->assertEquals( - 'Expected', - $this->_plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock) - ); + $this->_plugin->beforeDispatch($this->subjectMock, $this->requestMock); } - } diff --git a/app/code/Magento/Store/Test/Unit/Url/Plugin/RouteParamsResolverTest.php b/app/code/Magento/Store/Test/Unit/Url/Plugin/RouteParamsResolverTest.php index b2c5a972734a9663cbcfa3ae13f7cd9f42812ee7..959f9efd0334546196a12df2bf1b546f42c7d6d4 100644 --- a/app/code/Magento/Store/Test/Unit/Url/Plugin/RouteParamsResolverTest.php +++ b/app/code/Magento/Store/Test/Unit/Url/Plugin/RouteParamsResolverTest.php @@ -39,10 +39,7 @@ class RouteParamsResolverTest extends \PHPUnit_Framework_TestCase ); } - /** - * @SuppressWarnings(PHPMD.UnusedLocalVariable) - */ - public function testAroundSetRouteParamsScopeInParams() + public function testBeforeSetRouteParamsScopeInParams() { $storeCode = 'custom_store'; $this->scopeConfigMock @@ -66,20 +63,13 @@ class RouteParamsResolverTest extends \PHPUnit_Framework_TestCase $this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('___store', $storeCode); - $this->model->aroundSetRouteParams( + $this->model->beforeSetRouteParams( $routeParamsResolverMock, - function ($data, $unsetOldParams) { - $this->assertArrayNotHasKey('_scope_to_url', $data, 'This data item should have been unset.'); - $this->assertArrayNotHasKey('_scope', $data, 'This data item should have been unset.'); - }, $data ); } - /** - * @SuppressWarnings(PHPMD.UnusedLocalVariable) - */ - public function testAroundSetRouteParamsScopeUseStoreInUrl() + public function testBeforeSetRouteParamsScopeUseStoreInUrl() { $storeCode = 'custom_store'; $this->scopeConfigMock @@ -103,20 +93,13 @@ class RouteParamsResolverTest extends \PHPUnit_Framework_TestCase $this->queryParamsResolverMock->expects($this->never())->method('setQueryParam'); - $this->model->aroundSetRouteParams( + $this->model->beforeSetRouteParams( $routeParamsResolverMock, - function ($data, $unsetOldParams) { - $this->assertArrayNotHasKey('_scope_to_url', $data, 'This data item should have been unset.'); - $this->assertArrayNotHasKey('_scope', $data, 'This data item should have been unset.'); - }, $data ); } - /** - * @SuppressWarnings(PHPMD.UnusedLocalVariable) - */ - public function testAroundSetRouteParamsSingleStore() + public function testBeforeSetRouteParamsSingleStore() { $storeCode = 'custom_store'; $this->scopeConfigMock @@ -140,20 +123,13 @@ class RouteParamsResolverTest extends \PHPUnit_Framework_TestCase $this->queryParamsResolverMock->expects($this->never())->method('setQueryParam'); - $this->model->aroundSetRouteParams( + $this->model->beforeSetRouteParams( $routeParamsResolverMock, - function ($data, $unsetOldParams) { - $this->assertArrayNotHasKey('_scope_to_url', $data, 'This data item should have been unset.'); - $this->assertArrayNotHasKey('_scope', $data, 'This data item should have been unset.'); - }, $data ); } - /** - * @SuppressWarnings(PHPMD.UnusedLocalVariable) - */ - public function testAroundSetRouteParamsNoScopeInParams() + public function testBeforeSetRouteParamsNoScopeInParams() { $storeCode = 'custom_store'; $this->scopeConfigMock @@ -185,11 +161,8 @@ class RouteParamsResolverTest extends \PHPUnit_Framework_TestCase $this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('___store', $storeCode); - $this->model->aroundSetRouteParams( + $this->model->beforeSetRouteParams( $routeParamsResolverMock, - function ($data, $unsetOldParams) { - $this->assertArrayNotHasKey('_scope_to_url', $data, 'This data item should have been unset.'); - }, $data ); } diff --git a/app/code/Magento/Store/Url/Plugin/RouteParamsResolver.php b/app/code/Magento/Store/Url/Plugin/RouteParamsResolver.php index 4435dfe108cffbffcfd458cc4a8a53dbdd8eb821..8ef0420a23cbe706d8df93abf18ae0fb573f709f 100644 --- a/app/code/Magento/Store/Url/Plugin/RouteParamsResolver.php +++ b/app/code/Magento/Store/Url/Plugin/RouteParamsResolver.php @@ -49,15 +49,12 @@ class RouteParamsResolver * Process scope query parameters. * * @param \Magento\Framework\Url\RouteParamsResolver $subject - * @param callable $proceed * @param array $data * @param bool $unsetOldParams - * @return \Magento\Framework\Url\RouteParamsResolver - * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @return array */ - public function aroundSetRouteParams( + public function beforeSetRouteParams( \Magento\Framework\Url\RouteParamsResolver $subject, - \Closure $proceed, array $data, $unsetOldParams = true ) { @@ -78,6 +75,6 @@ class RouteParamsResolver } unset($data['_scope_to_url']); - return $proceed($data, $unsetOldParams); + return [$data, $unsetOldParams]; } } diff --git a/app/code/Magento/Tax/Model/App/Action/ContextPlugin.php b/app/code/Magento/Tax/Model/App/Action/ContextPlugin.php index 00dd9ff4cda98962934294e1ba52dfbc588b0e50..03927aed968889324c3e44df9dcb5f5fd1de36ac 100644 --- a/app/code/Magento/Tax/Model/App/Action/ContextPlugin.php +++ b/app/code/Magento/Tax/Model/App/Action/ContextPlugin.php @@ -6,9 +6,6 @@ namespace Magento\Tax\Model\App\Action; -use Magento\Customer\Model\Context; -use Magento\Customer\Model\GroupManagement; - /** * Class ContextPlugin */ @@ -74,21 +71,19 @@ class ContextPlugin /** * @param \Magento\Framework\App\ActionInterface $subject - * @param callable $proceed * @param \Magento\Framework\App\RequestInterface $request * @return mixed * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundDispatch( + public function beforeDispatch( \Magento\Framework\App\ActionInterface $subject, - \Closure $proceed, \Magento\Framework\App\RequestInterface $request ) { if (!$this->customerSession->isLoggedIn() || !$this->moduleManager->isEnabled('Magento_PageCache') || !$this->cacheConfig->isEnabled() || !$this->taxHelper->isCatalogPriceDisplayAffectedByTax()) { - return $proceed($request); + return; } $defaultBillingAddress = $this->customerSession->getDefaultTaxBillingAddress(); @@ -107,6 +102,5 @@ class ContextPlugin 0 ); } - return $proceed($request); } } diff --git a/app/code/Magento/Tax/Model/Quote/GrandTotalDetailsPlugin.php b/app/code/Magento/Tax/Model/Quote/GrandTotalDetailsPlugin.php index 7964353a74c96eb40ac652eb72c19730c6c135f8..a727beef10b0648934dbb21b89945ff2d1462193 100644 --- a/app/code/Magento/Tax/Model/Quote/GrandTotalDetailsPlugin.php +++ b/app/code/Magento/Tax/Model/Quote/GrandTotalDetailsPlugin.php @@ -71,18 +71,18 @@ class GrandTotalDetailsPlugin /** * @param \Magento\Quote\Model\Cart\TotalsConverter $subject - * @param \Closure $proceed + * @param \Magento\Quote\Api\Data\TotalSegmentInterface[] $totalSegments * @param \Magento\Quote\Model\Quote\Address\Total[] $addressTotals + * * @return \Magento\Quote\Api\Data\TotalSegmentInterface[] * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ - public function aroundProcess( + public function afterProcess( \Magento\Quote\Model\Cart\TotalsConverter $subject, - \Closure $proceed, + array $totalSegments, array $addressTotals = [] ) { - $totalSegments = $proceed($addressTotals); if (!array_key_exists($this->code, $addressTotals)) { return $totalSegments; diff --git a/app/code/Magento/Tax/Test/Unit/App/Action/ContextPluginTest.php b/app/code/Magento/Tax/Test/Unit/App/Action/ContextPluginTest.php index 6bf64c3300937045c7bde0a6f455f2d4667f4b10..a95ead9efe30405a51558dc8f7ade3ef23306ae4 100644 --- a/app/code/Magento/Tax/Test/Unit/App/Action/ContextPluginTest.php +++ b/app/code/Magento/Tax/Test/Unit/App/Action/ContextPluginTest.php @@ -111,9 +111,9 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase * @param bool $cache * @param bool $taxEnabled * @param bool $loggedIn - * @dataProvider dataProviderAroundDispatch + * @dataProvider beforeDispatchDataProvider */ - public function testAroundDispatch($cache, $taxEnabled, $loggedIn) + public function testBeforeDispatch($cache, $taxEnabled, $loggedIn) { $this->customerSessionMock->expects($this->any()) ->method('isLoggedIn') @@ -160,18 +160,14 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase $action = $this->objectManager->getObject(\Magento\Framework\App\Test\Unit\Action\Stub\ActionStub::class); $request = $this->getMock(\Magento\Framework\App\Request\Http::class, ['getActionName'], [], '', false); - $expectedResult = 'expectedResult'; - $proceed = function ($request) use ($expectedResult) { - return $expectedResult; - }; - $this->contextPlugin->aroundDispatch($action, $proceed, $request); + $this->contextPlugin->beforeDispatch($action, $request); } } /** * @return array */ - public function dataProviderAroundDispatch() + public function beforeDispatchDataProvider() { return [ [false, false, false], diff --git a/app/code/Magento/Tax/Test/Unit/Model/Quote/GrandTotalDetailsPluginTest.php b/app/code/Magento/Tax/Test/Unit/Model/Quote/GrandTotalDetailsPluginTest.php index 52af1315ba76685ac64662f425bcfb9f6b11837e..693b0d437afc45853b5ab224d04c535f1af1ca92 100644 --- a/app/code/Magento/Tax/Test/Unit/Model/Quote/GrandTotalDetailsPluginTest.php +++ b/app/code/Magento/Tax/Test/Unit/Model/Quote/GrandTotalDetailsPluginTest.php @@ -38,11 +38,6 @@ class GrandTotalDetailsPluginTest extends \PHPUnit_Framework_TestCase */ protected $subjectMock; - /** - * @var \Closure|\PHPUnit_Framework_MockObject_MockObject - */ - protected $closureMock; - /** * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */ @@ -153,7 +148,7 @@ class GrandTotalDetailsPluginTest extends \PHPUnit_Framework_TestCase return $taxDetailsMock; } - public function testAroundProcess() + public function testAfterProcess() { $taxRate = [ 'percent' => 8.25, @@ -211,11 +206,7 @@ class GrandTotalDetailsPluginTest extends \PHPUnit_Framework_TestCase 'tax' => $taxSegmentMock, ]; - $this->closureMock = function () use ($totalSegments) { - return $totalSegments; - }; - - $result = $this->model->aroundProcess($this->subjectMock, $this->closureMock, $addressTotals); + $result = $this->model->afterProcess($this->subjectMock, $totalSegments, $addressTotals); $this->assertEquals($totalSegments, $result); } } diff --git a/app/code/Magento/Theme/Model/Url/Plugin/Signature.php b/app/code/Magento/Theme/Model/Url/Plugin/Signature.php index 2a4d878e80d7a1fec2a8daaf970298575b8a633a..8934c598feb79a438b442a39612f81673b3d8f4b 100644 --- a/app/code/Magento/Theme/Model/Url/Plugin/Signature.php +++ b/app/code/Magento/Theme/Model/Url/Plugin/Signature.php @@ -47,7 +47,7 @@ class Signature * Append signature to rendered base URL for static view files * * @param \Magento\Framework\Url\ScopeInterface $subject - * @param callable $proceed + * @param string $baseUrl * @param string $type * @param null $secure * @return string @@ -55,13 +55,12 @@ class Signature * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundGetBaseUrl( + public function afterGetBaseUrl( \Magento\Framework\Url\ScopeInterface $subject, - \Closure $proceed, + $baseUrl, $type = \Magento\Framework\UrlInterface::URL_TYPE_LINK, $secure = null ) { - $baseUrl = $proceed($type, $secure); if ($type == \Magento\Framework\UrlInterface::URL_TYPE_STATIC && $this->isUrlSignatureEnabled()) { $baseUrl .= $this->renderUrlSignature() . '/'; } diff --git a/app/code/Magento/Theme/Test/Unit/Model/Url/Plugin/SignatureTest.php b/app/code/Magento/Theme/Test/Unit/Model/Url/Plugin/SignatureTest.php index 46097a13db12f48c6670649568e44e1cd6d67ee6..58cf9bcc31e5b8bb4a02cf9d5c93e5ca6f645bce 100644 --- a/app/code/Magento/Theme/Test/Unit/Model/Url/Plugin/SignatureTest.php +++ b/app/code/Magento/Theme/Test/Unit/Model/Url/Plugin/SignatureTest.php @@ -3,9 +3,6 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - -// @codingStandardsIgnoreFile - namespace Magento\Theme\Test\Unit\Model\Url\Plugin; use \Magento\Theme\Model\Url\Plugin\Signature; @@ -27,29 +24,25 @@ class SignatureTest extends \PHPUnit_Framework_TestCase */ private $deploymentVersion; - /** - * @var callable - */ - private $closureMock; - protected function setUp() { $this->config = $this->getMock(\Magento\Framework\View\Url\ConfigInterface::class); - $this->deploymentVersion = $this->getMock( - \Magento\Framework\App\View\Deployment\Version::class, [], [], '', false + $this->deploymentVersion = $this->getMock( + \Magento\Framework\App\View\Deployment\Version::class, + [], + [], + '', + false ); - $this->closureMock = function () { - return 'http://127.0.0.1/magento/pub/static/'; - }; $this->object = new Signature($this->config, $this->deploymentVersion); } /** * @param bool|int $fixtureConfigFlag * @param string $inputUrlType - * @dataProvider aroundGetBaseUrlInactiveDataProvider + * @dataProvider afterGetBaseUrlInactiveDataProvider */ - public function testAroundGetBaseUrlInactive($fixtureConfigFlag, $inputUrlType) + public function testAfterGetBaseUrlInactive($fixtureConfigFlag, $inputUrlType) { $this->config ->expects($this->any()) @@ -59,11 +52,14 @@ class SignatureTest extends \PHPUnit_Framework_TestCase $this->deploymentVersion->expects($this->never())->method($this->anything()); $url = $this->getMockForAbstractClass(\Magento\Framework\Url\ScopeInterface::class); - $actualResult = $this->object->aroundGetBaseUrl($url, $this->closureMock, $inputUrlType); + $actualResult = $this->object->afterGetBaseUrl($url, 'http://127.0.0.1/magento/pub/static/', $inputUrlType); $this->assertEquals('http://127.0.0.1/magento/pub/static/', $actualResult); } - public function aroundGetBaseUrlInactiveDataProvider() + /** + * @return array + */ + public function afterGetBaseUrlInactiveDataProvider() { return [ 'disabled in config, relevant URL type' => [0, \Magento\Framework\UrlInterface::URL_TYPE_STATIC], @@ -81,8 +77,10 @@ class SignatureTest extends \PHPUnit_Framework_TestCase $this->deploymentVersion->expects($this->once())->method('getValue')->will($this->returnValue('123')); $url = $this->getMockForAbstractClass(\Magento\Framework\Url\ScopeInterface::class); - $actualResult = $this->object->aroundGetBaseUrl( - $url, $this->closureMock, \Magento\Framework\UrlInterface::URL_TYPE_STATIC + $actualResult = $this->object->afterGetBaseUrl( + $url, + 'http://127.0.0.1/magento/pub/static/', + \Magento\Framework\UrlInterface::URL_TYPE_STATIC ); $this->assertEquals('http://127.0.0.1/magento/pub/static/version123/', $actualResult); } diff --git a/app/code/Magento/Vault/Plugin/PaymentVaultAttributesLoad.php b/app/code/Magento/Vault/Plugin/PaymentVaultAttributesLoad.php index 810a8c5242da8ce24cbbb5ac15d63260f3853687..710b765d11effb155d17c02986ec561e0d54207d 100644 --- a/app/code/Magento/Vault/Plugin/PaymentVaultAttributesLoad.php +++ b/app/code/Magento/Vault/Plugin/PaymentVaultAttributesLoad.php @@ -10,6 +10,8 @@ namespace Magento\Vault\Plugin; use Magento\Sales\Api\Data\OrderPaymentExtensionInterface; use Magento\Sales\Api\Data\OrderPaymentInterface; use Magento\Vault\Api\PaymentTokenManagementInterface; +use Magento\Sales\Api\Data\OrderPaymentExtensionFactory; +use Magento\Vault\Api\Data\PaymentTokenInterface; /** * Plugin for loading vault payment extension attribute to order/payment entity @@ -17,7 +19,7 @@ use Magento\Vault\Api\PaymentTokenManagementInterface; class PaymentVaultAttributesLoad { /** - * @var \Magento\Sales\Api\Data\OrderPaymentExtensionFactory + * @var OrderPaymentExtensionFactory */ protected $paymentExtensionFactory; @@ -27,11 +29,11 @@ class PaymentVaultAttributesLoad protected $paymentTokenManagement; /** - * @param \Magento\Sales\Api\Data\OrderPaymentExtensionFactory $paymentExtensionFactory + * @param OrderPaymentExtensionFactory $paymentExtensionFactory * @param PaymentTokenManagement|PaymentTokenManagementInterface $paymentTokenManagement */ public function __construct( - \Magento\Sales\Api\Data\OrderPaymentExtensionFactory $paymentExtensionFactory, + OrderPaymentExtensionFactory $paymentExtensionFactory, PaymentTokenManagementInterface $paymentTokenManagement ) { $this->paymentExtensionFactory = $paymentExtensionFactory; @@ -42,16 +44,13 @@ class PaymentVaultAttributesLoad * Load vault payment extension attribute to order/payment entity * * @param OrderPaymentInterface $payment - * @param \Closure $proceed + * @param OrderPaymentExtensionInterface|null $paymentExtension * @return OrderPaymentExtensionInterface */ - public function aroundGetExtensionAttributes( + public function afterGetExtensionAttributes( OrderPaymentInterface $payment, - \Closure $proceed + OrderPaymentExtensionInterface $paymentExtension = null ) { - /** @var OrderPaymentExtensionInterface $paymentExtension */ - $paymentExtension = $proceed(); - if ($paymentExtension === null) { $paymentExtension = $this->paymentExtensionFactory->create(); } @@ -59,7 +58,7 @@ class PaymentVaultAttributesLoad $paymentToken = $paymentExtension->getVaultPaymentToken(); if ($paymentToken === null) { $paymentToken = $this->paymentTokenManagement->getByPaymentId($payment->getEntityId()); - if ($paymentToken instanceof \Magento\Vault\Api\Data\PaymentTokenInterface) { + if ($paymentToken instanceof PaymentTokenInterface) { $paymentExtension->setVaultPaymentToken($paymentToken); } $payment->setExtensionAttributes($paymentExtension); diff --git a/app/code/Magento/Weee/Model/App/Action/ContextPlugin.php b/app/code/Magento/Weee/Model/App/Action/ContextPlugin.php index c00f2f3491e03b16628d49ea34e5990e705c13e5..43344c196ec524e053842854db76c86951f70ad0 100644 --- a/app/code/Magento/Weee/Model/App/Action/ContextPlugin.php +++ b/app/code/Magento/Weee/Model/App/Action/ContextPlugin.php @@ -92,29 +92,27 @@ class ContextPlugin /** * @param \Magento\Framework\App\ActionInterface $subject - * @param callable $proceed * @param \Magento\Framework\App\RequestInterface $request - * @return mixed + * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ - public function aroundDispatch( + public function beforeDispatch( \Magento\Framework\App\ActionInterface $subject, - \Closure $proceed, \Magento\Framework\App\RequestInterface $request ) { if (!$this->weeeHelper->isEnabled() || !$this->customerSession->isLoggedIn() || !$this->moduleManager->isEnabled('Magento_PageCache') || !$this->cacheConfig->isEnabled()) { - return $proceed($request); + return; } $basedOn = $this->taxHelper->getTaxBasedOn(); if ($basedOn != 'shipping' && $basedOn != 'billing') { - return $proceed($request); + return; } $weeeTaxRegion = $this->getWeeeTaxRegion($basedOn); @@ -124,7 +122,7 @@ class ContextPlugin if (!$countryId && !$regionId) { // country and region does not exist - return $proceed($request); + return; } else if ($countryId && !$regionId) { // country exist and region does not exist $regionId = 0; @@ -158,7 +156,6 @@ class ContextPlugin 0 ); } - return $proceed($request); } /** diff --git a/app/code/Magento/Weee/Test/Unit/App/Action/ContextPluginTest.php b/app/code/Magento/Weee/Test/Unit/App/Action/ContextPluginTest.php index ebb0837d3c14474f27951a1158927d75cace4ed3..77c8070ca44eb51ff546a71a85545393657f6f94 100644 --- a/app/code/Magento/Weee/Test/Unit/App/Action/ContextPluginTest.php +++ b/app/code/Magento/Weee/Test/Unit/App/Action/ContextPluginTest.php @@ -123,7 +123,7 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase ); } - public function testAroundDispatchBasedOnDefault() + public function testBeforeDispatchBasedOnDefault() { $this->customerSessionMock->expects($this->once()) ->method('isLoggedIn') @@ -187,14 +187,11 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase $action = $this->objectManager->getObject(\Magento\Framework\App\Test\Unit\Action\Stub\ActionStub::class); $request = $this->getMock(\Magento\Framework\App\Request\Http::class, ['getActionName'], [], '', false); - $expectedResult = 'expectedResult'; - $proceed = function ($request) use ($expectedResult) { - return $expectedResult; - }; - $this->contextPlugin->aroundDispatch($action, $proceed, $request); + + $this->contextPlugin->beforeDispatch($action, $request); } - public function testAroundDispatchBasedOnOrigin() + public function testBeforeDispatchBasedOnOrigin() { $this->customerSessionMock->expects($this->once()) ->method('isLoggedIn') @@ -219,14 +216,11 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase $action = $this->objectManager->getObject(\Magento\Framework\App\Test\Unit\Action\Stub\ActionStub::class); $request = $this->getMock(\Magento\Framework\App\Request\Http::class, ['getActionName'], [], '', false); - $expectedResult = 'expectedResult'; - $proceed = function ($request) use ($expectedResult) { - return $expectedResult; - }; - $this->contextPlugin->aroundDispatch($action, $proceed, $request); + + $this->contextPlugin->beforeDispatch($action, $request); } - public function testAroundDispatchBasedOnBilling() + public function testBeforeDispatchBasedOnBilling() { $this->customerSessionMock->expects($this->once()) ->method('isLoggedIn') @@ -294,14 +288,11 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase $action = $this->objectManager->getObject(\Magento\Framework\App\Test\Unit\Action\Stub\ActionStub::class); $request = $this->getMock(\Magento\Framework\App\Request\Http::class, ['getActionName'], [], '', false); - $expectedResult = 'expectedResult'; - $proceed = function ($request) use ($expectedResult) { - return $expectedResult; - }; - $this->contextPlugin->aroundDispatch($action, $proceed, $request); + + $this->contextPlugin->beforeDispatch($action, $request); } - public function testAroundDispatchBasedOnShipping() + public function testBeforeDispatchBasedOnShipping() { $this->customerSessionMock->expects($this->once()) ->method('isLoggedIn') @@ -369,10 +360,7 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase $action = $this->objectManager->getObject(\Magento\Framework\App\Test\Unit\Action\Stub\ActionStub::class); $request = $this->getMock(\Magento\Framework\App\Request\Http::class, ['getActionName'], [], '', false); - $expectedResult = 'expectedResult'; - $proceed = function ($request) use ($expectedResult) { - return $expectedResult; - }; - $this->contextPlugin->aroundDispatch($action, $proceed, $request); + + $this->contextPlugin->beforeDispatch($action, $request); } } diff --git a/lib/internal/Magento/Framework/Interception/Code/InterfaceValidator.php b/lib/internal/Magento/Framework/Interception/Code/InterfaceValidator.php index 1ff11552533b1e6514409402e778687483ae6d18..93945c607b53d7498a9031fbb475f30e015ecb6c 100644 --- a/lib/internal/Magento/Framework/Interception/Code/InterfaceValidator.php +++ b/lib/internal/Magento/Framework/Interception/Code/InterfaceValidator.php @@ -111,13 +111,13 @@ class InterfaceValidator ); break; case self::METHOD_AFTER: - // TODO: Remove this condition check in scope of MAGETWO-56123 if (count($pluginMethodParameters) > 1) { // remove result array_shift($pluginMethodParameters); + $matchedParameters = array_intersect_key($originMethodParameters, $pluginMethodParameters); $this->validateMethodsParameters( $pluginMethodParameters, - $originMethodParameters, + $matchedParameters, $pluginClass, $pluginMethod->getName() ); diff --git a/lib/internal/Magento/Framework/Module/Plugin/DbStatusValidator.php b/lib/internal/Magento/Framework/Module/Plugin/DbStatusValidator.php index 6f312529c12b40eaacc9d815411d4f065999b2d3..850d64b14ae0cd7efc4c067f919eb99245ba0414 100644 --- a/lib/internal/Magento/Framework/Module/Plugin/DbStatusValidator.php +++ b/lib/internal/Magento/Framework/Module/Plugin/DbStatusValidator.php @@ -1,22 +1,24 @@ <?php /** - * Validation of DB up to date state - * * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - -// @codingStandardsIgnoreFile - namespace Magento\Framework\Module\Plugin; -use Magento\Framework\Cache\FrontendInterface; +use Magento\Framework\Cache\FrontendInterface as FrontendCacheInterface; use Magento\Framework\Module\DbVersionInfo; +use Magento\Framework\App\FrontController; +use Magento\Framework\App\RequestInterface; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Phrase; +/** + * Validation of DB up to date state + */ class DbStatusValidator { /** - * @var FrontendInterface + * @var FrontendCacheInterface */ private $cache; @@ -26,47 +28,40 @@ class DbStatusValidator private $dbVersionInfo; /** - * @param FrontendInterface $cache + * @param FrontendCacheInterface $cache * @param DbVersionInfo $dbVersionInfo */ - public function __construct( - FrontendInterface $cache, - DbVersionInfo $dbVersionInfo - ) { + public function __construct(FrontendCacheInterface $cache, DbVersionInfo $dbVersionInfo) + { $this->cache = $cache; $this->dbVersionInfo = $dbVersionInfo; } /** - * @param \Magento\Framework\App\FrontController $subject - * @param \Closure $proceed - * @param \Magento\Framework\App\RequestInterface $request + * Perform check if DB is up to date + * + * @param FrontController $subject + * @param RequestInterface $request + * @return void + * @throws LocalizedException * - * @throws \Magento\Framework\Exception\LocalizedException - * @return \Magento\Framework\App\ResponseInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundDispatch( - \Magento\Framework\App\FrontController $subject, - \Closure $proceed, - \Magento\Framework\App\RequestInterface $request - ) { + public function beforeDispatch(FrontController $subject, RequestInterface $request) + { if (!$this->cache->load('db_is_up_to_date')) { $errors = $this->dbVersionInfo->getDbVersionErrors(); + if ($errors) { - $formattedErrors = $this->formatErrors($errors); - throw new \Magento\Framework\Exception\LocalizedException( - new \Magento\Framework\Phrase( - 'Please upgrade your database: Run "bin/magento setup:upgrade" from the Magento root directory.' - . ' %1The following modules are outdated:%2%3', - [PHP_EOL, PHP_EOL, implode(PHP_EOL, $formattedErrors)] - ) - ); + $message = 'Please upgrade your database: ' + . "Run \"bin/magento setup:upgrade\" from the Magento root directory.\n" + . "The following modules are outdated:\n%1"; + + throw new LocalizedException(new Phrase($message, [implode("\n", $this->formatErrors($errors))])); } else { $this->cache->save('true', 'db_is_up_to_date'); } } - return $proceed($request); } /** @@ -78,12 +73,13 @@ class DbStatusValidator private function formatErrors($errorsData) { $formattedErrors = []; + foreach ($errorsData as $error) { - $formattedErrors[] = $error[DbVersionInfo::KEY_MODULE] . - ' ' . $error[DbVersionInfo::KEY_TYPE] . - ': current version - ' . $error[DbVersionInfo::KEY_CURRENT ] . - ', required version - ' . $error[DbVersionInfo::KEY_REQUIRED]; + $formattedErrors[] = $error[DbVersionInfo::KEY_MODULE] . ' ' . $error[DbVersionInfo::KEY_TYPE] + . ': current version - ' . $error[DbVersionInfo::KEY_CURRENT] + . ', required version - ' . $error[DbVersionInfo::KEY_REQUIRED]; } + return $formattedErrors; } } diff --git a/lib/internal/Magento/Framework/Module/Test/Unit/Plugin/DbStatusValidatorTest.php b/lib/internal/Magento/Framework/Module/Test/Unit/Plugin/DbStatusValidatorTest.php index fee050560815c293a1f7a724de0653e606e21fc8..c0563020110daa8b4ef5a4e7cd3824dee2a5d3b0 100644 --- a/lib/internal/Magento/Framework/Module/Test/Unit/Plugin/DbStatusValidatorTest.php +++ b/lib/internal/Magento/Framework/Module/Test/Unit/Plugin/DbStatusValidatorTest.php @@ -21,11 +21,6 @@ class DbStatusValidatorTest extends \PHPUnit_Framework_TestCase */ protected $_cacheMock; - /** - * @var \Closure - */ - protected $closureMock; - /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -49,9 +44,6 @@ class DbStatusValidatorTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->_cacheMock = $this->getMock(\Magento\Framework\Cache\FrontendInterface::class); - $this->closureMock = function () { - return 'Expected'; - }; $this->requestMock = $this->getMock(\Magento\Framework\App\RequestInterface::class); $this->subjectMock = $this->getMock(\Magento\Framework\App\FrontController::class, [], [], '', false); $moduleList = $this->getMockForAbstractClass(\Magento\Framework\Module\ModuleListInterface::class); @@ -85,8 +77,8 @@ class DbStatusValidatorTest extends \PHPUnit_Framework_TestCase ->will($this->returnValueMap($returnMap)); $this->assertEquals( - 'Expected', - $this->_model->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock) + null, + $this->_model->beforeDispatch($this->subjectMock, $this->requestMock) ); } @@ -101,8 +93,8 @@ class DbStatusValidatorTest extends \PHPUnit_Framework_TestCase $this->moduleManager->expects($this->never()) ->method('isDbDataUpToDate'); $this->assertEquals( - 'Expected', - $this->_model->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock) + null, + $this->_model->beforeDispatch($this->subjectMock, $this->requestMock) ); } @@ -125,7 +117,7 @@ class DbStatusValidatorTest extends \PHPUnit_Framework_TestCase ->method('getDbVersionErrors') ->will($this->returnValue($dbVersionErrors)); - $this->_model->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock); + $this->_model->beforeDispatch($this->subjectMock, $this->requestMock); } /** diff --git a/lib/internal/Magento/Framework/Test/Unit/Module/Plugin/DbStatusValidatorTest.php b/lib/internal/Magento/Framework/Test/Unit/Module/Plugin/DbStatusValidatorTest.php new file mode 100644 index 0000000000000000000000000000000000000000..f22192639f77e62f52ccae93906b096b733934ba --- /dev/null +++ b/lib/internal/Magento/Framework/Test/Unit/Module/Plugin/DbStatusValidatorTest.php @@ -0,0 +1,137 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Framework\Test\Unit\Module\Plugin; + +use Magento\Framework\Module\Plugin\DbStatusValidator as DbStatusValidatorPlugin; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Magento\Framework\Cache\FrontendInterface as FrontendCacheInterface; +use Magento\Framework\Module\DbVersionInfo; +use Magento\Framework\App\FrontController; +use Magento\Framework\App\RequestInterface; +use Magento\Framework\Exception\LocalizedException; + +class DbStatusValidatorTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var DbStatusValidatorPlugin + */ + private $plugin; + + /** + * @var ObjectManagerHelper + */ + private $objectManagerHelper; + + /** + * @var FrontendCacheInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $cacheMock; + + /** + * @var DbVersionInfo|\PHPUnit_Framework_MockObject_MockObject + */ + private $dbVersionInfoMock; + + /** + * @var FrontController|\PHPUnit_Framework_MockObject_MockObject + */ + private $frontControllerMock; + + /** + * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $requestMock; + + protected function setUp() + { + $this->cacheMock = $this->getMockBuilder(FrontendCacheInterface::class) + ->getMockForAbstractClass(); + $this->dbVersionInfoMock = $this->getMockBuilder(DbVersionInfo::class) + ->disableOriginalConstructor() + ->getMock(); + $this->frontControllerMock = $this->getMockBuilder(FrontController::class) + ->disableOriginalConstructor() + ->getMock(); + $this->requestMock = $this->getMockBuilder(RequestInterface::class) + ->getMockForAbstractClass(); + + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->plugin = $this->objectManagerHelper->getObject( + DbStatusValidatorPlugin::class, + [ + 'cache' => $this->cacheMock, + 'dbVersionInfo' => $this->dbVersionInfoMock + ] + ); + } + + public function testBeforeDispatchUpToDate() + { + $this->cacheMock->expects(static::any()) + ->method('load') + ->with('db_is_up_to_date') + ->willReturn('cache_data'); + $this->dbVersionInfoMock->expects(static::never()) + ->method('getDbVersionErrors'); + $this->cacheMock->expects(static::never()) + ->method('save'); + + $this->plugin->beforeDispatch($this->frontControllerMock, $this->requestMock); + } + + public function testBeforeDispatchOutOfDateNoErrors() + { + $this->cacheMock->expects(static::any()) + ->method('load') + ->with('db_is_up_to_date') + ->willReturn(false); + $this->dbVersionInfoMock->expects(static::once()) + ->method('getDbVersionErrors') + ->willReturn([]); + $this->cacheMock->expects(static::once()) + ->method('save') + ->with('true', 'db_is_up_to_date', [], null) + ->willReturn(true); + + $this->plugin->beforeDispatch($this->frontControllerMock, $this->requestMock); + } + + public function testBeforeDispatchOutOfDateWithErrors() + { + $errors = [ + [ + DbVersionInfo::KEY_MODULE => 'Magento_Module1', + DbVersionInfo::KEY_TYPE => 'schema', + DbVersionInfo::KEY_CURRENT => '3.3.3', + DbVersionInfo::KEY_REQUIRED => '4.4.4' + ], + [ + DbVersionInfo::KEY_MODULE => 'Magento_Module2', + DbVersionInfo::KEY_TYPE => 'data', + DbVersionInfo::KEY_CURRENT => '2.8.7', + DbVersionInfo::KEY_REQUIRED => '5.1.6' + ] + ]; + $message = 'Please upgrade your database: ' + . "Run \"bin/magento setup:upgrade\" from the Magento root directory.\n" + . "The following modules are outdated:\n" + . "Magento_Module1 schema: current version - 3.3.3, required version - 4.4.4\n" + . "Magento_Module2 data: current version - 2.8.7, required version - 5.1.6"; + + $this->cacheMock->expects(static::any()) + ->method('load') + ->with('db_is_up_to_date') + ->willReturn(false); + $this->dbVersionInfoMock->expects(static::once()) + ->method('getDbVersionErrors') + ->willReturn($errors); + $this->cacheMock->expects(static::never()) + ->method('save'); + + $this->setExpectedException(LocalizedException::class, $message); + $this->plugin->beforeDispatch($this->frontControllerMock, $this->requestMock); + } +}