Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 3c446dc

Browse files
author
Stanislav Idolov
committed
CheckoutAgreements getList refactoring
1 parent 14eeea2 commit 3c446dc

File tree

10 files changed

+371
-24
lines changed

10 files changed

+371
-24
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CheckoutAgreements\Api;
8+
9+
/**
10+
* Interface CheckoutAgreementsListingInterface
11+
*
12+
* @api
13+
*/
14+
interface CheckoutAgreementsListingInterface
15+
{
16+
/**
17+
* Listing of checkout agreements.
18+
*
19+
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
20+
* @return \Magento\CheckoutAgreements\Api\Data\AgreementInterface[]
21+
*/
22+
public function getListing(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) : array;
23+
}

app/code/Magento/CheckoutAgreements/Api/CheckoutAgreementsRepositoryInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public function get($id, $storeId = null);
2525
* Lists active checkout agreements.
2626
*
2727
* @return \Magento\CheckoutAgreements\Api\Data\AgreementInterface[]
28+
* @deprecated
29+
* @see \Magento\CheckoutAgreements\Api\CheckoutAgreementsListingInterface::getListing
2830
*/
2931
public function getList();
3032

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CheckoutAgreements\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor;
8+
9+
class StoreFilter implements
10+
\Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface
11+
{
12+
/**
13+
* Apply custom store filter to collection
14+
*
15+
* @param \Magento\Framework\Api\Filter $filter
16+
* @param \Magento\Framework\Data\Collection\AbstractDb $collection
17+
* @return bool
18+
*/
19+
public function apply(
20+
\Magento\Framework\Api\Filter $filter,
21+
\Magento\Framework\Data\Collection\AbstractDb $collection
22+
) {
23+
/** @var \Magento\Cms\Model\ResourceModel\Page\Collection $collection */
24+
$collection->addStoreFilter($filter->getValue(), false);
25+
return true;
26+
}
27+
}
28+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CheckoutAgreements\Model;
8+
9+
class CheckoutAgreementsListing implements \Magento\CheckoutAgreements\Api\CheckoutAgreementsListingInterface
10+
{
11+
/**
12+
* Collection factory.
13+
*
14+
* @var \Magento\CheckoutAgreements\Model\ResourceModel\Agreement\CollectionFactory
15+
*/
16+
private $collectionFactory;
17+
18+
/**
19+
* @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface
20+
*/
21+
private $extAttributesJoinProcessor;
22+
23+
/**
24+
* @var \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface
25+
*/
26+
private $collectionProcessor;
27+
28+
/**
29+
* @param ResourceModel\Agreement\CollectionFactory $collectionFactory
30+
* @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
31+
* @param \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface $collectionProcessor
32+
*/
33+
public function __construct(
34+
\Magento\CheckoutAgreements\Model\ResourceModel\Agreement\CollectionFactory $collectionFactory,
35+
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor,
36+
\Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface $collectionProcessor
37+
) {
38+
$this->collectionFactory = $collectionFactory;
39+
$this->extAttributesJoinProcessor = $extensionAttributesJoinProcessor;
40+
$this->collectionProcessor = $collectionProcessor;
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function getListing(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) : array
47+
{
48+
/** @var $collection \Magento\CheckoutAgreements\Model\ResourceModel\Agreement\Collection */
49+
$collection = $this->collectionFactory->create();
50+
$this->collectionProcessor->process($searchCriteria, $collection);
51+
$this->extAttributesJoinProcessor->process($collection);
52+
return $collection->getItems();
53+
}
54+
}

app/code/Magento/CheckoutAgreements/Model/CheckoutAgreementsRepository.php

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\CheckoutAgreements\Model\ResourceModel\Agreement\Collection as AgreementCollection;
1111
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
1212
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Framework\App\ObjectManager;
1314
use Magento\Store\Model\StoreManagerInterface;
1415
use Magento\Store\Model\ScopeInterface;
1516
use Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface;
@@ -60,6 +61,21 @@ class CheckoutAgreementsRepository implements CheckoutAgreementsRepositoryInterf
6061
*/
6162
private $extensionAttributesJoinProcessor;
6263

64+
/**
65+
* @var \Magento\CheckoutAgreements\Api\CheckoutAgreementsListingInterface
66+
*/
67+
private $agreementsListing;
68+
69+
/**
70+
* @var \Magento\Framework\Api\FilterBuilder
71+
*/
72+
private $filterBuilder;
73+
74+
/**
75+
* @var \Magento\Framework\Api\SearchCriteriaBuilder
76+
*/
77+
private $searchCriteriaBuilder;
78+
6379
/**
6480
* Constructs a checkout agreement data object.
6581
*
@@ -69,6 +85,9 @@ class CheckoutAgreementsRepository implements CheckoutAgreementsRepositoryInterf
6985
* @param AgreementResource $agreementResource
7086
* @param AgreementFactory $agreementFactory
7187
* @param JoinProcessorInterface $extensionAttributesJoinProcessor
88+
* @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsListingInterface|null $agreementsListing
89+
* @param \Magento\Framework\Api\FilterBuilder|null $filterBuilder
90+
* @param \Magento\Framework\Api\SearchCriteriaBuilder|null $searchCriteriaBuilder
7291
* @codeCoverageIgnore
7392
*/
7493
public function __construct(
@@ -77,14 +96,26 @@ public function __construct(
7796
ScopeConfigInterface $scopeConfig,
7897
AgreementResource $agreementResource,
7998
AgreementFactory $agreementFactory,
80-
JoinProcessorInterface $extensionAttributesJoinProcessor
99+
JoinProcessorInterface $extensionAttributesJoinProcessor,
100+
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListingInterface $agreementsListing = null,
101+
\Magento\Framework\Api\FilterBuilder $filterBuilder = null,
102+
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder = null
81103
) {
82104
$this->collectionFactory = $collectionFactory;
83105
$this->storeManager = $storeManager;
84106
$this->scopeConfig = $scopeConfig;
85107
$this->resourceModel = $agreementResource;
86108
$this->agreementFactory = $agreementFactory;
87109
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
110+
$this->agreementsListing = $agreementsListing ?: ObjectManager::getInstance()->get(
111+
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListingInterface::class
112+
);
113+
$this->filterBuilder = $filterBuilder ?: ObjectManager::getInstance()->get(
114+
\Magento\Framework\Api\FilterBuilder::class
115+
);
116+
$this->searchCriteriaBuilder = $searchCriteriaBuilder ?: ObjectManager::getInstance()->get(
117+
\Magento\Framework\Api\SearchCriteriaBuilder::class
118+
);
88119
}
89120

90121
/**
@@ -97,18 +128,21 @@ public function getList()
97128
if (!$this->scopeConfig->isSetFlag('checkout/options/enable_agreements', ScopeInterface::SCOPE_STORE)) {
98129
return [];
99130
}
100-
$storeId = $this->storeManager->getStore()->getId();
101-
/** @var $agreementCollection AgreementCollection */
102-
$agreementCollection = $this->collectionFactory->create();
103-
$this->extensionAttributesJoinProcessor->process($agreementCollection);
104-
$agreementCollection->addStoreFilter($storeId);
105-
$agreementCollection->addFieldToFilter('is_active', 1);
106-
$agreementDataObjects = [];
107-
foreach ($agreementCollection as $agreement) {
108-
$agreementDataObjects[] = $agreement;
109-
}
110131

111-
return $agreementDataObjects;
132+
$storeFilter = $this->filterBuilder
133+
->setField('store_id')
134+
->setConditionType('eq')
135+
->setValue($this->storeManager->getStore()->getId())
136+
->create();
137+
$isActiveFilter = $this->filterBuilder
138+
->setField('is_active')
139+
->setConditionType('eq')
140+
->setValue(1)
141+
->create();
142+
143+
$this->searchCriteriaBuilder->addFilters([$storeFilter]);
144+
$this->searchCriteriaBuilder->addFilters([$isActiveFilter]);
145+
return $this->agreementsListing->getListing($this->searchCriteriaBuilder->create());
112146
}
113147

114148
/**
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CheckoutAgreements\Test\Unit\Model;
8+
9+
class CheckoutAgreementsListingTest extends \PHPUnit\Framework\TestCase
10+
{
11+
/**
12+
* @var \Magento\CheckoutAgreements\Model\CheckoutAgreementsListing
13+
*/
14+
private $model;
15+
16+
/**
17+
* @var \PHPUnit_Framework_MockObject_MockObject
18+
*/
19+
private $collectionFactoryMock;
20+
21+
/**
22+
* @var \PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
private $attributesJoinProcessorMock;
25+
26+
/**
27+
* @var \PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
private $collectionProcessorMock;
30+
31+
protected function setUp()
32+
{
33+
$this->collectionFactoryMock = $this->createMock(
34+
\Magento\CheckoutAgreements\Model\ResourceModel\Agreement\CollectionFactory::class
35+
);
36+
$this->attributesJoinProcessorMock = $this->createMock(
37+
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class
38+
);
39+
$this->collectionProcessorMock = $this->createMock(
40+
\Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
41+
);
42+
$this->model = new \Magento\CheckoutAgreements\Model\CheckoutAgreementsListing(
43+
$this->collectionFactoryMock,
44+
$this->attributesJoinProcessorMock,
45+
$this->collectionProcessorMock
46+
);
47+
}
48+
49+
public function testGetListing()
50+
{
51+
$searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteriaInterface::class);
52+
$collectionMock = $this->createMock(
53+
\Magento\CheckoutAgreements\Model\ResourceModel\Agreement\Collection::class
54+
);
55+
$this->collectionFactoryMock->expects($this->once())->method('create')->willReturn($collectionMock);
56+
$this->collectionProcessorMock->expects($this->once())
57+
->method('process')
58+
->with($searchCriteriaMock, $collectionMock);
59+
$this->attributesJoinProcessorMock->expects($this->once())->method('process')->with($collectionMock);
60+
$agreementMock = $this->createMock(\Magento\CheckoutAgreements\Api\Data\AgreementInterface::class);
61+
$collectionMock->expects($this->once())->method('getItems')->willReturn([$agreementMock]);
62+
$this->assertEquals([$agreementMock], $this->model->getListing($searchCriteriaMock));
63+
}
64+
}

app/code/Magento/CheckoutAgreements/Test/Unit/Model/CheckoutAgreementsRepositoryTest.php

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ class CheckoutAgreementsRepositoryTest extends \PHPUnit\Framework\TestCase
6464
*/
6565
protected $extensionAttributesJoinProcessorMock;
6666

67+
/**
68+
* @var \PHPUnit_Framework_MockObject_MockObject
69+
*/
70+
private $agreementsListingMock;
71+
72+
/**
73+
* @var \PHPUnit_Framework_MockObject_MockObject
74+
*/
75+
private $filterBuilderMock;
76+
77+
/**
78+
* @var \PHPUnit_Framework_MockObject_MockObject
79+
*/
80+
private $searchCriteriaBuilderMock;
81+
6782
protected function setUp()
6883
{
6984
$this->objectManager = new ObjectManager($this);
@@ -88,13 +103,22 @@ protected function setUp()
88103
['process']
89104
);
90105

106+
$this->agreementsListingMock = $this->createMock(
107+
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListingInterface::class
108+
);
109+
$this->filterBuilderMock = $this->createMock(\Magento\Framework\Api\FilterBuilder::class);
110+
$this->searchCriteriaBuilderMock = $this->createMock(\Magento\Framework\Api\SearchCriteriaBuilder::class);
111+
91112
$this->model = new \Magento\CheckoutAgreements\Model\CheckoutAgreementsRepository(
92113
$this->factoryMock,
93114
$this->storeManagerMock,
94115
$this->scopeConfigMock,
95116
$this->resourceMock,
96117
$this->agrFactoryMock,
97-
$this->extensionAttributesJoinProcessorMock
118+
$this->extensionAttributesJoinProcessorMock,
119+
$this->agreementsListingMock,
120+
$this->filterBuilderMock,
121+
$this->searchCriteriaBuilderMock
98122
);
99123
}
100124

