LaravelTest::testSaveUpdateDeleteItem()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 40
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 51
rs 9.28

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), 2015-2025
6
 */
7
8
9
 namespace Aimeos\MShop\Customer\Manager\Lists;
10
11
12
class LaravelTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $object;
15
	private $context;
16
17
18
	protected function setUp() : void
19
	{
20
		$this->context = \TestHelper::context();
21
		$this->object = new \Aimeos\MShop\Customer\Manager\Lists\Laravel( $this->context );
22
	}
23
24
25
	protected function tearDown() : void
26
	{
27
		unset( $this->object, $this->context );
28
	}
29
30
31
	public function testClear()
32
	{
33
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->clear( array( -1 ) ) );
34
	}
35
36
37
	public function testCreateItem()
38
	{
39
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $this->object->create() );
40
	}
41
42
43
	public function testGetItem()
44
	{
45
		$search = $this->object->filter()->slice( 0, 1 );
46
		$item = $this->object->search( $search )->first( new \RuntimeException( 'No item found' ) );
47
48
		$this->assertEquals( $item, $this->object->get( $item->getId() ) );
49
	}
50
51
52
	public function testSaveUpdateDeleteItem()
53
	{
54
		$search = $this->object->filter()->slice( 0, 1 );
55
		$item = $this->object->search( $search )->first( new \RuntimeException( 'No item found' ) );
56
57
		$item->setId( null );
58
		$item->setDomain( 'unittest' );
59
		$resultSaved = $this->object->save( $item );
60
		$itemSaved = $this->object->get( $item->getId() );
61
62
		$itemExp = clone $itemSaved;
63
		$itemExp->setDomain( 'unittest2' );
64
		$resultUpd = $this->object->save( $itemExp );
65
		$itemUpd = $this->object->get( $itemExp->getId() );
66
67
		$this->object->delete( $itemSaved->getId() );
68
69
		$this->assertTrue( $item->getId() !== null );
70
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
71
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
72
		$this->assertEquals( $item->getParentId(), $itemSaved->getParentId() );
73
		$this->assertEquals( $item->getType(), $itemSaved->getType() );
74
		$this->assertEquals( $item->getRefId(), $itemSaved->getRefId() );
75
		$this->assertEquals( $item->getDomain(), $itemSaved->getDomain() );
76
		$this->assertEquals( $item->getDateStart(), $itemSaved->getDateStart() );
77
		$this->assertEquals( $item->getDateEnd(), $itemSaved->getDateEnd() );
78
		$this->assertEquals( $item->getPosition(), $itemSaved->getPosition() );
79
80
		$this->assertEquals( $this->context->editor(), $itemSaved->editor() );
81
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
82
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
83
84
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
85
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
86
		$this->assertEquals( $itemExp->getParentId(), $itemUpd->getParentId() );
87
		$this->assertEquals( $itemExp->getType(), $itemUpd->getType() );
88
		$this->assertEquals( $itemExp->getRefId(), $itemUpd->getRefId() );
89
		$this->assertEquals( $itemExp->getDomain(), $itemUpd->getDomain() );
90
		$this->assertEquals( $itemExp->getDateStart(), $itemUpd->getDateStart() );
91
		$this->assertEquals( $itemExp->getDateEnd(), $itemUpd->getDateEnd() );
92
		$this->assertEquals( $itemExp->getPosition(), $itemUpd->getPosition() );
93
94
		$this->assertEquals( $this->context->editor(), $itemUpd->editor() );
95
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
96
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
97
98
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
99
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
100
101
		$this->expectException( '\\Aimeos\\MShop\\Exception' );
102
		$this->object->get( $itemSaved->getId() );
103
	}
104
105
106
	public function testSearchItems()
107
	{
108
		$total = 0;
109
		$search = $this->object->filter();
110
111
		$expr = [];
112
		$expr[] = $search->compare( '!=', 'customer.lists.id', null );
113
		$expr[] = $search->compare( '!=', 'customer.lists.siteid', null );
114
		$expr[] = $search->compare( '!=', 'customer.lists.parentid', null );
115
		$expr[] = $search->compare( '!=', 'customer.lists.key', null );
116
		$expr[] = $search->compare( '==', 'customer.lists.domain', 'text' );
117
		$expr[] = $search->compare( '==', 'customer.lists.type', 'default' );
118
		$expr[] = $search->compare( '>', 'customer.lists.refid', 0 );
119
		$expr[] = $search->compare( '==', 'customer.lists.datestart', '2010-01-01 00:00:00' );
120
		$expr[] = $search->compare( '==', 'customer.lists.dateend', '2098-01-01 00:00:00' );
121
		$expr[] = $search->compare( '!=', 'customer.lists.config', null );
122
		$expr[] = $search->compare( '>', 'customer.lists.position', 0 );
123
		$expr[] = $search->compare( '==', 'customer.lists.status', 1 );
124
		$expr[] = $search->compare( '>=', 'customer.lists.mtime', '1970-01-01 00:00:00' );
125
		$expr[] = $search->compare( '>=', 'customer.lists.ctime', '1970-01-01 00:00:00' );
126
		$expr[] = $search->compare( '!=', 'customer.lists.editor', '' );
127
128
		$search->setConditions( $search->and( $expr ) );
129
		$search->slice( 0, 2 );
130
		$results = $this->object->search( $search, [], $total );
131
132
		$this->assertEquals( 2, count( $results ) );
133
		$this->assertEquals( 3, $total );
134
135
		foreach( $results as $itemId => $item ) {
136
			$this->assertEquals( $itemId, $item->getId() );
137
		}
138
	}
139
140
141
	public function testSearchItemsAll()
142
	{
143
		$result = $this->object->search( $this->object->filter() );
144
		$this->assertGreaterThanOrEqual( 5, count( $result ) );
145
	}
146
147
148
	public function testSearchItemsBase()
149
	{
150
		$result = $this->object->search( $this->object->filter( true ) );
151
		$this->assertGreaterThanOrEqual( 5, count( $result ) );
152
	}
153
154
155
	public function testGetSubManager()
156
	{
157
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'type' ) );
158
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'type', 'Standard' ) );
159
160
		$this->expectException( \LogicException::class );
161
		$this->object->getSubManager( 'unknown' );
162
	}
163
}
164