Issues (3641)

Zed/Merchant/_support/MerchantBusinessTester.php (2 issues)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerTest\Zed\Merchant;
9
10
use ArrayObject;
11
use Codeception\Actor;
12
use Generated\Shared\DataBuilder\MerchantBuilder;
13
use Generated\Shared\DataBuilder\StoreRelationBuilder;
14
use Generated\Shared\Transfer\EventEntityTransfer;
15
use Generated\Shared\Transfer\MerchantCriteriaTransfer;
16
use Generated\Shared\Transfer\MerchantTransfer;
17
use Generated\Shared\Transfer\PriceProductMerchantRelationshipStorageTransfer;
18
use Generated\Shared\Transfer\PriceProductMerchantRelationshipValueTransfer;
19
use Generated\Shared\Transfer\StoreRelationTransfer;
20
use Generated\Shared\Transfer\StoreTransfer;
21
use Orm\Zed\Merchant\Persistence\SpyMerchantQuery;
22
use PHPUnit\Framework\Constraint\Callback;
23
use PHPUnit\Framework\MockObject\Rule\InvokedAtLeastOnce as InvokedAtLeastOnceMatcher;
24
use PHPUnit\Framework\MockObject\Rule\InvokedCount as InvokedCountMatcher;
25
use Spryker\Zed\Event\Business\EventFacadeInterface;
26
use Spryker\Zed\Merchant\Dependency\MerchantEvents;
27
use Spryker\Zed\Store\Business\StoreFacadeInterface;
28
29
/**
30
 * @method void wantToTest($text)
31
 * @method void wantTo($text)
32
 * @method void execute($callable)
33
 * @method void expectTo($prediction)
34
 * @method void expect($prediction)
35
 * @method void amGoingTo($argumentation)
36
 * @method void am($role)
37
 * @method void lookForwardTo($achieveValue)
38
 * @method void comment($description)
39
 * @method \Codeception\Lib\Friend haveFriend($name, $actorClass = null)
40
 * @method \Spryker\Zed\Merchant\Business\MerchantFacadeInterface getFacade()
41
 *
42
 * @SuppressWarnings(\SprykerTest\Zed\Merchant\PHPMD)
43
 */
44
class MerchantBusinessTester extends Actor
45
{
46
    use _generated\MerchantBusinessTesterActions;
47
48
    /**
49
     * @return void
50
     */
51
    public function truncateMerchantRelations(): void
52
    {
53
        $this->truncateTableRelations($this->getMerchantQuery());
54
    }
55
56
    /**
57
     * @param int|null $merchantId
58
     *
59
     * @return \Generated\Shared\Transfer\MerchantTransfer
60
     */
61
    public function createMerchantTransfer(?int $merchantId = null): MerchantTransfer
62
    {
63
        return (new MerchantBuilder())
64
            ->build()
65
            ->setIdMerchant($merchantId)
66
            ->setStoreRelation((new StoreRelationBuilder())->build());
67
    }
68
69
    /**
70
     * @param int $merchantId
71
     * @param int $price
72
     *
73
     * @return \Generated\Shared\Transfer\PriceProductMerchantRelationshipStorageTransfer
74
     */
75
    public function createPriceProductMerchantRelationshipStorageTransfer(int $merchantId, int $price): PriceProductMerchantRelationshipStorageTransfer
76
    {
77
        $ungroupedPrices = new ArrayObject();
78
        $priceProductMerchantRelationshipValueTransfer = (new PriceProductMerchantRelationshipValueTransfer())
79
            ->setGrossPrice($price)
80
            ->setNetPrice($price)
81
            ->setFkMerchant($merchantId);
82
        $ungroupedPrices->append($priceProductMerchantRelationshipValueTransfer);
83
84
        return (new PriceProductMerchantRelationshipStorageTransfer())
85
            ->setUngroupedPrices($ungroupedPrices);
86
    }
87
88
    /**
89
     * @return \Orm\Zed\Merchant\Persistence\SpyMerchantQuery
90
     */
91
    protected function getMerchantQuery(): SpyMerchantQuery
92
    {
93
        return SpyMerchantQuery::create();
94
    }
95
96
    /**
97
     * @param int $numberOfInvokations
98
     *
99
     * @return \PHPUnit\Framework\MockObject\Rule\InvokedCount
100
     */
101
    protected function exactly(int $numberOfInvokations): InvokedCountMatcher
102
    {
103
        return new InvokedCountMatcher($numberOfInvokations);
104
    }
105
106
    /**
107
     * @return \PHPUnit\Framework\MockObject\Rule\InvokedAtLeastOnce
108
     */
109
    public static function atLeastOnce(): InvokedAtLeastOnceMatcher
110
    {
111
        return new InvokedAtLeastOnceMatcher();
112
    }
113
114
    /**
115
     * @param callable $callback
116
     *
117
     * @return \PHPUnit\Framework\Constraint\Callback
118
     */
119
    protected static function callback(callable $callback): Callback
120
    {
121
        return new Callback($callback);
122
    }
123
124
    /**
125
     * @return \Spryker\Zed\Store\Business\StoreFacadeInterface
126
     */
127
    public function getStoreFacade(): StoreFacadeInterface
128
    {
129
        return $this->getLocator()->store()->facade();
130
    }
131
132
    /**
133
     * @return \Generated\Shared\Transfer\MerchantTransfer
134
     */
135
    public function haveMerchantWithStore(): MerchantTransfer
136
    {
137
        $storeRelationTransfer = $this->getStoreRelationTransfer();
138
139
        $merchantTransfer = $this->haveMerchant([MerchantTransfer::STORE_RELATION => $storeRelationTransfer->toArray()]);
140
141
        return $this->getFacade()->findOne((new MerchantCriteriaTransfer())->setIdMerchant($merchantTransfer->getIdMerchant()));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getFacade(...sfer->getIdMerchant())) could return the type null which is incompatible with the type-hinted return Generated\Shared\Transfer\MerchantTransfer. Consider adding an additional type-check to rule them out.
Loading history...
142
    }
