Passed
Push — master ( f7bbd3...17cd43 )
by Aimeos
04:27
created

StandardTest::testSaveUpdateDeleteItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 51
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 37
nc 2
nop 0
dl 0
loc 51
rs 9.328
c 0
b 0
f 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), 2017-2018
6
 */
7
8
9
namespace Aimeos\MShop\Price\Manager\Property\Type;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $object;
15
	private $editor = '';
16
17
18
	protected function setUp()
19
	{
20
		$this->editor = \TestHelperMShop::getContext()->getEditor();
21
		$manager = \Aimeos\MShop\Price\Manager\Factory::create( \TestHelperMShop::getContext() );
22
		$this->object = $manager->getSubManager( 'property' )->getSubManager('type');
23
	}
24
25
26
	protected function tearDown()
27
	{
28
		unset($this->object);
29
	}
30
31
32
	public function testCleanup()
33
	{
34
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->cleanup( [-1] ) );
35
	}
36
37
38
	public function testCreateItem()
39
	{
40
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Type\Iface::class, $this->object->createItem() );
41
	}
42
43
44
	public function testGetResourceType()
45
	{
46
		$result = $this->object->getResourceType();
47
48
		$this->assertContains( 'price/property/type', $result );
49
	}
50
51
52
	public function testGetSearchAttributes()
53
	{
54
		foreach( $this->object->getSearchAttributes() as $price ) {
55
			$this->assertInstanceOf( \Aimeos\MW\Criteria\Attribute\Iface::class, $price );
56
		}
57
	}
58
59
60
	public function testGetItem()
61
	{
62
		$search = $this->object->createSearch()->setSlice( 0, 1 );
63
		$conditions = array(
64
			$search->compare( '==', 'price.property.type.code', 'taxrate-local' ),
65
			$search->compare( '==', 'price.property.type.editor', $this->editor )
66
		);
67
		$search->setConditions( $search->combine( '&&', $conditions ) );
68
69
		$results = $this->object->searchItems( $search );
70
71
		if( ($expected = reset($results) ) === false )
72
		{
73
			throw new \RuntimeException( 'No property type item found.' );
74
		}
75
76
		$actual = $this->object->getItem( $expected->getId() );
77
78
		$this->assertEquals( $expected, $actual );
79
	}
80
81
82
	public function testSaveInvalid()
83
	{
84
		$this->setExpectedException( \Aimeos\MW\Common\Exception::class );
85
		$this->object->saveItem( new \Aimeos\MShop\Locale\Item\Standard() );
86
	}
87
88
89
	public function testSaveUpdateDeleteItem()
90
	{
91
		$search = $this->object->createSearch();
92
		$search->setConditions( $search->compare( '==', 'price.property.type.editor', $this->editor ) );
93
		$results = $this->object->searchItems($search);
94
95
		if( ( $item = reset($results) ) === false ) {
96
			throw new \RuntimeException( 'No type item found' );
97
		}
98
99
		$item->setId(null);
100
		$item->setCode( 'unitTestSave' );
101
		$resultSaved = $this->object->saveItem( $item );
102
		$itemSaved = $this->object->getItem( $item->getId() );
103
104
		$itemExp = clone $itemSaved;
105
		$itemExp->setCode( 'unitTestSave2' );
106
		$resultUpd = $this->object->saveItem( $itemExp );
107
		$itemUpd = $this->object->getItem( $itemExp->getId() );
108
109
		$this->object->deleteItem( $itemSaved->getId() );
110
111
112
		$this->assertTrue( $item->getId() !== null );
113
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
114
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
115
		$this->assertEquals( $item->getCode(), $itemSaved->getCode() );
116
		$this->assertEquals( $item->getDomain(), $itemSaved->getDomain() );
117
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
118
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
119
120
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
121
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
122
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
123
124
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
125
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
126
		$this->assertEquals( $itemExp->getCode(), $itemUpd->getCode() );
127
		$this->assertEquals( $itemExp->getDomain(), $itemUpd->getDomain() );
128
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
129
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
130
131
		$this->assertEquals( $this->editor, $itemUpd->getEditor() );
132
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
133
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
134
135
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
136
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
137
138
		$this->setExpectedException( \Aimeos\MShop\Exception::class );
139
		$this->object->getItem( $itemSaved->getId() );
140
	}
141
142
143
	public function testSearchItems()
144
	{
145
		$total = 0;
146
		$search = $this->object->createSearch();
147
148
		$expr = [];
149
		$expr[] = $search->compare( '!=', 'price.property.type.id', null );
150
		$expr[] = $search->compare( '!=', 'price.property.type.siteid', null );
151
		$expr[] = $search->compare( '==', 'price.property.type.domain', 'price' );
152
		$expr[] = $search->compare( '==', 'price.property.type.code', 'taxrate-local' );
153
		$expr[] = $search->compare( '==', 'price.property.type.label', 'Local tax' );
154
		$expr[] = $search->compare( '>=', 'price.property.type.position', 0 );
155
		$expr[] = $search->compare( '==', 'price.property.type.status', 1 );
156
		$expr[] = $search->compare( '>=', 'price.property.type.mtime', '1970-01-01 00:00:00' );
157
		$expr[] = $search->compare( '>=', 'price.property.type.ctime', '1970-01-01 00:00:00' );
158
		$expr[] = $search->compare( '==', 'price.property.type.editor', $this->editor );
159
160
		$search->setConditions( $search->combine('&&', $expr) );
161
		$results = $this->object->searchItems( $search, [], $total );
162
163
		$this->assertEquals( 1, count( $results ) );
164
	}
165
166
167
	public function testGetSubManager()
168
	{
169
		$this->setExpectedException( \Aimeos\MShop\Exception::class );
170
		$this->object->getSubManager('unknown');
171
	}
172
}
173