@@ -112,10 +136,6 @@ public function testGetListReturnsEmptyListIfCheckoutAgreementsAreDisabledOnFron
112136

113137
public function testGetListReturnsTheListOfActiveCheckoutAgreements()
114138
{
115-
$this->extensionAttributesJoinProcessorMock->expects($this->once())
116-
->method('process')
117-
->with($this->isInstanceOf(\Magento\CheckoutAgreements\Model\ResourceModel\Agreement\Collection::class));
118-
119139
$this->scopeConfigMock->expects($this->once())
120140
->method('isSetFlag')
121141
->with('checkout/options/enable_agreements', ScopeInterface::SCOPE_STORE, null)
@@ -126,14 +146,35 @@ public function testGetListReturnsTheListOfActiveCheckoutAgreements()
126146
$storeMock->expects($this->any())->method('getId')->will($this->returnValue($storeId));
127147
$this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($storeMock));
128148

129-
$collectionMock = $this->objectManager->getCollectionMock(
130-
\Magento\CheckoutAgreements\Model\ResourceModel\Agreement\Collection::class,
131-
[$this->agreementMock]
132-
);
133-
$this->factoryMock->expects($this->once())->method('create')->will($this->returnValue($collectionMock));
134-
$collectionMock->expects($this->once())->method('addStoreFilter')->with($storeId);
135-
$collectionMock->expects($this->once())->method('addFieldToFilter')->with('is_active', 1);
149+
$storeFilterMock = $this->createMock(\Magento\Framework\Api\Filter::class);
150+
$activeFilterMock = $this->createMock(\Magento\Framework\Api\Filter::class);
151+
152+
$this->filterBuilderMock->expects($this->at(0))->method('setField')->with('store_id')->willReturnSelf();
153+
$this->filterBuilderMock->expects($this->at(1))->method('setConditionType')->with('eq')->willReturnSelf();
154+
$this->filterBuilderMock->expects($this->at(2))->method('setValue')->with($storeId)->willReturnSelf();
155+
$this->filterBuilderMock->expects($this->at(3))->method('create')->willReturn($storeFilterMock);
156+
157+
$this->filterBuilderMock->expects($this->at(4))->method('setField')->with('is_active')->willReturnSelf();
158+
$this->filterBuilderMock->expects($this->at(5))->method('setConditionType')->with('eq')->willReturnSelf();
159+
$this->filterBuilderMock->expects($this->at(6))->method('setValue')->with(1)->willReturnSelf();
160+
$this->filterBuilderMock->expects($this->at(7))->method('create')->willReturn($activeFilterMock);
161+
162+
$this->searchCriteriaBuilderMock->expects($this->at(0))
163+
->method('addFilters')
164+
->with([$storeFilterMock])
165+
->willReturnSelf();
166+
$this->searchCriteriaBuilderMock->expects($this->at(1))
167+
->method('addFilters')
168+
->with([$activeFilterMock])
169+
->willReturnSelf();
170+
171+
$searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
172+
$this->searchCriteriaBuilderMock->expects($this->at(2))->method('create')->willReturn($searchCriteriaMock);
136173

174+
$this->agreementsListingMock->expects($this->once())
175+
->method('getListing')
176+
->with($searchCriteriaMock)
177+
->willReturn([$this->agreementMock]);
137178
$this->assertEquals([$this->agreementMock], $this->model->getList());
138179
}
139180

0 commit comments

Comments
 (0)