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

FosUserTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 6
dl 0
loc 199
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A tearDown() 0 4 1
A testCleanup() 0 4 1
A testCreateItem() 0 5 1
A testSaveInvalid() 0 5 1
B testSaveUpdateDeleteItem() 0 55 2
A testGetItem() 0 18 2
A testGetResourceType() 0 7 1
A testGetSearchAttributes() 0 6 2
A testSearchItems() 0 45 2
A testGetSubManager() 0 8 1
A testGetSubManagerInvalidName() 0 5 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018
6
 */
7
8
9
namespace Aimeos\MShop\Customer\Manager\Property;
10
11
12
class FosUserTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $object;
15
	private $editor = '';
16
17
18
	protected function setUp()
19
	{
20
		$context = \TestHelper::getContext();
21
		$this->editor = $context->getEditor();
22
23
		$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $context, 'FosUser' );
24
		$this->object = $manager->getSubManager( 'property', 'FosUser' );
25
	}
26
27
28
	protected function tearDown()
29
	{
30
		unset( $this->object );
31
	}
32
33
34
	public function testCleanup()
35
	{
36
		$this->object->cleanup( array( -1 ) );
37
	}
38
39
40
	public function testCreateItem()
41
	{
42
		$item = $this->object->createItem();
43
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Property\\Iface', $item );
44
	}
45
46
47
	public function testSaveInvalid()
48
	{
49
		$this->setExpectedException( '\Aimeos\MShop\Exception' );
50
		$this->object->saveItem( new \Aimeos\MShop\Locale\Item\Standard() );
51
	}
52
53
54
	public function testSaveUpdateDeleteItem()
55
	{
56
		$search = $this->object->createSearch();
57
		$search->setConditions( $search->compare( '==', 'customer.property.editor', $this->editor ) );
58
		$results = $this->object->searchItems( $search );
59
60
		if( ( $item = reset($results) ) === false ) {
61
			throw new \RuntimeException( 'No property item found' );
62
		}
63
64
		$item->setId(null);
65
		$item->setLanguageId( 'en' );
66
		$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...
67
		$itemSaved = $this->object->getItem( $item->getId() );
68
69
		$itemExp = clone $itemSaved;
70
		$itemExp->setValue( 'unittest' );
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 setValue() does only exist in the following implementations of said interface: Aimeos\MAdmin\Cache\Item\Standard, Aimeos\MShop\Common\Item\Property\Standard, Aimeos\MShop\Order\Item\...duct\Attribute\Standard, Aimeos\MShop\Order\Item\...vice\Attribute\Standard, Aimeos\MShop\Order\Item\Status\Standard, Aimeos\MShop\Price\Item\Base, Aimeos\MShop\Price\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...
71
		$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...
72
		$itemUpd = $this->object->getItem( $itemExp->getId() );
73
74
		$this->object->deleteItem( $itemSaved->getId() );
75
76
		$context = \TestHelper::getContext();
77
78
		$this->assertTrue( $item->getId() !== null );
79
		$this->assertTrue( $itemSaved->getType() !== null );
80
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
81
		$this->assertEquals( $item->getParentId(), $itemSaved->getParentId() );
82
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
83
		$this->assertEquals( $item->getTypeId(), $itemSaved->getTypeId() );
84
		$this->assertEquals( $item->getLanguageId(), $itemSaved->getLanguageId() );
85
		$this->assertEquals( $item->getValue(), $itemSaved->getValue() );
86
87
		$this->assertEquals( $context->getEditor(), $itemSaved->getEditor() );
88
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
89
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
90
91
		$this->assertTrue( $itemUpd->getType() !== null );
92
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
93
		$this->assertEquals( $itemExp->getParentId(), $itemUpd->getParentId() );
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 getParentId() does only exist in the following implementations of said interface: Aimeos\MShop\Catalog\Item\Standard, Aimeos\MShop\Common\Item\Address\Standard, Aimeos\MShop\Common\Item\Lists\Standard, Aimeos\MShop\Common\Item\Property\Standard, Aimeos\MShop\Coupon\Item\Code\Standard, Aimeos\MShop\Customer\Item\Address\Standard, Aimeos\MShop\Locale\Item\Site\Standard, Aimeos\MShop\Order\Item\...duct\Attribute\Standard, Aimeos\MShop\Order\Item\...vice\Attribute\Standard, Aimeos\MShop\Order\Item\Status\Standard, Aimeos\MShop\Supplier\Item\Address\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...
94
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
95
		$this->assertEquals( $itemExp->getTypeId(), $itemUpd->getTypeId() );
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 getTypeId() does only exist in the following implementations of said interface: Aimeos\MShop\Attribute\Item\Standard, Aimeos\MShop\Common\Item\Lists\Standard, Aimeos\MShop\Common\Item\Property\Standard, Aimeos\MShop\Media\Item\Standard, Aimeos\MShop\Plugin\Item\Standard, Aimeos\MShop\Price\Item\Base, Aimeos\MShop\Price\Item\Standard, Aimeos\MShop\Product\Item\Standard, Aimeos\MShop\Service\Item\Standard, Aimeos\MShop\Stock\Item\Standard, Aimeos\MShop\Tag\Item\Standard, Aimeos\MShop\Text\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...
96
		$this->assertEquals( $itemExp->getLanguageId(), $itemUpd->getLanguageId() );
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 getLanguageId() does only exist in the following implementations of said interface: Aimeos\MShop\Common\Item\Address\Base, Aimeos\MShop\Common\Item\Address\Standard, Aimeos\MShop\Common\Item\Property\Standard, Aimeos\MShop\Customer\Item\Address\Standard, Aimeos\MShop\Locale\Item\Standard, Aimeos\MShop\Media\Item\Standard, Aimeos\MShop\Order\Item\Base\Address\Base, Aimeos\MShop\Order\Item\Base\Address\Standard, Aimeos\MShop\Supplier\Item\Address\Standard, Aimeos\MShop\Tag\Item\Standard, Aimeos\MShop\Text\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...
97
		$this->assertEquals( $itemExp->getValue(), $itemUpd->getValue() );
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 getValue() does only exist in the following implementations of said interface: Aimeos\MAdmin\Cache\Item\Standard, Aimeos\MShop\Common\Item\Property\Standard, Aimeos\MShop\Order\Item\...duct\Attribute\Standard, Aimeos\MShop\Order\Item\...vice\Attribute\Standard, Aimeos\MShop\Order\Item\Status\Standard, Aimeos\MShop\Price\Item\Base, Aimeos\MShop\Price\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...
98
99
		$this->assertEquals( $context->getEditor(), $itemUpd->getEditor() );
100
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
101
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
102
103
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultSaved );
104
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultUpd );
105
106
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
107
		$this->object->getItem( $itemSaved->getId() );
