Completed
Push — master ( 1742b0...c0cbdc )
by Aimeos
02:15
created

FosUserTest::testSearchItemsTotal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
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), 2015-2016
6
 */
7
8
9
namespace Aimeos\MShop\Customer\Manager;
10
11
12
class FosUserTest extends \PHPUnit_Framework_TestCase
13
{
14
	private $object;
15
	private $fixture;
16
	private $address;
17
	private $editor = '';
18
19
20
	protected function setUp()
21
	{
22
		$context = \TestHelper::getContext();
23
		$this->editor = $context->getEditor();
24
		$this->object = new \Aimeos\MShop\Customer\Manager\FosUser( $context );
25
26
		$this->fixture = array(
27
			'label' => 'unitTest',
28
			'status' => 2,
29
		);
30
31
		$this->address = new \Aimeos\MShop\Common\Item\Address\Standard( 'common.address.' );
32
	}
33
34
35
	protected function tearDown()
36
	{
37
		unset( $this->object, $this->fixture, $this->address );
38
	}
39
40
41
	public function testCleanup()
42
	{
43
		$this->object->cleanup( array( -1 ) );
44
	}
45
46
47
	public function testGetSearchAttributes()
48
	{
49
		foreach( $this->object->getSearchAttributes() as $attribute )
50
		{
51
			$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Attribute\\Iface', $attribute );
52
		}
53
	}
54
55
56
	public function testCreateItem()
57
	{
58
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Customer\\Item\\Iface', $this->object->createItem() );
59
	}
60
61
62
	public function testGetItem()
63
	{
64
		$search = $this->object->createSearch();
65
		$conditions = array(
66
			$search->compare( '==', 'customer.code', 'UTC003' ),
67
			$search->compare( '==', 'customer.editor', $this->editor )
68
		);
69
		$search->setConditions( $search->combine( '&&', $conditions ) );
70
		$items = $this->object->searchItems( $search, array( 'text' ) );
71
72
		if( ( $expected = reset( $items ) ) === false ) {
73
			throw new \RuntimeException( 'No customer item with code "UTC003" found' );
74
		}
75
76
		$actual = $this->object->getItem( $expected->getId(), array( 'text' ) );
77
78
		$this->assertEquals( $expected, $actual );
79
		$this->assertEquals( 3, count( $actual->getListItems( 'text' ) ) );
80
		$this->assertEquals( 3, count( $actual->getRefItems( 'text' ) ) );
81
	}
82
83
84
	public function testSaveUpdateDeleteItem()
85
	{
86
		$item = $this->object->createItem();
87
88
		$item->setCode( 'unitTest' );
89
		$item->setLabel( 'unitTest' );
90
		$this->object->saveItem( $item );
91
		$itemSaved = $this->object->getItem( $item->getId() );
92
93
		$itemExp = clone $itemSaved;
94
		$itemExp->setCode( 'unitTest2' );
95
		$itemExp->setLabel( 'unitTest2' );
96
		$this->object->saveItem( $itemExp );
97
		$itemUpd = $this->object->getItem( $itemExp->getId() );
98
99
		$this->object->deleteItem( $item->getId() );
100
101
		$this->assertTrue( $item->getId() !== null );
102
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
103
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
104
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
105
		$this->assertEquals( $item->getCode(), $itemSaved->getCode() );
106
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
107
		$this->assertEquals( $item->getPaymentAddress(), $itemSaved->getPaymentAddress() );
108
		$this->assertEquals( $item->getBirthday(), $itemSaved->getBirthday() );
109
		$this->assertEquals( $item->getPassword(), $itemSaved->getPassword() );
110
		$this->assertEquals( $item->getRoles(), $itemSaved->getRoles() );
111
		$this->assertEquals( $item->getSalt(), $itemSaved->getSalt() );
112
113
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
114
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
115
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
116
117
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
118
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
119
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
120
		$this->assertEquals( $itemExp->getCode(), $itemUpd->getCode() );
121
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
122
		$this->assertEquals( $itemExp->getPaymentAddress(), $itemUpd->getPaymentAddress() );
123
		$this->assertEquals( $itemExp->getBirthday(), $itemUpd->getBirthday() );
124
		$this->assertEquals( $itemExp->getPassword(), $itemUpd->getPassword() );
125
		$this->assertEquals( $itemExp->getRoles(), $itemUpd->getRoles() );
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MShop\Customer\Item\Iface as the method getRoles() does only exist in the following implementations of said interface: Aimeos\MShop\Customer\Item\FosUser.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
126
		$this->assertEquals( $itemExp->getSalt(), $itemUpd->getSalt() );
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MShop\Customer\Item\Iface as the method getSalt() does only exist in the following implementations of said interface: Aimeos\MShop\Customer\Item\FosUser.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
127
128
		$this->assertEquals( $this->editor, $itemUpd->getEditor() );
129
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
130
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
131
132
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
133
		$this->object->getItem( $item->getId() );
134
	}
135
136
137
	public function testCreateSearch()
138
	{
139
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $this->object->createSearch() );
140
	}
