Completed
Push — master ( ed1b14...81e2f6 )
by Aimeos
02:53
created

CustomerAddLaravelTestData::cleanupCustomerData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 9
nc 1
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2014
6
 */
7
8
9
namespace Aimeos\MW\Setup\Task;
10
11
12
/**
13
 * Adds Laravel customer test data.
14
 */
15
class CustomerAddLaravelTestData extends \Aimeos\MW\Setup\Task\CustomerAddTestData
16
{
17
	/**
18
	 * Returns the list of task names which this task depends on.
19
	 *
20
	 * @return string[] List of task names
21
	 */
22
	public function getPreDependencies()
23
	{
24
		return array( 'TablesAddLaravelTestData' );
25
	}
26
27
28
	/**
29
	 * Adds attribute test data.
30
	 */
31
	public function migrate()
32
	{
33
		$iface = '\\Aimeos\\MShop\\Context\\Item\\Iface';
34
		if( !( $this->additional instanceof $iface ) ) {
35
			throw new \Aimeos\MW\Setup\Exception( sprintf( 'Additionally provided object is not of type "%1$s"', $iface ) );
36
		}
37
38
		$this->msg( 'Adding Laravel customer test data', 0 );
39
		$this->additional->setEditor( 'ai-laravel:unittest' );
40
41
		$parentIds = array();
42
		$ds = DIRECTORY_SEPARATOR;
43
		$path = __DIR__ . $ds . 'data' . $ds . 'customer.php';
44
45
		if( ( $testdata = include( $path ) ) == false ){
46
			throw new \Aimeos\MShop\Exception( sprintf( 'No file "%1$s" found for customer domain', $path ) );
47
		}
48
49
50
		$customerManager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $this->additional, 'Laravel' );
51
		$customerAddressManager = $customerManager->getSubManager( 'address', 'Laravel' );
52
53
		$this->cleanupCustomerData( $customerManager, $customerAddressManager );
54
55
		$this->conn->begin();
56
57
		$parentIds = $this->addCustomerData( $testdata, $customerManager, $customerAddressManager->createItem() );
58
		$this->addCustomerAddressData( $testdata, $customerAddressManager, $parentIds );
59
60
		$this->conn->commit();
61
62
63
		$this->status( 'done' );
64
	}
65
66
67
	/**
68
	 * Removes all customer unit test entries
69
	 *
70
	 * @param \Aimeos\MShop\Common\Manager\Iface $customerManager Customer manager
71
	 * @param \Aimeos\MShop\Common\Manager\Iface $customerAddressManager Customer address manager
72
	 */
73
	protected function cleanupCustomerData( \Aimeos\MShop\Common\Manager\Iface $customerManager, \Aimeos\MShop\Common\Manager\Iface $customerAddressManager )
74
	{
75
		$search = $customerManager->createSearch();
76
		$search->setConditions( $search->compare( '=~', 'customer.code', 'unitCustomer' ) );
77
		$customerItems = $customerManager->searchItems( $search );
78
79
		$search = $customerAddressManager->createSearch();
80
		$search->setConditions( $search->compare( '=~', 'customer.address.email', 'unitCustomer' ) );
81
		$addressItems = $customerAddressManager->searchItems( $search );
82
83
		$customerAddressManager->deleteItems( array_keys( $addressItems ) );
84
		$customerManager->deleteItems( array_keys( $customerItems ) );
85
	}
86
}
87