Skip to content

Commit 5e6df33

Browse files
committed
Merge branch 'public_api' into bugs
2 parents bdfa4a1 + c44ab83 commit 5e6df33

14 files changed

+284
-12
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,55 @@
55
*/
66
namespace Magento\CheckoutAgreements\Api;
77

8+
/**
9+
* Interface CheckoutAgreementsRepositoryInterface
10+
* @api
11+
*/
812
interface CheckoutAgreementsRepositoryInterface
913
{
14+
/**
15+
* Return data object for specified checkout agreement ID and store.
16+
*
17+
* @param int $id
18+
* @param int $storeId
19+
* @return \Magento\CheckoutAgreements\Api\Data\AgreementInterface
20+
*/
21+
public function get($id, $storeId = null);
22+
1023
/**
1124
* Lists active checkout agreements.
1225
*
1326
* @return \Magento\CheckoutAgreements\Api\Data\AgreementInterface[]
1427
*/
1528
public function getList();
29+
30+
/**
31+
* Create/Update new checkout agreements with data object values
32+
*
33+
* @param \Magento\CheckoutAgreements\Api\Data\AgreementInterface $data
34+
* @param int $storeId
35+
* @return \Magento\CheckoutAgreements\Api\Data\AgreementInterface
36+
* @throws \Magento\Framework\Exception\CouldNotSaveException If there is a problem with the input
37+
* @throws \Magento\Framework\Exception\NoSuchEntityException If a ID is sent but the entity does not exist
38+
*/
39+
public function save(\Magento\CheckoutAgreements\Api\Data\AgreementInterface $data, $storeId = null);
40+
41+
/**
42+
* Delete checkout agreement
43+
*
44+
* @param \Magento\CheckoutAgreements\Api\Data\AgreementInterface $data
45+
* @return bool
46+
* @throws \Magento\Framework\Exception\CouldNotDeleteException If there is a problem with the input
47+
*/
48+
public function delete(\Magento\CheckoutAgreements\Api\Data\AgreementInterface $data);
49+
50+
/**
51+
* Delete checkout agreement by id
52+
*
53+
* @param int $id
54+
* @return bool
55+
* @throws \Magento\Framework\Exception\NoSuchEntityException If a ID is sent but the entity does not exist
56+
* @throws \Magento\Framework\Exception\CouldNotDeleteException If there is a problem with the input
57+
*/
58+
public function deleteById($id);
1659
}

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

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414
use Magento\Store\Model\StoreManagerInterface;
1515
use Magento\Store\Model\ScopeInterface;
1616
use Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface;
17+
use Magento\CheckoutAgreements\Model\Resource\Agreement as AgreementResource;
18+
use Magento\Framework\Exception\NoSuchEntityException;
19+
use Magento\Store\Model\Store;
1720

1821
/**
1922
* Checkout agreement repository.
23+
*
24+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2025
*/
2126
class CheckoutAgreementsRepository implements CheckoutAgreementsRepositoryInterface
2227
{
@@ -41,21 +46,37 @@ class CheckoutAgreementsRepository implements CheckoutAgreementsRepositoryInterf
4146
*/
4247
private $scopeConfig;
4348

49+
/**
50+
* @var AgreementResource
51+
*/
52+
private $resourceModel;
53+
54+
/**
55+
* @var AgreementFactory
56+
*/
57+
private $agreementFactory;
58+
4459
/**
4560
* Constructs a checkout agreement data object.
4661
*
4762
* @param AgreementCollectionFactory $collectionFactory Collection factory.
4863
* @param \Magento\Store\Model\StoreManagerInterface $storeManager Store manager.
4964
* @param ScopeConfigInterface $scopeConfig Scope config.
65+
* @param AgreementResource $agreementResource
66+
* @param AgreementFactory $agreementFactory
5067
*/
5168
public function __construct(
5269
AgreementCollectionFactory $collectionFactory,
5370
StoreManagerInterface $storeManager,
54-
ScopeConfigInterface $scopeConfig
71+
ScopeConfigInterface $scopeConfig,
72+
AgreementResource $agreementResource,
73+
AgreementFactory $agreementFactory
5574
) {
5675
$this->collectionFactory = $collectionFactory;
5776
$this->storeManager = $storeManager;
5877
$this->scopeConfig = $scopeConfig;
78+
$this->resourceModel = $agreementResource;
79+
$this->agreementFactory = $agreementFactory;
5980
}
6081

6182
/**
@@ -81,4 +102,67 @@ public function getList()
81102

82103
return $agreementDataObjects;
83104
}
105+
106+
/**
107+
* {@inheritdoc}
108+
*/
109+
public function save(\Magento\CheckoutAgreements\Api\Data\AgreementInterface $data, $storeId = null)
110+
{
111+
$id = $data->getAgreementId();
112+
113+
if ($id) {
114+
$data = $this->get($id, $storeId)->addData($data->getData());
115+
}
116+
if ($storeId === null) {
117+
$storeId = $this->storeManager->getStore()->getId();
118+
}
119+
$data->setStores($storeId);
120+
try {
121+
$this->resourceModel->save($data);
122+
} catch (\Exception $e) {
123+
throw new \Magento\Framework\Exception\CouldNotSaveException(
124+
__('Unable to save checkout agreement %1', $data->getAgreementId())
125+
);
126+
}
127+
return $data;
128+
}
129+
130+
/**
131+
* {@inheritdoc}
132+
*/
133+
public function delete(\Magento\CheckoutAgreements\Api\Data\AgreementInterface $data)
134+
{
135+
try {
136+
$this->resourceModel->delete($data);
137+
} catch (\Exception $e) {
138+
throw new \Magento\Framework\Exception\CouldNotDeleteException(
139+
__('Unable to remove checkout agreement %1', $data->getAgreementId())
140+
);
141+
}
142+
return true;
143+
}
144+
145+
/**
146+
* {@inheritdoc}
147+
*/
148+
public function deleteById($id)
149+
{
150+
$model = $this->get($id);
151+
$this->delete($model);
152+
return true;
153+
}
154+
155+
/**
156+
* {@inheritdoc}
157+
*/
158+
public function get($id, $storeId = null)
159+
{
160+
/** @var AgreementFactory $agreement */
161+
$agreement = $this->agreementFactory->create();
162+
$this->resourceModel->load($agreement, $id);
163+
if (!$agreement->getId()) {
164+
throw new NoSuchEntityException(__('Checkout agreement with specified ID "%1" not found.', $id));
165+
}
166+
return $agreement;
167+
}
84168
}

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

