Completed
Push — master ( 94b58b...843436 )
by Aimeos
02:17
created

LaravelTest   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 313
Duplicated Lines 26.84 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 27
lcom 1
cbo 5
dl 84
loc 313
rs 10
c 1
b 1
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A tearDown() 0 4 1
A testCleanup() 0 4 1
A testAggregate() 0 15 1
A testCreateItem() 0 4 1
A testGetItem() 12 12 2
B testSaveUpdateDeleteItem() 0 63 2
A testMoveItemLastToFront() 0 23 3
A testMoveItemFirstToLast() 27 27 4
A testMoveItemFirstUp() 27 27 4
A testSearchItems() 0 41 2
A testSearchItemsNoCriteria() 0 6 1
A testSearchItemsBaseCriteria() 10 10 1
A testGetSubManager() 8 8 1
A getListItems() 0 26 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Aimeos\MShop\Customer\Manager\Lists;
4
5
6
/**
7
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
8
 * @copyright Aimeos (aimeos.org), 2015-2017
9
 */
10
class LaravelTest extends \PHPUnit\Framework\TestCase
11
{
12
	private $object;
13
	private $context;
14
	private $editor = 'ai-laravel:unittest';
15
16
17
	protected function setUp()
18
	{
19
		$this->context = \TestHelper::getContext();
20
		$this->editor = $this->context->getEditor();
21
		$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $this->context, 'Laravel' );
22
		$this->object = $manager->getSubManager( 'lists', 'Laravel' );
23
	}
24
25
26
	protected function tearDown()
27
	{
28
		unset( $this->object, $this->context );
29
	}
30
31
32
	public function testCleanup()
33
	{
34
		$this->object->cleanup( array( -1 ) );
35
	}
36
37
38
	public function testAggregate()
39
	{
40
		$search = $this->object->createSearch( true );
41
		$expr = array(
42
			$search->getConditions(),
43
			$search->compare( '==', 'customer.lists.editor', 'ai-laravel:unittest' ),
44
		);
45
		$search->setConditions( $search->combine( '&&', $expr ) );
46
47
		$result = $this->object->aggregate( $search, 'customer.lists.domain' );
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MShop\Common\Manager\Iface as the method aggregate() does only exist in the following implementations of said interface: Aimeos\MShop\Attribute\Manager\Lists\Standard, Aimeos\MShop\Catalog\Manager\Lists\Standard, Aimeos\MShop\Common\Manager\Lists\Base, Aimeos\MShop\Customer\Manager\Lists\Laravel, Aimeos\MShop\Customer\Manager\Lists\Standard, Aimeos\MShop\Index\Manager\Attribute\MySQL, Aimeos\MShop\Index\Manager\Attribute\Standard, Aimeos\MShop\Index\Manager\Catalog\MySQL, Aimeos\MShop\Index\Manager\Catalog\Standard, Aimeos\MShop\Index\Manager\DBBase, Aimeos\MShop\Index\Manager\MySQL, Aimeos\MShop\Index\Manager\PgSQL, Aimeos\MShop\Index\Manager\Price\MySQL, Aimeos\MShop\Index\Manager\Price\Standard, Aimeos\MShop\Index\Manager\Standard, Aimeos\MShop\Index\Manager\Supplier\MySQL, Aimeos\MShop\Index\Manager\Supplier\Standard, Aimeos\MShop\Index\Manager\Text\MySQL, Aimeos\MShop\Index\Manager\Text\PgSQL, Aimeos\MShop\Index\Manager\Text\Standard, Aimeos\MShop\Media\Manager\Lists\Standard, Aimeos\MShop\Order\Manager\Base\Address\Standard, Aimeos\MShop\Order\Manager\Base\Coupon\Standard, Aimeos\MShop\Order\Manag...duct\Attribute\Standard, Aimeos\MShop\Order\Manager\Base\Product\Standard, Aimeos\MShop\Order\Manag...vice\Attribute\Standard, Aimeos\MShop\Order\Manager\Base\Service\Standard, Aimeos\MShop\Order\Manager\Base\Standard, Aimeos\MShop\Order\Manager\Standard, Aimeos\MShop\Order\Manager\Status\Standard, Aimeos\MShop\Price\Manager\Lists\Standard, Aimeos\MShop\Product\Manager\Lists\Standard, Aimeos\MShop\Service\Manager\Lists\Standard, Aimeos\MShop\Subscription\Manager\Standard, Aimeos\MShop\Supplier\Manager\Lists\Standard, Aimeos\MShop\Text\Manager\Lists\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...
48
49
		$this->assertEquals( 1, count( $result ) );
50
		$this->assertArrayHasKey( 'text', $result );
51
		$this->assertEquals( 4, $result['text'] );
52
	}
