Passed
Push — master ( 8f16ed...3311dc )
by Aimeos
10:04
created

lib/mshoplib/setup/unittest/CouponAddTestData.php (2 issues)

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 */
8
9
10
namespace Aimeos\MW\Setup\Task;
11
12
13
/**
14
 * Adds the coupon test data.
15
 */
16
class CouponAddTestData extends \Aimeos\MW\Setup\Task\Base
17
{
18
	/**
19
	 * Returns the list of task names which this task depends on.
20
	 *
21
	 * @return string[] List of task names
22
	 */
23
	public function getPreDependencies()
24
	{
25
		return ['OrderAddTestData'];
26
	}
27
28
29
	/**
30
	 * Adds coupon test data.
31
	 */
32
	public function migrate()
33
	{
34
		$this->msg( 'Adding coupon test data', 0 );
35
36
		$ds = DIRECTORY_SEPARATOR;
37
		$path = __DIR__ . $ds . 'data' . $ds . 'coupon.php';
38
39
		if( ( $testdata = include( $path ) ) == false ) {
40
			throw new \Aimeos\MShop\Exception( sprintf( 'No file "%1$s" found for coupon test data', $path ) );
41
		}
42
43
		$this->addCouponData( $testdata );
44
		$this->addOrderCouponTestData( $testdata );
45
46
		$this->status( 'done' );
47
	}
48
49
50
	/**
51
	 * Adds the coupon test data.
52
	 *
53
	 * @param array $testdata Associative list of key/list pairs
54
	 * @throws \Aimeos\MW\Setup\Exception If a required ID is not available
55
	 */
56
	private function addCouponData( array $testdata )
57
	{
58
		$couponManager = \Aimeos\MShop\Coupon\Manager\Factory::create( $this->additional, 'Standard' );
59
		$couponCodeManager = $couponManager->getSubmanager( 'code' );
60
61
		$couponIds = [];
62
		$coupon = $couponManager->createItem();
63
		foreach( $testdata['coupon'] as $key => $dataset )
64
		{
65
			$coupon->setId( null );
66
			$coupon->setLabel( $dataset['label'] );
67
			$coupon->setProvider( $dataset['provider'] );
68
			$coupon->setDateStart( $dataset['start'] );
69
			$coupon->setDateEnd( $dataset['end'] );
70
			$coupon->setConfig( $dataset['config'] );
71
			$coupon->setStatus( $dataset['status'] );
72
73
			$couponManager->saveItem( $coupon );
74
			$couponIds[$key] = $coupon->getId();
75
		}
76
77
78
		$ccode = $couponCodeManager->createItem();
79
		foreach( $testdata['coupon/code'] as $key => $dataset )
80
		{
81
			if( !isset( $couponIds[$dataset['parentid']] ) ) {
82
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'No coupon ID found for "%1$s"', $dataset['parentid'] ) );
83
			}
84
85
			$ccode->setId( null );
86
			$ccode->setParentId( $couponIds[$dataset['parentid']] );
87
			$ccode->setCount( $dataset['count'] );
88
			$ccode->setDateStart( $dataset['start'] );
89
			$ccode->setDateEnd( $dataset['end'] );
90
			$ccode->setCode( $dataset['code'] );
91
92
			$couponCodeManager->saveItem( $ccode, false );
93
		}
94
	}
95
96
97
	/**
98
	 * Adds the order coupon test data.
99
	 *
100
	 * @param array $testdata Associative list of key/list pairs
101
	 * @throws \Aimeos\MW\Setup\Exception If a required ID is not available
102
	 */
103
	private function addOrderCouponTestData( array $testdata )
104
	{
105
		$order = \Aimeos\MShop\Order\Manager\Factory::create( $this->additional, 'Standard' );
106
		$orderBase = $order->getSubManager( 'base', 'Standard' );
107
		$orderBaseProd = $orderBase->getSubManager( 'product', 'Standard' );
108
		$orderBaseCoupon = $orderBase->getSubManager( 'coupon', 'Standard' );
109
110
		$orderBaseIds = [];
111
		$orderBasePrices = [];
112
		$ordProdIds = [];
113
		$prodcode = $quantity = $pos = [];
114
		foreach( $testdata['order/base/coupon'] as $key => $dataset ) {
115
			$exp = explode( '/', $dataset['ordprodid'] );
116
117
			if( count( $exp ) != 3 ) {
118
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'Some keys for ordprod are set wrong "%1$s"', $dataset ) );
119
			}
120
121
			$prodcode[$exp[0]] = $exp[0];
122
			$quantity[$exp[1]] = $exp[1];
123
			$pos[$exp[2]] = $exp[2];
124
125
			$orderBasePrices[$dataset['baseid']] = $dataset['baseid'];
126
		}
127
128
		$search = $orderBase->createSearch();
129
		$search->setConditions( $search->compare( '==', 'order.base.price', $orderBasePrices ) );
130
131
		foreach( $orderBase->searchItems( $search ) as $orderBaseItem ) {
132
			$orderBaseIds[$orderBaseItem->getPrice()->getValue()] = $orderBaseItem->getId();
133
		}
134
135
136
		$search = $orderBaseProd->createSearch();
137
		$expr = array(
138
			$search->compare( '==', 'order.base.product.prodcode', $prodcode ),
139
			$search->compare( '==', 'order.base.product.quantity', $quantity ),
140
			$search->compare( '==', 'order.base.product.position', $pos ),
141
		);
142
		$search->setConditions( $search->combine( '&&', $expr ) );
143
144
		foreach( $orderBaseProd->searchItems( $search ) as $ordProd ) {
145
			$ordProdIds[$ordProd->getProductCode() . '/' . $ordProd->getQuantity() . '/' . $ordProd->getPosition()] = $ordProd->getId();
146
		}
147
148
		$orderCoupon = $orderBaseCoupon->createItem();
149
		foreach( $testdata['order/base/coupon'] as $key => $dataset )
150
		{
151
			if( !isset( $orderBaseIds[$dataset['baseid']] ) ) {
152
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'No oder base ID found for "%1$s"', $dataset['baseid'] ) );
153
			}
154
155
			if( !isset( $ordProdIds[$dataset['ordprodid']] ) ) {
156
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'No order base product ID found for "%1$s"', $dataset['ordprodid'] ) );
157
			}
158
159
			$orderCoupon->setId( null );
160
			$orderCoupon->setBaseId( $orderBaseIds[$dataset['baseid']] );
0 ignored issues
show
The method setBaseId() does not exist on Aimeos\MShop\Attribute\Item\Iface. ( Ignorable by Annotation )

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

160
			$orderCoupon->/** @scrutinizer ignore-call */ 
161
                 setBaseId( $orderBaseIds[$dataset['baseid']] );

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...
161
			$orderCoupon->setProductId( $ordProdIds[$dataset['ordprodid']] );
0 ignored issues
show
The method setProductId() does not exist on Aimeos\MShop\Attribute\Item\Iface. ( Ignorable by Annotation )

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

161
			$orderCoupon->/** @scrutinizer ignore-call */ 
162
                 setProductId( $ordProdIds[$dataset['ordprodid']] );

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...
162
			$orderCoupon->setCode( $dataset['code'] );
163
164
			$orderBaseCoupon->saveItem( $orderCoupon, false );
165
		}
166
	}
167
}
168