Lines changed: 117 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@ class CheckoutAgreementsRepositoryTest extends \PHPUnit_Framework_TestCase
3636
*/
3737
private $objectManager;
3838

39+
/**
40+
* @var \PHPUnit_Framework_MockObject_MockObject
41+
*/
42+
private $resourceMock;
43+
44+
/**
45+
* @var \PHPUnit_Framework_MockObject_MockObject
46+
*/
47+
private $agrFactoryMock;
48+
49+
/**
50+
* @var \PHPUnit_Framework_MockObject_MockObject
51+
*/
52+
private $agreementMock;
53+
54+
/**
55+
* @var \PHPUnit_Framework_MockObject_MockObject
56+
*/
57+
private $storeMock;
58+
3959
protected function setUp()
4060
{
4161
$this->objectManager = new ObjectManager($this);
@@ -49,10 +69,24 @@ protected function setUp()
4969
);
5070
$this->storeManagerMock = $this->getMock('Magento\Store\Model\StoreManagerInterface');
5171
$this->scopeConfigMock = $this->getMock('Magento\Framework\App\Config\ScopeConfigInterface');
72+
$this->resourceMock = $this->getMock('Magento\CheckoutAgreements\Model\Resource\Agreement', [], [], '', false);
73+
$this->agrFactoryMock = $this->getMock(
74+
'Magento\CheckoutAgreements\Model\AgreementFactory',
75+
['create'],
76+
[],
77+
'',
78+
false
79+
);
80+
$methods = ['addData', 'getData', 'setStores', 'getAgreementId', 'getId'];
81+
$this->agreementMock =
82+
$this->getMock('\Magento\CheckoutAgreements\Model\Agreement', $methods, [], '', false);
83+
$this->storeMock = $this->getMock('Magento\Store\Model\Store', [], [], '', false);
5284
$this->model = new \Magento\CheckoutAgreements\Model\CheckoutAgreementsRepository(
5385
$this->factoryMock,
5486
$this->storeManagerMock,
55-
$this->scopeConfigMock
87+
$this->scopeConfigMock,
88+
$this->resourceMock,
89+
$this->agrFactoryMock
5690
);
5791
}
5892

@@ -73,27 +107,99 @@ public function testGetListReturnsTheListOfActiveCheckoutAgreements()
73107
->with('checkout/options/enable_agreements', ScopeInterface::SCOPE_STORE, null)
74108
->will($this->returnValue(true));
75109

76-
$agreementDataObject = $this->getMock(
77-
'Magento\CheckoutAgreements\Model\Agreement',
78-
[],
79-
[],
80-
'',
81-
false
82-
);
83-
84110
$storeId = 1;
85111
$storeMock = $this->getMock('Magento\Store\Model\Store', [], [], '', false);
86112
$storeMock->expects($this->any())->method('getId')->will($this->returnValue($storeId));
87113
$this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($storeMock));
88114

89115
$collectionMock = $this->objectManager->getCollectionMock(
90116
'Magento\CheckoutAgreements\Model\Resource\Agreement\Collection',
91-
[$agreementDataObject]
117+
[$this->agreementMock]
92118
);
93119
$this->factoryMock->expects($this->once())->method('create')->will($this->returnValue($collectionMock));
94120
$collectionMock->expects($this->once())->method('addStoreFilter')->with($storeId);
95121
$collectionMock->expects($this->once())->method('addFieldToFilter')->with('is_active', 1);
96122

