Completed
Push — master ( 5bedaf...185f90 )
by Aimeos
01:33
created

Typo3Test::testGetSavePropertyItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2014-2017
7
 */
8
9
namespace Aimeos\MShop\Customer\Manager;
10
11
12
class Typo3Test extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $object;
16
	private $editor;
17
	private $item;
18
19
20
	protected function setUp()
21
	{
22
		$this->context = \TestHelper::getContext();
23
		$this->editor = $this->context->getEditor();
24
		$this->context->getConfig()->set( 'mshop/customer/manager/typo3/pid-default', 999999 );
25
		$this->object = new \Aimeos\MShop\Customer\Manager\Typo3( $this->context );
26
	}
27
28
29
	protected function tearDown()
30
	{
31
		unset( $this->object, $this->item );
32
	}
33
34
35
	public function testGetSearchAttributes()
36
	{
37
		foreach( $this->object->getSearchAttributes() as $attribute )
38
		{
39
			$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Attribute\\Iface', $attribute );
40
		}
41
	}
42
43
44
	public function testCreateItem()
45
	{
46
		$item = $this->object->createItem();
47
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Customer\\Item\\Iface', $item );
48
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Customer\\Item\\Typo3', $item );
49
		$this->assertEquals( 999999, $item->getPageId() );
50
	}
51
52
53
	public function testGetItem()
54
	{
55
		$search = $this->object->createSearch();
56
		$search->setConditions( $search->compare( '==', 'customer.code', 'UTC001' ) );
57
		$items = $this->object->searchItems( $search );
58
59
		if( ( $expected = reset( $items ) ) === false ) {
60
			throw new \RuntimeException( 'No customer found.' );
61
		}
62
63
		$actual = $this->object->getItem( $expected->getId() );
64
		$billing = $actual->getPaymentAddress();
65
66
		$this->assertEquals( $expected, $actual );
67
68
		$this->assertEquals( 'unitCustomer1', $actual->getLabel() );
69
		$this->assertEquals( 'UTC001', $actual->getCode() );
70
		$this->assertEquals( 'mr', $billing->getSalutation() );
71
		$this->assertEquals( 'ABC GmbH', $billing->getCompany() );
72
		$this->assertEquals( 'Dr.', $billing->getTitle() );
73
		$this->assertEquals( 'Max', $billing->getFirstname() );
74
		$this->assertEquals( 'Mustermann', $billing->getLastname() );
75
		$this->assertEquals( 'Musterstraße 1a', $billing->getAddress1() );
76
		$this->assertEquals( '', $billing->getAddress2() );
77
		$this->assertEquals( '', $billing->getAddress3() );
78
		$this->assertEquals( '20001', $billing->getPostal() );
79
		$this->assertEquals( 'Musterstadt', $billing->getCity() );
80
		$this->assertEquals( 'Hamburg', $billing->getState() );
81
		$this->assertEquals( 'de', $billing->getLanguageId() );
82
		$this->assertEquals( 'DE', $billing->getCountryId() );
83
		$this->assertEquals( '055544332211', $billing->getTelephone() );
84
		$this->assertEquals( '[email protected]', $billing->getEMail() );
85
		$this->assertEquals( '055544332212', $billing->getTelefax() );
86
		$this->assertEquals( 'unittest.aimeos.org', $billing->getWebsite() );
87
		$this->assertEquals( '10.0', $billing->getLongitude() );
88
		$this->assertEquals( '50.0', $billing->getLatitude() );
89
		$this->assertEquals( 1, $actual->getStatus() );
90
		$this->assertEquals( '5f4dcc3b5aa765d61d8327deb882cf99', $actual->getPassword() );
91
		$this->assertEquals( '2011-01-13 11:03:36', $actual->getTimeCreated() );
92
		$this->assertEquals( '2011-01-13 11:03:46', $actual->getTimeModified() );
93
		$this->assertEquals( '', $actual->getEditor() );
94
	}
95
96
97
	public function testSaveUpdateDeleteItem()
98
	{
99
		$search = $this->object->createSearch();
100
		$search->setConditions( $search->compare( '==', 'customer.code', 'UTC001' ) );
101
		$results = $this->object->searchItems( $search );
102
103
		if( ( $item = reset( $results ) ) === false ) {
104
			throw new \RuntimeException( 'No customer found.' );
105
		}
106
107
		$item->setId( null );
108
		$item->setCode( 'unitTest' );
109
		$item->setLabel( 'unitTest' );
110
		$item->setGroups( array( 1, 2, 3 ) );
111
		$resultSaved = $this->object->saveItem( $item );
112
		$itemSaved = $this->object->getItem( $item->getId() );
113
114
		$itemExp = clone $itemSaved;
115
		$itemExp->setCode( 'unitTest2' );
116
		$itemExp->setLabel( 'unitTest2' );
117
		$itemExp->setGroups( array( 2, 4 ) );
118
		$resultUpd = $this->object->saveItem( $itemExp );
119
		$itemUpd = $this->object->getItem( $itemExp->getId() );
120
121
		$this->object->deleteItem( $item->getId() );
122
123
124
		$this->assertTrue( $item->getId() !== null );
125
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
126
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
127
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
128
		$this->assertEquals( $item->getCode(), $itemSaved->getCode() );
129
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
130
		$this->assertEquals( $item->getPaymentAddress(), $itemSaved->getPaymentAddress() );
131
		$this->assertEquals( $item->getBirthday(), $itemSaved->getBirthday() );
132
		$this->assertEquals( $item->getPassword(), $itemSaved->getPassword() );
133
		$this->assertEquals( $item->getGroups(), $itemSaved->getGroups() );
134
		$this->assertEquals( $item->getPageId(), $itemSaved->getPageId() );
135
136
		$this->assertEquals( '', $itemSaved->getEditor() );
137
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
138
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
139
140
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
141
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
142
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
143
		$this->assertEquals( $itemExp->getCode(), $itemUpd->getCode() );
144
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
145
		$this->assertEquals( $itemExp->getPaymentAddress(), $itemUpd->getPaymentAddress() );
146
		$this->assertEquals( $itemExp->getBirthday(), $itemUpd->getBirthday() );
147
		$this->assertEquals( $itemExp->getPassword(), $itemUpd->getPassword() );
148
		$this->assertEquals( $itemExp->getGroups(), $itemUpd->getGroups() );
149
		$this->assertEquals( $itemExp->getPageId(), $itemUpd->getPageId() );
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 getPageId() does only exist in the following implementations of said interface: Aimeos\MShop\Customer\Item\Typo3.

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