53
54
55
	public function testCreateItem()
56
	{
57
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $this->object->createItem() );
58
	}
59
60
61 View Code Duplication
	public function testGetItem()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
	{
63
		$search = $this->object->createSearch();
64
		$search->setSlice(0, 1);
65
		$results = $this->object->searchItems( $search );
66
67
		if( ( $item = reset( $results ) ) === false ) {
68
			throw new \RuntimeException( 'No item found' );
69
		}
70
71
		$this->assertEquals( $item, $this->object->getItem( $item->getId() ) );
72
	}
73
74
75
	public function testSaveUpdateDeleteItem()
76
	{
77
		$search = $this->object->createSearch();
78
		$search->setSlice(0, 1);
79
		$items = $this->object->searchItems( $search );
80
81
		if( ( $item = reset( $items ) ) === false ) {
82
			throw new \RuntimeException( 'No item found' );
83
		}
84
85
		$item->setId( null );
86
		$item->setDomain( 'unittest' );
87
		$resultSaved = $this->object->saveItem( $item );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $resultSaved is correct as $this->object->saveItem($item) (which targets Aimeos\MShop\Common\Manager\Iface::saveItem()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
88
		$itemSaved = $this->object->getItem( $item->getId() );
89
90
		$itemExp = clone $itemSaved;
91
		$itemExp->setDomain( 'unittest2' );
92
		$resultUpd = $this->object->saveItem( $itemExp );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $resultUpd is correct as $this->object->saveItem($itemExp) (which targets Aimeos\MShop\Common\Manager\Iface::saveItem()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
93
		$itemUpd = $this->object->getItem( $itemExp->getId() );
94
95
		$this->object->deleteItem( $itemSaved->getId() );
96
97
98
		$this->assertTrue( $item->getId() !== null );
99
		$this->assertTrue( $itemSaved->getType() !== null );
100
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
101
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
102
		$this->assertEquals( $item->getParentId(), $itemSaved->getParentId() );
103
		$this->assertEquals( $item->getTypeId(), $itemSaved->getTypeId() );
104
		$this->assertEquals( $item->getRefId(), $itemSaved->getRefId() );
105
		$this->assertEquals( $item->getDomain(), $itemSaved->getDomain() );
106
		$this->assertEquals( $item->getDateStart(), $itemSaved->getDateStart() );
107
		$this->assertEquals( $item->getDateEnd(), $itemSaved->getDateEnd() );
108
		$this->assertEquals( $item->getPosition(), $itemSaved->getPosition() );
109
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
110
		$this->assertStringStartsWith(date('Y-m-d', time()), $itemSaved->getTimeCreated());
111
		$this->assertStringStartsWith(date('Y-m-d', time()), $itemSaved->getTimeModified());
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->assertTrue( $itemUpd->getType() !== null );
118
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
119
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
120
		$this->assertEquals( $itemExp->getParentId(), $itemUpd->getParentId() );
121
		$this->assertEquals( $itemExp->getTypeId(), $itemUpd->getTypeId() );
122
		$this->assertEquals( $itemExp->getRefId(), $itemUpd->getRefId() );
123
		$this->assertEquals( $itemExp->getDomain(), $itemUpd->getDomain() );
124
		$this->assertEquals( $itemExp->getDateStart(), $itemUpd->getDateStart() );
125
		$this->assertEquals( $itemExp->getDateEnd(), $itemUpd->getDateEnd() );
126
		$this->assertEquals( $itemExp->getPosition(), $itemUpd->getPosition() );
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( $itemSaved->getId() );
137
	}
138
139
140
	public function testMoveItemLastToFront()
141
	{
142
		$listItems = $this->getListItems();
143
		$this->assertGreaterThan( 1, count( $listItems ) );
144
145
		if( ( $first = reset( $listItems ) ) === false ) {
146
			throw new \RuntimeException( 'No first customer list item' );
147
		}
148
149
		if( ( $last = end( $listItems ) ) === false ) {
150
			throw new \RuntimeException( 'No last customer list item' );
151
		}
152
153
		$this->object->moveItem( $last->getId(), $first->getId() );
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MShop\Common\Manager\Iface as the method moveItem() does only exist in the following implementations of said interface: Aimeos\MShop\Attribute\Manager\Lists\Standard, Aimeos\MShop\Catalog\Manager\Decorator\Base, Aimeos\MShop\Catalog\Manager\Lists\Standard, Aimeos\MShop\Catalog\Manager\Standard, Aimeos\MShop\Common\Manager\Lists\Base, Aimeos\MShop\Customer\Manager\Lists\Laravel, Aimeos\MShop\Customer\Manager\Lists\Standard, Aimeos\MShop\Locale\Manager\Site\Standard, Aimeos\MShop\Media\Manager\Lists\Standard, Aimeos\MShop\Price\Manager\Lists\Standard, Aimeos\MShop\Product\Manager\Lists\Standard, Aimeos\MShop\Service\Manager\Lists\Standard, Aimeos\MShop\Supplier\Manager\Lists\Standard, Aimeos\MShop\Text\Manager\Lists\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...
154
155
		$newFirst = $this->object->getItem( $last->getId() );
156
		$newSecond = $this->object->getItem( $first->getId() );
157
158
		$this->object->moveItem( $last->getId() );
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MShop\Common\Manager\Iface as the method moveItem() does only exist in the following implementations of said interface: Aimeos\MShop\Attribute\Manager\Lists\Standard, Aimeos\MShop\Catalog\Manager\Decorator\Base, Aimeos\MShop\Catalog\Manager\Lists\Standard, Aimeos\MShop\Catalog\Manager\Standard, Aimeos\MShop\Common\Manager\Lists\Base, Aimeos\MShop\Customer\Manager\Lists\Laravel, Aimeos\MShop\Customer\Manager\Lists\Standard, Aimeos\MShop\Locale\Manager\Site\Standard, Aimeos\MShop\Media\Manager\Lists\Standard, Aimeos\MShop\Price\Manager\Lists\Standard, Aimeos\MShop\Product\Manager\Lists\Standard, Aimeos\MShop\Service\Manager\Lists\Standard, Aimeos\MShop\Supplier\Manager\Lists\Standard, Aimeos\MShop\Text\Manager\Lists\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...
159
160
		$this->assertEquals( 1, $newFirst->getPosition() );
161
		$this->assertEquals( 2, $newSecond->getPosition() );
162
	}
163
164
165 View Code Duplication
	public function testMoveItemFirstToLast()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
	{
167
		$listItems = $this->getListItems();
168
		$this->assertGreaterThan( 1, count( $listItems ) );
169
170
		if( ( $first = reset( $listItems ) ) === false ) {
171
			throw new \RuntimeException( 'No first customer list item' );
172
		}
173
174
		if( ( $second = next( $listItems ) ) === false ) {
175
			throw new \RuntimeException( 'No second customer list item' );
176
		}
177
178
		if( ( $last = end( $listItems ) ) === false ) {
179
			throw new \RuntimeException( 'No last customer list item' );
180
		}
181
182
		$this->object->moveItem( $first->getId() );
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MShop\Common\Manager\Iface as the method moveItem() does only exist in the following implementations of said interface: Aimeos\MShop\Attribute\Manager\Lists\Standard, Aimeos\MShop\Catalog\Manager\Decorator\Base, Aimeos\MShop\Catalog\Manager\Lists\Standard, Aimeos\MShop\Catalog\Manager\Standard, Aimeos\MShop\Common\Manager\Lists\Base, Aimeos\MShop\Customer\Manager\Lists\Laravel, Aimeos\MShop\Customer\Manager\Lists\Standard, Aimeos\MShop\Locale\Manager\Site\Standard, Aimeos\MShop\Media\Manager\Lists\Standard, Aimeos\MShop\Price\Manager\Lists\Standard, Aimeos\MShop\Product\Manager\Lists\Standard, Aimeos\MShop\Service\Manager\Lists\Standard, Aimeos\MShop\Supplier\Manager\Lists\Standard, Aimeos\MShop\Text\Manager\Lists\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...
183
184
		$newBefore = $this->object->getItem( $last->getId() );
185
		$newLast = $this->object->getItem( $first->getId() );
186
187
		$this->object->moveItem( $first->getId(), $second->getId() );
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MShop\Common\Manager\Iface as the method moveItem() does only exist in the following implementations of said interface: Aimeos\MShop\Attribute\Manager\Lists\Standard, Aimeos\MShop\Catalog\Manager\Decorator\Base, Aimeos\MShop\Catalog\Manager\Lists\Standard, Aimeos\MShop\Catalog\Manager\Standard, Aimeos\MShop\Common\Manager\Lists\Base, Aimeos\MShop\Customer\Manager\Lists\Laravel, Aimeos\MShop\Customer\Manager\Lists\Standard, Aimeos\MShop\Locale\Manager\Site\Standard, Aimeos\MShop\Media\Manager\Lists\Standard, Aimeos\MShop\Price\Manager\Lists\Standard, Aimeos\MShop\Product\Manager\Lists\Standard, Aimeos\MShop\Service\Manager\Lists\Standard, Aimeos\MShop\Supplier\Manager\Lists\Standard, Aimeos\MShop\Text\Manager\Lists\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...
188
189
		$this->assertEquals( $last->getPosition() - 1, $newBefore->getPosition() );
190
		$this->assertEquals( $last->getPosition(), $newLast->getPosition() );
191
	}
192
193
194 View Code Duplication
	public function testMoveItemFirstUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
195
	{
196
		$listItems = $this->getListItems();
197
		$this->assertGreaterThan( 1, count( $listItems ) );
198
199
		if( ( $first = reset( $listItems ) ) === false ) {
200
			throw new \RuntimeException( 'No first customer list item' );
201
		}
202
203
		if( ( $second = next( $listItems ) ) === false ) {
204
			throw new \RuntimeException( 'No second customer list item' );
205
		}
206
207
		if( ( $last = end( $listItems ) ) === false ) {
208
			throw new \RuntimeException( 'No last customer list item' );
209
		}
210
211
		$this->object->moveItem( $first->getId(), $last->getId() );
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MShop\Common\Manager\Iface as the method moveItem() does only exist in the following implementations of said interface: Aimeos\MShop\Attribute\Manager\Lists\Standard, Aimeos\MShop\Catalog\Manager\Decorator\Base, Aimeos\MShop\Catalog\Manager\Lists\Standard, Aimeos\MShop\Catalog\Manager\Standard, Aimeos\MShop\Common\Manager\Lists\Base, Aimeos\MShop\Customer\Manager\Lists\Laravel, Aimeos\MShop\Customer\Manager\Lists\Standard, Aimeos\MShop\Locale\Manager\Site\Standard, Aimeos\MShop\Media\Manager\Lists\Standard, Aimeos\MShop\Price\Manager\Lists\Standard, Aimeos\MShop\Product\Manager\Lists\Standard, Aimeos\MShop\Service\Manager\Lists\Standard, Aimeos\MShop\Supplier\Manager\Lists\Standard, Aimeos\MShop\Text\Manager\Lists\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...
212
213
		$newLast = $this->object->getItem( $last->getId() );
214
		$newUp = $this->object->getItem( $first->getId() );
215
216
		$this->object->moveItem( $first->getId(), $second->getId() );
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MShop\Common\Manager\Iface as the method moveItem() does only exist in the following implementations of said interface: Aimeos\MShop\Attribute\Manager\Lists\Standard, Aimeos\MShop\Catalog\Manager\Decorator\Base, Aimeos\MShop\Catalog\Manager\Lists\Standard, Aimeos\MShop\Catalog\Manager\Standard, Aimeos\MShop\Common\Manager\Lists\Base, Aimeos\MShop\Customer\Manager\Lists\Laravel, Aimeos\MShop\Customer\Manager\Lists\Standard, Aimeos\MShop\Locale\Manager\Site\Standard, Aimeos\MShop\Media\Manager\Lists\Standard, Aimeos\MShop\Price\Manager\Lists\Standard, Aimeos\MShop\Product\Manager\Lists\Standard, Aimeos\MShop\Service\Manager\Lists\Standard, Aimeos\MShop\Supplier\Manager\Lists\Standard, Aimeos\MShop\Text\Manager\Lists\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...
217
218
		$this->assertEquals( $last->getPosition() - 1, $newUp->getPosition() );
219
		$this->assertEquals( $last->getPosition(), $newLast->getPosition() );
220
	}
221
222
223
	public function testSearchItems()
224
	{
225
		$total = 0;
226
		$search = $this->object->createSearch();
227
228
		$expr = [];
229
		$expr[] = $search->compare( '!=', 'customer.lists.id', null );
230
		$expr[] = $search->compare( '!=', 'customer.lists.siteid', null );
231
		$expr[] = $search->compare( '>', 'customer.lists.parentid', 0 );
232
		$expr[] = $search->compare( '==', 'customer.lists.domain', 'text' );
233
		$expr[] = $search->compare( '>', 'customer.lists.typeid', 0 );
234
		$expr[] = $search->compare( '>', 'customer.lists.refid', 0 );
235
		$expr[] = $search->compare( '==', 'customer.lists.datestart', '2010-01-01 00:00:00' );
236
		$expr[] = $search->compare( '==', 'customer.lists.dateend', '2100-01-01 00:00:00' );
237
		$expr[] = $search->compare( '!=', 'customer.lists.config', null );
238
		$expr[] = $search->compare( '>', 'customer.lists.position', 1 );
239
		$expr[] = $search->compare( '==', 'customer.lists.status', 1 );
240
		$expr[] = $search->compare( '>=', 'customer.lists.mtime', '1970-01-01 00:00:00' );
241
		$expr[] = $search->compare( '>=', 'customer.lists.ctime', '1970-01-01 00:00:00' );
242
		$expr[] = $search->compare( '==', 'customer.lists.editor', $this->editor );
243
244
		$expr[] = $search->compare( '!=', 'customer.lists.type.id', 0 );
245
		$expr[] = $search->compare( '!=', 'customer.lists.type.siteid', null );
246
		$expr[] = $search->compare( '==', 'customer.lists.type.code', 'default' );
247
		$expr[] = $search->compare( '==', 'customer.lists.type.domain', 'text' );
248
		$expr[] = $search->compare( '==', 'customer.lists.type.label', 'Standard' );
249
		$expr[] = $search->compare( '==', 'customer.lists.type.status', 1 );
250
		$expr[] = $search->compare( '>=', 'customer.lists.type.mtime', '1970-01-01 00:00:00' );
251
		$expr[] = $search->compare( '>=', 'customer.lists.type.ctime', '1970-01-01 00:00:00' );
252
		$expr[] = $search->compare( '==', 'customer.lists.type.editor', $this->editor );
253
254
		$search->setConditions( $search->combine( '&&', $expr ) );
255
		$search->setSlice(0, 1);
256
		$results = $this->object->searchItems( $search, [], $total );
257
		$this->assertEquals( 1, count( $results ) );
258
		$this->assertEquals( 2, $total );
259
260
		foreach($results as $itemId => $item) {
261
			$this->assertEquals( $itemId, $item->getId() );
262
		}
263
	}
264
265
266
	public function testSearchItemsNoCriteria()
267
	{
268
		$search = $this->object->createSearch();
269
		$search->setConditions( $search->compare( '==', 'customer.lists.editor', $this->editor ) );
270
		$this->assertEquals( 4, count( $this->object->searchItems($search) ) );
271
	}
272
273
274 View Code Duplication
	public function testSearchItemsBaseCriteria()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
275
	{
276
		$search = $this->object->createSearch(true);
277
		$conditions = array(
278
			$search->compare( '==', 'customer.lists.editor', $this->editor ),
279
			$search->getConditions()
280
		);
281
		$search->setConditions( $search->combine( '&&', $conditions ) );
282
		$this->assertEquals( 4, count( $this->object->searchItems($search) ) );
283
	}
284
285
286 View Code Duplication
	public function testGetSubManager()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
287
	{
288
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager('type') );
289
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager('type', 'Standard') );
290
291
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
292
		$this->object->getSubManager( 'unknown' );
293
	}
294
295
296
	protected function getListItems()
297
	{
298
		$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $this->context, 'Laravel' );
299
300
		$search = $manager->createSearch();
301
		$search->setConditions( $search->compare( '==', 'customer.code', 'UTC003' ) );
302
		$search->setSlice( 0, 1 );
303
304
		$results = $manager->searchItems( $search );
305
306
		if( ( $item = reset( $results ) ) === false ) {
307
			throw new \RuntimeException( 'No customer item found' );
308
		}
309
310
		$search = $this->object->createSearch();
311
		$expr = array(
312
			$search->compare( '==', 'customer.lists.parentid', $item->getId() ),
313
			$search->compare( '==', 'customer.lists.domain', 'text' ),
314
			$search->compare( '==', 'customer.lists.editor', $this->editor ),
315
			$search->compare( '==', 'customer.lists.type.code', 'default' ),
316
		);
317
		$search->setConditions( $search->combine( '&&', $expr ) );
318
		$search->setSortations( array( $search->sort( '+', 'customer.lists.position' ) ) );
319
320
		return $this->object->searchItems( $search );
321
	}
322
}
323