97-
$this->assertEquals([$agreementDataObject], $this->model->getList());
123+
$this->assertEquals([$this->agreementMock], $this->model->getList());
124+
}
125+
126+
public function testSave()
127+
{
128+
$this->agreementMock->expects($this->once())->method('getAgreementId')->willReturn(null);
129+
$this->agrFactoryMock->expects($this->never())->method('create');
130+
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
131+
$this->storeMock->expects($this->once())->method('getId')->willReturn('storeId');
132+
$this->agreementMock->expects($this->once())->method('setStores');
133+
$this->resourceMock->expects($this->once())->method('save')->with($this->agreementMock);
134+
$this->model->save($this->agreementMock);
135+
}
136+
137+
public function testUpdate()
138+
{
139+
$agreementId = 1;
140+
$this->agreementMock->expects($this->once())->method('getAgreementId')->willReturn($agreementId);
141+
$this->agrFactoryMock->expects($this->once())->method('create')->willReturn($this->agreementMock);
142+
$this->resourceMock
143+
->expects($this->once())
144+
->method('load')
145+
->with($this->agreementMock, $agreementId);
146+
$this->storeManagerMock->expects($this->never())->method('getStore');
147+
$this->agreementMock->expects($this->once())->method('setStores');
148+
$this->agreementMock->expects($this->once())->method('getId')->willReturn($agreementId);
149+
$this->agreementMock->expects($this->any())->method('getData')->willReturn(['data']);
150+
$this->agreementMock
151+
->expects($this->once())
152+
->method('addData')->with(['data'])
153+
->willReturn($this->agreementMock);
154+
$this->resourceMock->expects($this->once())->method('save')->with($this->agreementMock);
155+
$this->assertEquals($this->agreementMock, $this->model->save($this->agreementMock, 1));
156+
}
157+
158+
/**
159+
* @expectedException \Magento\Framework\Exception\CouldNotSaveException
160+
*/
161+
public function testSaveWithException()
162+
{
163+
$this->agreementMock->expects($this->exactly(2))->method('getAgreementId')->willReturn(null);
164+
$this->agrFactoryMock->expects($this->never())->method('create');
165+
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
166+
$this->storeMock->expects($this->once())->method('getId')->willReturn('storeId');
167+
$this->agreementMock->expects($this->once())->method('setStores');
168+
$this->resourceMock
169+
->expects($this->once())
170+
->method('save')
171+
->with($this->agreementMock)->willThrowException(new \Exception());
172+
$this->model->save($this->agreementMock);
173+
}
174+
175+
public function testDeleteById()
176+
{
177+
$agreementId = 1;
178+
$this->agrFactoryMock->expects($this->once())->method('create')->willReturn($this->agreementMock);
179+
$this->resourceMock
180+
->expects($this->once())
181+
->method('load')
182+
->with($this->agreementMock, $agreementId)
183+
->willReturn($this->agreementMock);
184+
$this->agreementMock->expects($this->once())->method('getId')->willReturn($agreementId);
185+
$this->resourceMock->expects($this->once())->method('delete');
186+
$this->assertTrue($this->model->deleteById(1));
187+
}
188+
189+
/**
190+
* @expectedException \Magento\Framework\Exception\CouldNotDeleteException
191+
*/
192+
public function testDeleteByIdWithException()
193+
{
194+
$agreementId = 1;
195+
$this->agrFactoryMock->expects($this->once())->method('create')->willReturn($this->agreementMock);
196+
$this->resourceMock
197+
->expects($this->once())
198+
->method('load')
199+
->with($this->agreementMock, $agreementId)
200+
->willReturn($this->agreementMock);
201+
$this->agreementMock->expects($this->once())->method('getId')->willReturn($agreementId);
202+
$this->resourceMock->expects($this->once())->method('delete')->willThrowException(new \Exception());
203+
$this->assertTrue($this->model->deleteById(1));
98204
}
99205
}

app/code/Magento/Eav/Api/AttributeGroupRepositoryInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
*/
77
namespace Magento\Eav\Api;
88

9+
/**
10+
* Interface AttributeGroupRepositoryInterface
11+
* @api
12+
*/
913
interface AttributeGroupRepositoryInterface
1014
{
1115
/**

app/code/Magento/Eav/Api/AttributeManagementInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
*/
77
namespace Magento\Eav\Api;
88

9+
/**
10+
* Interface AttributeManagementInterface
11+
* @api
12+
*/
913
interface AttributeManagementInterface
1014
{
1115
/**

app/code/Magento/Eav/Api/AttributeOptionManagementInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
/**
99
* Interface AttributeOptionManagementInterface
10+
* @api
1011
*/
1112
interface AttributeOptionManagementInterface
1213
{

app/code/Magento/Eav/Api/AttributeRepositoryInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
*/
77
namespace Magento\Eav\Api;
88

9+
/**
10+
* Interface AttributeRepositoryInterface
11+
* @api
12+
*/
913
interface AttributeRepositoryInterface
1014
{
1115
/**

app/code/Magento/Eav/Api/AttributeSetManagementInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
*/
77
namespace Magento\Eav\Api;
88

9+
/**
10+
* Interface AttributeSetManagementInterface
11+
* @api
12+
*/
913
interface AttributeSetManagementInterface
1014
{
1115
/**

0 commit comments

Comments
 (0)