141
142
143
	public function testSearchItems()
144
	{
145
		$total = 0;
146
		$search = $this->object->createSearch();
147
148
		$expr = array();
149
		$expr[] = $search->compare( '!=', 'customer.id', null );
150
		$expr[] = $search->compare( '==', 'customer.label', 'UTC002' );
151
		$expr[] = $search->compare( '==', 'customer.code', 'UTC002' );
152
153
		$expr[] = $search->compare( '>=', 'customer.salutation', '' );
154
		$expr[] = $search->compare( '>=', 'customer.company', '' );
155
		$expr[] = $search->compare( '>=', 'customer.vatid', '' );
156
		$expr[] = $search->compare( '>=', 'customer.title', '' );
157
		$expr[] = $search->compare( '>=', 'customer.firstname', '' );
158
		$expr[] = $search->compare( '>=', 'customer.lastname', '' );
159
		$expr[] = $search->compare( '>=', 'customer.address1', '' );
160
		$expr[] = $search->compare( '>=', 'customer.address2', '' );
161
		$expr[] = $search->compare( '>=', 'customer.address3', '' );
162
		$expr[] = $search->compare( '>=', 'customer.postal', '' );
163
		$expr[] = $search->compare( '>=', 'customer.city', '' );
164
		$expr[] = $search->compare( '>=', 'customer.state', '' );
165
		$expr[] = $search->compare( '!=', 'customer.languageid', null );
166
		$expr[] = $search->compare( '>=', 'customer.countryid', '' );
167
		$expr[] = $search->compare( '>=', 'customer.telephone', '' );
168
		$expr[] = $search->compare( '>=', 'customer.email', '' );
169
		$expr[] = $search->compare( '>=', 'customer.telefax', '' );
170
		$expr[] = $search->compare( '>=', 'customer.website', '' );
171
		$expr[] = $search->compare( '>=', 'customer.longitude', '10.0' );
172
		$expr[] = $search->compare( '>=', 'customer.latitude', '50.0' );
173
174
		$expr[] = $search->compare( '==', 'customer.birthday', '1970-01-01' );
175
		$expr[] = $search->compare( '>=', 'customer.password', '' );
176
		$expr[] = $search->compare( '==', 'customer.status', 0 );
177
		$expr[] = $search->compare( '!=', 'customer.mtime', '1970-01-01 00:00:00' );
178
		$expr[] = $search->compare( '!=', 'customer.ctime', '1970-01-01 00:00:00' );
179
		$expr[] = $search->compare( '==', 'customer.editor', $this->editor );
180
181
		$expr[] = $search->compare( '!=', 'customer.address.id', null );
182
		$expr[] = $search->compare( '!=', 'customer.address.parentid', null );
183
		$expr[] = $search->compare( '==', 'customer.address.company', 'ABC GmbH' );
184
		$expr[] = $search->compare( '==', 'customer.address.vatid', 'DE999999999' );
185
		$expr[] = $search->compare( '==', 'customer.address.salutation', 'mr' );
186
		$expr[] = $search->compare( '==', 'customer.address.title', 'Dr.' );
187
		$expr[] = $search->compare( '==', 'customer.address.firstname', 'Good' );
188
		$expr[] = $search->compare( '==', 'customer.address.lastname', 'Unittest' );
189
		$expr[] = $search->compare( '==', 'customer.address.address1', 'Pickhuben' );
190
		$expr[] = $search->compare( '==', 'customer.address.address2', '2-4' );
191
		$expr[] = $search->compare( '==', 'customer.address.address3', '' );
192
		$expr[] = $search->compare( '==', 'customer.address.postal', '11099' );
193
		$expr[] = $search->compare( '==', 'customer.address.city', 'Berlin' );
194
		$expr[] = $search->compare( '==', 'customer.address.state', 'Berlin' );
195
		$expr[] = $search->compare( '==', 'customer.address.languageid', 'de' );
196
		$expr[] = $search->compare( '==', 'customer.address.countryid', 'DE' );
197
		$expr[] = $search->compare( '==', 'customer.address.telephone', '055544332221' );
198
		$expr[] = $search->compare( '==', 'customer.address.email', '[email protected]' );
199
		$expr[] = $search->compare( '==', 'customer.address.telefax', '055544333212' );
200
		$expr[] = $search->compare( '==', 'customer.address.website', 'unittest.aimeos.org' );
201
		$expr[] = $search->compare( '>=', 'customer.address.longitude', '10.0' );
202
		$expr[] = $search->compare( '>=', 'customer.address.latitude', '50.0' );
203
		$expr[] = $search->compare( '==', 'customer.address.flag', 0 );
204
		$expr[] = $search->compare( '==', 'customer.address.position', 1 );
205
		$expr[] = $search->compare( '!=', 'customer.address.mtime', '1970-01-01 00:00:00' );
206
		$expr[] = $search->compare( '!=', 'customer.address.ctime', '1970-01-01 00:00:00' );
207
		$expr[] = $search->compare( '==', 'customer.address.editor', $this->editor );
208
209
		$search->setConditions( $search->combine( '&&', $expr ) );
210
		$result = $this->object->searchItems( $search, array(), $total );
211
		$this->assertEquals( 1, count( $result ) );
212
	}
