|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TddWizard\Fixtures\Checkout; |
|
4
|
|
|
|
|
5
|
|
|
use Magento\Catalog\Api\ProductRepositoryInterface; |
|
6
|
|
|
use Magento\Catalog\Model\Product; |
|
7
|
|
|
use Magento\Checkout\Model\Cart; |
|
8
|
|
|
use Magento\Framework\DataObject; |
|
9
|
|
|
use Magento\Framework\ObjectManagerInterface; |
|
10
|
|
|
use Magento\TestFramework\Helper\Bootstrap; |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class CartBuilder |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var ProductRepositoryInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $productRepository; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Cart |
|
21
|
|
|
*/ |
|
22
|
|
|
private $cart; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var DataObject[][] Array in the form [sku => [buyRequest]] (multiple requests per sku are possible) |
|
26
|
|
|
*/ |
|
27
|
|
|
private $addToCartRequests; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(ProductRepositoryInterface $productRepository, Cart $cart) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->productRepository = $productRepository; |
|
32
|
|
|
$this->cart = $cart; |
|
33
|
|
|
$this->addToCartRequests = []; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function forCurrentSession(ObjectManagerInterface $objectManager = null) |
|
37
|
|
|
{ |
|
38
|
|
|
if ($objectManager === null) { |
|
39
|
|
|
$objectManager = Bootstrap::getObjectManager(); |
|
40
|
|
|
} |
|
41
|
|
|
return new static( |
|
42
|
|
|
$objectManager->create(ProductRepositoryInterface::class), |
|
43
|
|
|
$objectManager->create(Cart::class) |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function withSimpleProduct($sku, $qty = 1) : CartBuilder |
|
48
|
|
|
{ |
|
49
|
|
|
$result = clone $this; |
|
50
|
|
|
$result->addToCartRequests[$sku][] = new DataObject(['qty' => $qty]); |
|
51
|
|
|
return $result; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Lower-level API to support arbitary products |
|
56
|
|
|
* |
|
57
|
|
|
* @param $sku |
|
58
|
|
|
* @param int $qty |
|
59
|
|
|
* @param $request |
|
60
|
|
|
* @return CartBuilder |
|
61
|
|
|
*/ |
|
62
|
|
|
public function withProductRequest($sku, $qty = 1, $request = []) : CartBuilder |
|
63
|
|
|
{ |
|
64
|
|
|
$result = clone $this; |
|
65
|
|
|
$requestInfo = array_merge(['qty'=> $qty], $request); |
|
66
|
|
|
$result->addToCartRequests[$sku][] = new DataObject($requestInfo); |
|
67
|
|
|
return $result; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function build() : Cart |
|
71
|
|
|
{ |
|
72
|
|
|
foreach ($this->addToCartRequests as $sku => $requests) { |
|
73
|
|
|
/** @var $product \Magento\Catalog\Model\Product */ |
|
74
|
|
|
$product = $this->productRepository->get($sku); |
|
75
|
|
|
foreach ($requests as $requestInfo) { |
|
76
|
|
|
$this->cart->addProduct($product, $requestInfo); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
$this->cart->save(); |
|
80
|
|
|
return $this->cart; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths