Completed
Push — master ( 201bf3...094a7b )
by Aimeos
02:16
created

FosUserTest::testGetSaveAddressItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2017
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
		$resultSaved = $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
		$resultUpd = $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->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultSaved );
133
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultUpd );
134
135
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
136
		$this->object->getItem( $item->getId() );
137
	}
138
139
140
	public function testGetSaveAddressItems()
141
	{
142
		$item = $this->object->findItem( 'UTC001', ['customer/address'] );
143
144
		$item->setId( null )->setCode( 'xyz' );
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MShop\Common\Item\Iface as the method setCode() does only exist in the following implementations of said interface: Aimeos\MShop\Attribute\Item\Standard, Aimeos\MShop\Catalog\Item\Standard, Aimeos\MShop\Common\Item\Type\Standard, Aimeos\MShop\Coupon\Item\Code\Standard, Aimeos\MShop\Customer\Item\Base, Aimeos\MShop\Customer\Item\FosUser, Aimeos\MShop\Customer\Item\Group\Standard, Aimeos\MShop\Customer\Item\Standard, Aimeos\MShop\Locale\Item\Currency\Standard, Aimeos\MShop\Locale\Item\Language\Standard, Aimeos\MShop\Locale\Item\Site\Standard, Aimeos\MShop\Order\Item\Base\Coupon\Standard, Aimeos\MShop\Order\Item\...duct\Attribute\Standard, Aimeos\MShop\Order\Item\...vice\Attribute\Standard, Aimeos\MShop\Order\Item\Base\Service\Base, Aimeos\MShop\Order\Item\Base\Service\Standard, Aimeos\MShop\Product\Item\Standard, Aimeos\MShop\Service\Item\Standard, Aimeos\MShop\Supplier\Item\Standard.

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...
145
		$item->getPaymentAddress()->setEmail( '[email protected]' );
146
		$item->addAddressItem( new \Aimeos\MShop\Common\Item\Address\Standard( 'customer.address.' ) );
0 ignored issues
show
Bug introduced by
The method addAddressItem() does not seem to exist on object<Aimeos\MShop\Common\Item\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...
147
		$this->object->saveItem( $item );
148
149
		$item2 = $this->object->findItem( 'xyz', ['customer/address'] );
150
151
		$this->object->deleteItem( $item->getId() );
152
153
		$this->assertEquals( 2, count( $item->getAddressItems() ) );
154
		$this->assertEquals( 2, count( $item2->getAddressItems() ) );
155
	}
156
157
158
	public function testGetSavePropertyItems()
159
	{
160
		$item = $this->object->findItem( 'UTC001', ['customer/property'] );
161
162
		$item->setId( null )->setCode( 'xyz' );
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MShop\Common\Item\Iface as the method setCode() does only exist in the following implementations of said interface: Aimeos\MShop\Attribute\Item\Standard, Aimeos\MShop\Catalog\Item\Standard, Aimeos\MShop\Common\Item\Type\Standard, Aimeos\MShop\Coupon\Item\Code\Standard, Aimeos\MShop\Customer\Item\Base, Aimeos\MShop\Customer\Item\FosUser, Aimeos\MShop\Customer\Item\Group\Standard, Aimeos\MShop\Customer\Item\Standard, Aimeos\MShop\Locale\Item\Currency\Standard, Aimeos\MShop\Locale\Item\Language\Standard, Aimeos\MShop\Locale\Item\Site\Standard, Aimeos\MShop\Order\Item\Base\Coupon\Standard, Aimeos\MShop\Order\Item\...duct\Attribute\Standard, Aimeos\MShop\Order\Item\...vice\Attribute\Standard, Aimeos\MShop\Order\Item\Base\Service\Base, Aimeos\MShop\Order\Item\Base\Service\Standard, Aimeos\MShop\Product\Item\Standard, Aimeos\MShop\Service\Item\Standard, Aimeos\MShop\Supplier\Item\Standard.

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...
163
		$item->getPaymentAddress()->setEmail( '[email protected]' );
164
		$this->object->saveItem( $item );
165
166
		$item2 = $this->object->findItem( 'xyz', ['customer/property'] );
167
168
		$this->object->deleteItem( $item->getId() );
169
170
		$this->assertEquals( 1, count( $item->getPropertyItems() ) );
171
		$this->assertEquals( 1, count( $item2->getPropertyItems() ) );
172
	}