213
214
215
	public function testSearchItemsTotal()
216
	{
217
		$total = 0;
218
219
		$search = $this->object->createSearch();
220
		$search->setConditions( $search->compare( '==', 'customer.address.editor', $this->editor ) );
221
		$search->setSlice( 0, 2 );
222
223
		$results = $this->object->searchItems( $search, array(), $total );
224
		$this->assertEquals( 2, count( $results ) );
225
		$this->assertEquals( 3, $total );
226
227
		foreach($results as $itemId => $item) {
228
			$this->assertEquals( $itemId, $item->getId() );
229
		}
230
	}
231
232
233
	public function testSearchItemsCriteria()
234
	{
235
		$search = $this->object->createSearch( true );
236
		$conditions = array(
237
			$search->compare( '==', 'customer.address.editor', $this->editor ),
238
			$search->getConditions()
239
		);
240
		$search->setConditions( $search->combine( '&&', $conditions ) );
241
		$this->assertEquals( 2, count( $this->object->searchItems( $search, array(), $total ) ) );
242
	}
243
244
245
	public function testSearchItemsRef()
246
	{
247
		$search = $this->object->createSearch();
248
		$search->setConditions( $search->compare( '==', 'customer.code', 'UTC001' ) );
249
250
		$results = $this->object->searchItems( $search, ['address', 'text'] );
251
252
		if( ( $item = reset( $results ) ) === false ) {
253
			throw new \Exception( 'No customer item for "UTC001" available' );
254
		}
255
256
		$this->assertEquals( 1, count( $item->getRefItems( 'text' ) ) );
257
		$this->assertEquals( 1, count( $item->getAddressItems() ) );
258
	}
259
260
261
	public function testGetSubManager()
262
	{
263
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'address' ) );
264
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'address', 'Standard' ) );
265
266
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
267
		$this->object->getSubManager( 'unknown' );
268
	}
269
270
271
	public function testGetSubManagerInvalidName()
272
	{
273
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
274
		$this->object->getSubManager( 'address', 'unknown' );
275
	}
276
}
277