143
144
    /**
145
     * @return \Generated\Shared\Transfer\MerchantTransfer
146
     */
147
    public function getMerchantTransferWithStore(): MerchantTransfer
148
    {
149
        $storeRelationTransfer = $this->getStoreRelationTransfer();
150
151
        return $this->getMerchantTransfer([MerchantTransfer::STORE_RELATION => $storeRelationTransfer->toArray()]);
152
    }
153
154
    /**
155
     * @param \Spryker\Zed\Event\Business\EventFacadeInterface $eventFacade
156
     * @param \Generated\Shared\Transfer\MerchantTransfer $merchantTransfer
157
     *
158
     * @return void
159
     */
160
    public function assertTriggerMerchantExportEventsSuccessfully(EventFacadeInterface $eventFacade, MerchantTransfer $merchantTransfer): void
161
    {
162
        $merchantStore = $merchantTransfer->getStoreRelation()->getStores()->offsetGet(0);
163
        $merchantCriteriaTransfer = (new MerchantCriteriaTransfer())->setStore($merchantStore);
164
        $merchantCollectionTransfer = $this->getFacade()->get($merchantCriteriaTransfer);
165
166
        $eventFacade->expects($this->atLeastOnce())
0 ignored issues
show
The method expects() does not exist on Spryker\Zed\Event\Business\EventFacadeInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

166
        $eventFacade->/** @scrutinizer ignore-call */ 
167
                      expects($this->atLeastOnce())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
167
            ->method('triggerBulk')
168
            ->with(MerchantEvents::MERCHANT_EXPORTED, $this->callback(
169
                function ($transfers) use ($merchantCollectionTransfer) {
170
                    $this->assertNotEmpty($transfers);
171
                    $this->assertInstanceOf(EventEntityTransfer::class, $transfers[0]);
172
                    $this->assertCount($merchantCollectionTransfer->getMerchants()->count(), $transfers);
173
174
                    return true;
175
                },
176
            ));
177
    }
178
179
    /**
180
     * @param \Spryker\Zed\Event\Business\EventFacadeInterface $eventFacade
181
     * @param \Generated\Shared\Transfer\MerchantTransfer $merchantTransfer
182
     *
183
     * @return void
184
     */
