Completed
Push — master ( bb9fc3...b1ee36 )
by Aimeos
01:36
created

FosUserTest::testSaveUpdateDeleteItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 9.0254
c 0
b 0
f 0
cc 2
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\MW\Common\Exception::class );
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 );
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 );
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->assertEquals( $item->getId(), $itemSaved->getId() );
80
		$this->assertEquals( $item->getParentId(), $itemSaved->getParentId() );
81
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
82
		$this->assertEquals( $item->getType(), $itemSaved->getType() );
83
		$this->assertEquals( $item->getLanguageId(), $itemSaved->getLanguageId() );
84
		$this->assertEquals( $item->getValue(), $itemSaved->getValue() );
85
86
		$this->assertEquals( $context->getEditor(), $itemSaved->getEditor() );
87
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
88
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
89
90
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
91
		$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...
92
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
93
		$this->assertEquals( $itemExp->getType(), $itemUpd->getType() );
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 getType() 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\Order\Item\Base\Address\Standard, Aimeos\MShop\Order\Item\...duct\Attribute\Standard, Aimeos\MShop\Order\Item\Base\Product\Base, Aimeos\MShop\Order\Item\Base\Product\Standard, Aimeos\MShop\Order\Item\...vice\Attribute\Standard, Aimeos\MShop\Order\Item\Base\Service\Base, Aimeos\MShop\Order\Item\Base\Service\Standard, Aimeos\MShop\Order\Item\Standard, Aimeos\MShop\Order\Item\Status\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...
94
		$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\Simple, 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...
95
		$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...
96
97
		$this->assertEquals( $context->getEditor(), $itemUpd->getEditor() );
98
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
99
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
100
101
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
102
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
103
104
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
105
		$this->object->getItem( $itemSaved->getId() );
106
	}
107
108
109
	public function testGetItem()
110
	{
111
		$search = $this->object->createSearch();
112
		$conditions = array(
113
			$search->compare( '~=', 'customer.property.value', '1'),
114
			$search->compare( '==', 'customer.property.editor', $this->editor )
115
		);
116
		$search->setConditions( $search->combine( '&&', $conditions ) );
117
		$results = $this->object->searchItems( $search );
118
119
		if( ($expected = reset($results)) === false ) {
120
			throw new \RuntimeException( sprintf( 'No customer property item found for value "%1$s".', '1' ) );
121
		}
122
123
		$actual = $this->object->getItem( $expected->getId() );
124
		$this->assertEquals( $expected, $actual );
125
	}
126
127
128
	public function testGetResourceType()
129
	{
130
		$result = $this->object->getResourceType();
131
132
		$this->assertContains( 'customer/property', $result );
133
	}
134
135
136
	public function testGetSearchAttributes()
137
	{
138
		foreach( $this->object->getSearchAttributes() as $attribute ) {
139
			$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Attribute\\Iface', $attribute );
140
		}
141
	}
142
143
144
	public function testSearchItems()
145
	{
146
		$total = 0;
147
		$search = $this->object->createSearch();
148
149
		$expr = [];
150
		$expr[] = $search->compare( '!=', 'customer.property.id', null );
151
		$expr[] = $search->compare( '!=', 'customer.property.parentid', null );
152
		$expr[] = $search->compare( '!=', 'customer.property.siteid', null );
153
		$expr[] = $search->compare( '==', 'customer.property.type', 'newsletter' );
154
		$expr[] = $search->compare( '==', 'customer.property.languageid', null );
155
		$expr[] = $search->compare( '==', 'customer.property.value', '1' );
156
		$expr[] = $search->compare( '==', 'customer.property.editor', $this->editor );
157
158
		$search->setConditions( $search->combine('&&', $expr) );
159
		$results = $this->object->searchItems( $search, [], $total );
160
		$this->assertEquals( 1, count( $results ) );
161
	}
162
163
164
	public function testGetSubManager()
165
	{
166
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager('type') );
167
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager('type', 'Standard') );
168
169
		$this->setExpectedException('\\Aimeos\\MShop\\Exception');
170
		$this->object->getSubManager('unknown');
171
	}
172
173
174
	public function testGetSubManagerInvalidName()
175
	{
176
		$this->setExpectedException('\\Aimeos\\MShop\\Exception');
177
		$this->object->getSubManager('type', 'unknown');
178
	}
179
}
180