173
174
175
	public function testCreateSearch()
176
	{
177
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $this->object->createSearch() );
178
	}
179
180
181
	public function testSearchItems()
182
	{
183
		$total = 0;
184
		$search = $this->object->createSearch();
185
186
		$expr = [];
187
		$expr[] = $search->compare( '!=', 'customer.id', null );
188
		$expr[] = $search->compare( '==', 'customer.label', 'UTC002' );
189
		$expr[] = $search->compare( '==', 'customer.code', 'UTC002' );
190
191
		$expr[] = $search->compare( '>=', 'customer.salutation', '' );
192
		$expr[] = $search->compare( '>=', 'customer.company', '' );
193
		$expr[] = $search->compare( '>=', 'customer.vatid', '' );
194
		$expr[] = $search->compare( '>=', 'customer.title', '' );
195
		$expr[] = $search->compare( '>=', 'customer.firstname', '' );
196
		$expr[] = $search->compare( '>=', 'customer.lastname', '' );
197
		$expr[] = $search->compare( '>=', 'customer.address1', '' );
198
		$expr[] = $search->compare( '>=', 'customer.address2', '' );
199
		$expr[] = $search->compare( '>=', 'customer.address3', '' );
200
		$expr[] = $search->compare( '>=', 'customer.postal', '' );
201
		$expr[] = $search->compare( '>=', 'customer.city', '' );
202
		$expr[] = $search->compare( '>=', 'customer.state', '' );
203
		$expr[] = $search->compare( '!=', 'customer.languageid', null );
204
		$expr[] = $search->compare( '>=', 'customer.countryid', '' );
205
		$expr[] = $search->compare( '>=', 'customer.telephone', '' );
206
		$expr[] = $search->compare( '>=', 'customer.email', '' );
207
		$expr[] = $search->compare( '>=', 'customer.telefax', '' );
208
		$expr[] = $search->compare( '>=', 'customer.website', '' );
209
		$expr[] = $search->compare( '>=', 'customer.longitude', '10.0' );
210
		$expr[] = $search->compare( '>=', 'customer.latitude', '50.0' );
211
212
		$expr[] = $search->compare( '==', 'customer.birthday', '1970-01-01' );
213
		$expr[] = $search->compare( '>=', 'customer.password', '' );
214
		$expr[] = $search->compare( '==', 'customer.status', 0 );
215
		$expr[] = $search->compare( '!=', 'customer.mtime', '1970-01-01 00:00:00' );
216
		$expr[] = $search->compare( '!=', 'customer.ctime', '1970-01-01 00:00:00' );
217
		$expr[] = $search->compare( '==', 'customer.editor', $this->editor );
218
219
		$expr[] = $search->compare( '!=', 'customer.address.id', null );
220
		$expr[] = $search->compare( '!=', 'customer.address.parentid', null );
221
		$expr[] = $search->compare( '==', 'customer.address.company', 'ABC GmbH' );
222
		$expr[] = $search->compare( '==', 'customer.address.vatid', 'DE999999999' );
223
		$expr[] = $search->compare( '==', 'customer.address.salutation', 'mr' );
224
		$expr[] = $search->compare( '==', 'customer.address.title', 'Dr.' );
225
		$expr[] = $search->compare( '==', 'customer.address.firstname', 'Good' );
226
		$expr[] = $search->compare( '==', 'customer.address.lastname', 'Unittest' );
227
		$expr[] = $search->compare( '==', 'customer.address.address1', 'Pickhuben' );
228
		$expr[] = $search->compare( '==', 'customer.address.address2', '2-4' );
229
		$expr[] = $search->compare( '==', 'customer.address.address3', '' );
230
		$expr[] = $search->compare( '==', 'customer.address.postal', '11099' );
231
		$expr[] = $search->compare( '==', 'customer.address.city', 'Berlin' );
232
		$expr[] = $search->compare( '==', 'customer.address.state', 'Berlin' );
233
		$expr[] = $search->compare( '==', 'customer.address.languageid', 'de' );
234
		$expr[] = $search->compare( '==', 'customer.address.countryid', 'DE' );
235
		$expr[] = $search->compare( '==', 'customer.address.telephone', '055544332221' );
236
		$expr[] = $search->compare( '==', 'customer.address.email', '[email protected]' );
237
		$expr[] = $search->compare( '==', 'customer.address.telefax', '055544333212' );
238
		$expr[] = $search->compare( '==', 'customer.address.website', 'unittest.aimeos.org' );
239
		$expr[] = $search->compare( '>=', 'customer.address.longitude', '10.0' );
240
		$expr[] = $search->compare( '>=', 'customer.address.latitude', '50.0' );
241
		$expr[] = $search->compare( '==', 'customer.address.flag', 0 );
242
		$expr[] = $search->compare( '==', 'customer.address.position', 1 );
243
		$expr[] = $search->compare( '!=', 'customer.address.mtime', '1970-01-01 00:00:00' );
244
		$expr[] = $search->compare( '!=', 'customer.address.ctime', '1970-01-01 00:00:00' );
245
		$expr[] = $search->compare( '==', 'customer.address.editor', $this->editor );
246
247
		$search->setConditions( $search->combine( '&&', $expr ) );
248
		$result = $this->object->searchItems( $search, [], $total );
249
		$this->assertEquals( 1, count( $result ) );
250
	}