108
	}
109
110
111
	public function testGetItem()
112
	{
113
		$search = $this->object->createSearch();
114
		$conditions = array(
115
			$search->compare( '~=', 'customer.property.value', '1'),
116
			$search->compare( '==', 'customer.property.editor', $this->editor )
117
		);
118
		$search->setConditions( $search->combine( '&&', $conditions ) );
119
		$results = $this->object->searchItems( $search );
120
121
		if( ($expected = reset($results)) === false ) {
122
			throw new \RuntimeException( sprintf( 'No customer property item found for value "%1$s".', '1' ) );
123
		}
124
125
		$actual = $this->object->getItem( $expected->getId() );
126
		$this->assertNotEquals( '', $actual->getTypeName() );
127
		$this->assertEquals( $expected, $actual );
128
	}
129
130
131
	public function testGetResourceType()
132
	{
133
		$result = $this->object->getResourceType();
134
135
		$this->assertContains( 'customer/property', $result );
136
		$this->assertContains( 'customer/property/type', $result );
137
	}
138
139
140
	public function testGetSearchAttributes()
141
	{
142
		foreach( $this->object->getSearchAttributes() as $attribute ) {
143
			$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Attribute\\Iface', $attribute );
144
		}
145
	}
146
147
148
	public function testSearchItems()
149
	{
150
		$total = 0;
151
		$search = $this->object->createSearch();
152
153
		$expr = [];
154
		$expr[] = $search->compare( '!=', 'customer.property.id', null );
155
		$expr[] = $search->compare( '!=', 'customer.property.parentid', null );
156
		$expr[] = $search->compare( '!=', 'customer.property.siteid', null );
157
		$expr[] = $search->compare( '!=', 'customer.property.typeid', null );
158
		$expr[] = $search->compare( '==', 'customer.property.languageid', null );
159
		$expr[] = $search->compare( '==', 'customer.property.value', '1' );
160
		$expr[] = $search->compare( '==', 'customer.property.editor', $this->editor );
161
162
		$expr[] = $search->compare( '!=', 'customer.property.type.id', null );
163
		$expr[] = $search->compare( '!=', 'customer.property.type.siteid', null );
164
		$expr[] = $search->compare( '==', 'customer.property.type.domain', 'customer' );
165
		$expr[] = $search->compare( '==', 'customer.property.type.code', 'newsletter' );
166
		$expr[] = $search->compare( '>', 'customer.property.type.label', '' );
167
		$expr[] = $search->compare( '==', 'customer.property.type.status', 1 );
168
		$expr[] = $search->compare( '>=', 'customer.property.type.mtime', '1970-01-01 00:00:00' );
169
		$expr[] = $search->compare( '>=', 'customer.property.type.ctime', '1970-01-01 00:00:00' );
170
		$expr[] = $search->compare( '==', 'customer.property.type.editor', $this->editor );
171
172
		$search->setConditions( $search->combine('&&', $expr) );
173
		$results = $this->object->searchItems( $search, [], $total );
174
		$this->assertEquals( 1, count( $results ) );
175
176
177
		$search = $this->object->createSearch();
178
		$conditions = array(
179
			$search->compare( '=~', 'customer.property.type.code', 'newsletter' ),
180
			$search->compare( '==', 'customer.property.editor', $this->editor )
181
		);
182
		$search->setConditions( $search->combine( '&&', $conditions ) );
183
		$search->setSlice(0, 1);
184
		$items = $this->object->searchItems( $search, [], $total );
185
186
		$this->assertEquals( 1, count( $items ) );
187
		$this->assertEquals( 1, $total );
188
189
		foreach($items as $itemId => $item) {
190
			$this->assertEquals( $itemId, $item->getId() );
191
		}
192
	}
193
194
195
	public function testGetSubManager()
196
	{
197
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager('type') );
198
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager('type', 'Standard') );
199
200
		$this->setExpectedException('\\Aimeos\\MShop\\Exception');
201
		$this->object->getSubManager('unknown');
202
	}
203
204
205
	public function testGetSubManagerInvalidName()
206
	{
207
		$this->setExpectedException('\\Aimeos\\MShop\\Exception');
208
		$this->object->getSubManager('type', 'unknown');
209
	}
210
}
211