185
    public function assertTriggerCreatedAndPublishEvent(EventFacadeInterface $eventFacade, MerchantTransfer $merchantTransfer): void
186
    {
187
        $eventFacade
188
            ->expects($this->exactly(2))
189
            ->method('trigger')
190
            ->withConsecutive(
191
                [MerchantEvents::MERCHANT_CREATED, $this->callback(function ($eventEntityTransfer) use ($merchantTransfer) {
192
                    $this->assertNotEmpty($eventEntityTransfer);
193
                    $this->assertEquals($eventEntityTransfer->getId(), $merchantTransfer->getIdMerchant());
194
195
                    return true;
196
                })],
197
                [MerchantEvents::MERCHANT_PUBLISH, $this->callback(function ($eventEntityTransfer) use ($merchantTransfer) {
198
                    $this->assertNotEmpty($eventEntityTransfer);
199
                    $this->assertEquals($eventEntityTransfer->getId(), $merchantTransfer->getIdMerchant());
200
201
                    return true;
202
                })],
203
            );
204
    }
205
206
    /**
207
     * @param \Spryker\Zed\Event\Business\EventFacadeInterface $eventFacade
208
     * @param \Generated\Shared\Transfer\MerchantTransfer $merchantTransfer
209
     *
210
     * @return void
211
     */
212
    public function assertTriggerUpdatedAndPublishEvent(EventFacadeInterface $eventFacade, MerchantTransfer $merchantTransfer): void
213
    {
214
        $eventFacade
215
            ->expects($this->atLeastOnce())
216
            ->method('trigger')
217
            ->withConsecutive(
218
                [MerchantEvents::MERCHANT_UPDATED, $this->callback(function ($eventEntityTransfer) use ($merchantTransfer) {
219
                    $this->assertNotEmpty($eventEntityTransfer);
220
                    $this->assertEquals($eventEntityTransfer->getId(), $merchantTransfer->getIdMerchant());
221
222
                    return true;
223
                })],
224
                [MerchantEvents::MERCHANT_PUBLISH, $this->callback(function ($eventEntityTransfer) use ($merchantTransfer) {
225
                    $this->assertNotEmpty($eventEntityTransfer);
226
                    $this->assertEquals($eventEntityTransfer->getId(), $merchantTransfer->getIdMerchant());
227
228
                    return true;
229
                })],
230
            );
231
    }
232
233
    /**
234
     * @param \Spryker\Zed\Event\Business\EventFacadeInterface $eventFacade
235
     * @param \Generated\Shared\Transfer\MerchantTransfer $merchantTransfer
236
     *
237
     * @return void
238
     */
239
    public function assertTriggerOnlyPublishEvent(EventFacadeInterface $eventFacade, MerchantTransfer $merchantTransfer): void
240
    {
241
        $eventFacade
242
            ->expects($this->exactly(1))
243
            ->method('trigger')
244
            ->with(MerchantEvents::MERCHANT_PUBLISH, $this->callback(function ($eventEntityTransfer) use ($merchantTransfer) {
245
                $this->assertNotEmpty($eventEntityTransfer);
246
                $this->assertEquals($eventEntityTransfer->getId(), $merchantTransfer->getIdMerchant());
247
248
                return true;
249
            }));
250
    }
251
252
    /**
253
     * @return \Generated\Shared\Transfer\StoreRelationTransfer
254
     */
255
    public function getStoreRelationTransfer(): StoreRelationTransfer
256
    {
257
        $storeTransfer = $this->haveStore([StoreTransfer::NAME => 'DE']);
258
        $storeRelationTransfer = (new StoreRelationBuilder())->seed([
259
            StoreRelationTransfer::ID_STORES => [$storeTransfer->getIdStore()],
260
        ])->build();
261
262
        return $storeRelationTransfer;
263
    }
264
265
    /**
266
     * @return array
267
     */
268
    public function getStoreReferences(): array
269
    {
270
        $storeReferences = [];
271
272
        foreach ($this->getStoreFacade()->getAllStores() as $storeTransfer) {
273
            if ($storeTransfer->getStoreReference()) {
274
                $storeReferences[] = $storeTransfer->getStoreReference();
275
            }
276
        }
277
278
        return $storeReferences;
279
    }
280
}
281