251
252
253
	public function testSearchItemsTotal()
254
	{
255
		$total = 0;
256
257
		$search = $this->object->createSearch();
258
		$search->setConditions( $search->compare( '==', 'customer.address.editor', $this->editor ) );
259
		$search->setSlice( 0, 2 );
260
261
		$results = $this->object->searchItems( $search, [], $total );
262
		$this->assertEquals( 2, count( $results ) );
263
		$this->assertEquals( 3, $total );
264
265
		foreach($results as $itemId => $item) {
266
			$this->assertEquals( $itemId, $item->getId() );
267
		}
268
	}
269
270
271
	public function testSearchItemsCriteria()
272
	{
273
		$search = $this->object->createSearch( true );
274
		$conditions = array(
275
			$search->compare( '==', 'customer.address.editor', $this->editor ),
276
			$search->getConditions()
277
		);
278
		$search->setConditions( $search->combine( '&&', $conditions ) );
279
		$this->assertEquals( 2, count( $this->object->searchItems( $search, [], $total ) ) );
280
	}
281
282
283
	public function testSearchItemsRef()
284
	{
285
		$search = $this->object->createSearch();
286
		$search->setConditions( $search->compare( '==', 'customer.code', 'UTC001' ) );
287
288
		$results = $this->object->searchItems( $search, ['customer/address', 'text'] );
289
290
		if( ( $item = reset( $results ) ) === false ) {
291
			throw new \Exception( 'No customer item for "UTC001" available' );
292
		}
293
294
		$this->assertEquals( 1, count( $item->getRefItems( 'text' ) ) );
295
		$this->assertEquals( 1, count( $item->getAddressItems() ) );
296
	}
297
298
299
	public function testGetSubManager()
300
	{
301
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'address' ) );
302
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'address', 'Standard' ) );
303
304
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
305
		$this->object->getSubManager( 'unknown' );
306
	}
307
308
309
	public function testGetSubManagerInvalidName()
310
	{
311
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
312
		$this->object->getSubManager( 'address', 'unknown' );
313
	}
314
}
315