Completed
Push — master ( 8c23c8...3a0c6f )
by Aimeos
02:00
created

LaravelTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 164
Duplicated Lines 20.12 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 6
dl 33
loc 164
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 8 8 1
A tearDown() 0 4 1
A testCleanup() 0 4 1
A testCreateItem() 0 5 1
A testSaveInvalid() 0 5 1
A testSaveUpdateDeleteItem() 0 53 2
A testGetItem() 17 17 2
A testGetResourceType() 0 6 1
A testGetSearchAttributes() 0 6 2
A testSearchItems() 0 14 1
A testGetSubManager() 8 8 1
A testGetSubManagerInvalidName() 0 5 1

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
/**
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 LaravelTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $object;
15
	private $editor = '';
16
17
18 View Code Duplication
	protected function setUp()
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...
19
	{
20
		$context = \TestHelper::getContext();
21
		$this->editor = $context->getEditor();
22
23
		$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $context, 'Laravel' );
24
		$this->object = $manager->getSubManager( 'property', 'Laravel' );
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' );
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() );
92
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
93
		$this->assertEquals( $itemExp->getType(), $itemUpd->getType() );
94
		$this->assertEquals( $itemExp->getLanguageId(), $itemUpd->getLanguageId() );
95
		$this->assertEquals( $itemExp->getValue(), $itemUpd->getValue() );
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 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...
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;
0 ignored issues
show
Unused Code introduced by
$total is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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
159
160 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...
161
	{
162
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager('type') );
163
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager('type', 'Standard') );
164
165
		$this->setExpectedException('\\Aimeos\\MShop\\Exception');
166
		$this->object->getSubManager('unknown');
167
	}
168
169
170
	public function testGetSubManagerInvalidName()
171
	{
172
		$this->setExpectedException('\\Aimeos\\MShop\\Exception');
173
		$this->object->getSubManager('type', 'unknown');
174
	}
175
}
176