Completed
Push — master ( 5e3755...9505fd )
by Aimeos
02:12
created

StandardTest::testGetListItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017
6
 */
7
8
9
namespace Aimeos\Controller\Frontend\Customer;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $object;
16
17
18
	protected function setUp()
19
	{
20
		$this->context = \TestHelperFrontend::getContext();
21
		$this->object = new \Aimeos\Controller\Frontend\Customer\Standard( $this->context );
22
	}
23
24
25
	protected function tearDown()
26
	{
27
		unset( $this->context, $this->object );
28
	}
29
30
31
	public function testAddEditSaveDeleteItem()
32
	{
33
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' );
34
		$id = $manager->findItem( 'UTC001' )->getId();
35
36
		$this->context->setUserId( $id );
37
		$item = $this->object->addItem( ['customer.code' => 'unittest-ctnl', 'customer.status' => 1] );
38
		$this->assertInstanceOf( '\Aimeos\MShop\Customer\Item\Iface', $item );
39
40
		$this->context->setUserId( $item->getId() );
41
		$item = $this->object->editItem( $item->getId(), ['customer.code' => 'unittest-ctnl2'] );
42
		$this->assertInstanceOf( '\Aimeos\MShop\Customer\Item\Iface', $item );
43
44
		$item->setStatus( 0 );
45
		$item = $this->object->saveItem( $item );
0 ignored issues
show
Bug introduced by
It seems like $item defined by $this->object->saveItem($item) on line 45 can be null; however, Aimeos\Controller\Fronte...er\Standard::saveItem() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
46
		$this->assertEquals( 0, $item->getStatus() );
0 ignored issues
show
Bug introduced by
The method getStatus() does not seem to exist on object<Aimeos\MShop\Customer\Item\Address\Iface>.

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...
47
48
		$this->object->deleteItem( $item->getId() );
49
50
		$this->setExpectedException( '\Aimeos\MShop\Exception' );
51
		$manager->findItem( 'unittest-ctnl' );
52
	}
53
54
55
	public function testAddExistingItem()
56
	{
57
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' );
58
		$id = $manager->findItem( 'UTC001' )->getId();
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
59
60
		$item = $this->object->addItem( ['customer.code' => 'UTC001'] );
61
		$this->assertInstanceOf( '\Aimeos\MShop\Customer\Item\Iface', $item );
62
	}
63
64
65
	public function testCreateItem()
66
	{
67
		$result = $this->object->createItem();
68
		$this->assertInstanceOf( '\Aimeos\MShop\Customer\Item\Iface', $result );
69
	}
70
71
72
	public function testGetItem()
73
	{
74
		$id = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' )->findItem( 'UTC001' )->getId();
75
		$this->context->setUserId( $id );
76
77
		$result = $this->object->getItem( $id, ['customer/address', 'text'] );
78
79
		$this->assertInstanceOf( '\Aimeos\MShop\Customer\Item\Iface', $result );
80
		$this->assertEquals( 1, count( $result->getRefItems( 'text' ) ) );
81
		$this->assertEquals( 1, count( $result->getAddressItems() ) );
82
	}
83
84
85
	public function testFindItem()
86
	{
87
		$result = $this->object->findItem( 'UTC001' );
88
		$this->assertInstanceOf( '\Aimeos\MShop\Customer\Item\Iface', $result );
89
	}
90
91
92
	public function testAddEditSaveDeleteAddressItem()
93
	{
94
		$customer = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' )->findItem( 'UTC001' );
95
		$this->context->setUserId( $customer->getId() );
96
97
		$item = $this->object->addAddressItem( ['customer.address.lastname' => 'unittest-ctnl'] );
98
		$this->assertInstanceOf( '\Aimeos\MShop\Customer\Item\Address\Iface', $item );
99
100
		$item = $this->object->editAddressItem( $item->getId(), ['customer.address.lastname' => 'unittest-ctnl2'] );
101
		$this->assertInstanceOf( '\Aimeos\MShop\Customer\Item\Address\Iface', $item );
102
103
		$item->setLastName( 'test' );
104
		$this->object->saveAddressItem( $item );
0 ignored issues
show
Bug introduced by
It seems like $item defined by $this->object->editAddre...' => 'unittest-ctnl2')) on line 100 can be null; however, Aimeos\Controller\Fronte...dard::saveAddressItem() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
105
		$this->assertEquals( 'test', $item->getLastName() );
106
107
		$this->object->deleteAddressItem( $item->getId() );
108
	}
109
110
111
	public function testCreateAddressItem()
112
	{
113
		$result = $this->object->createAddressItem();
114
		$this->assertInstanceOf( '\Aimeos\MShop\Customer\Item\Address\Iface', $result );
115
	}
116
117
118
	public function testGetAddressItem()
119
	{
120
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer/address' );
121
		$search = $manager->createSearch();
122
		$search->setSlice( 0, 1 );
123
		$result = $manager->searchItems( $search );
124
125
		if( ( $item = reset( $result ) ) === false ) {
126
			throw new \RuntimeException( 'No customer address available' );
127
		}
128
129
		$this->context->setUserId( $item->getParentId() );
130
		$result = $this->object->getAddressItem( $item->getId() );
131
		$this->assertInstanceOf( '\Aimeos\MShop\Customer\Item\Address\Iface', $result );
132
	}
133
134
135
	public function testAddEditDeleteListItem()
136
	{
137
		$customer = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' )->findItem( 'UTC001' );
138
		$this->context->setUserId( $customer->getId() );
139
140
		$values = [
141
			'customer.lists.type' => 'favorite',
142
			'customer.lists.domain' => 'product',
143
			'customer.lists.refid' => '-1'
144
		];
145
146
		$item = $this->object->addListItem( $values );
147
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Lists\Iface', $item );
148
149
		$values = [
150
			'customer.lists.type' => 'favorite',
151
			'customer.lists.domain' => 'product',
152
			'customer.lists.refid' => '-2'
153
		];
154
155
		$item = $this->object->editListItem( $item->getId(), $values );
156
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Lists\Iface', $item );
157
158
		$this->object->deleteListItem( $item->getId() );
159
160
		$this->setExpectedException( '\Aimeos\MShop\Exception' );
161
		$this->object->getListItem( $item->getId() );
162
	}
163
164
165
	public function testGetListItem()
166
	{
167
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer/lists' );
168
		$search = $manager->createSearch();
169
		$search->setSlice( 0, 1 );
170
		$result = $manager->searchItems( $search );
171
172
		if( ( $item = reset( $result ) ) === false ) {
173
			throw new \RuntimeException( 'No customer lists item available' );
174
		}
175
176
		$this->context->setUserId( $item->getParentId() );
177
		$result = $this->object->getListItem( $item->getId() );
178
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Lists\Iface', $result );
179
	}
180
181
182
	public function testSearchListItem()
183
	{
184
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' );
185
		$customer = $manager->findItem( 'UTC001' );
186
		$this->context->setUserId( $customer->getId() );
187
188
		$filter = $this->object->createListsFilter();
189
		$result = $this->object->searchListItems( $filter );
190
191
		foreach( $result as $item )
192
		{
193
			$this->assertEquals( $customer->getId(), $item->getParentId() );
194
			$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Lists\Iface', $item );
195
		}
196
	}
197
}
198