Passed
Push — master ( 9ddf12...7e7bb8 )
by Aimeos
14:56
created

StandardTest::testCreateOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2022-2024
6
 */
7
8
namespace Aimeos\MShop\Order\Manager\Basket;
9
10
11
class StandardTest extends \PHPUnit\Framework\TestCase
12
{
13
	private $context;
14
	private $object;
15
	private $editor = '';
16
17
18
	protected function setUp() : void
19
	{
20
		$this->context = \TestHelper::context();
21
		$this->editor = $this->context->editor();
22
23
		$this->object = new \Aimeos\MShop\Order\Manager\Basket\Standard( $this->context );
24
	}
25
26
27
	protected function tearDown() : void
28
	{
29
		unset( $this->object );
30
	}
31
32
33
	public function testClear()
34
	{
35
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->clear( [-1] ) );
36
	}
37
38
39
	public function testDelete()
40
	{
41
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->delete( [-1] ) );
42
	}
43
44
45
	public function testGetResourceType()
46
	{
47
		$result = $this->object->getResourceType();
48
49
		$this->assertContains( 'order/basket', $result );
50
	}
51
52
53
	public function testCreate()
54
	{
55
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Basket\Iface::class, $this->object->create() );
56
	}
57
58
59
	public function testCreateOrder()
60
	{
61
		$product = \Aimeos\MShop::create( $this->context, 'product' )->find( 'CNE' );
62
		$orderProduct = \Aimeos\MShop::create( $this->context, 'order/product' )->create()->copyFrom( $product );
63
		$order = \Aimeos\MShop::create( $this->context, 'order' )->create()->addProduct( $orderProduct );
64
		$basket = $this->object->save( $this->object->create()->setItem( $order )->setId( 'unittest-100' ) );
65
66
		$basket2 = $this->object->get( $basket->getId() );
67
		$this->object->delete( $basket );
68
69
		$this->assertEquals( 1, count( $basket2->getItem()->getProducts() ) );
70
	}
71
72
73
	public function testGet()
74
	{
75
		$search = $this->object->filter()->slice( 0, 1 );
76
		$expected = $this->object->search( $search )->first( new \Exception( 'No order basket item found' ) );
77
78
		$this->assertEquals( $expected, $this->object->get( $expected->getId() ) );
79
	}
80
81
82
	public function testSaveUpdateDelete()
83
	{
84
		$search = $this->object->filter()->add( ['order.basket.customerid' => -1] )->slice( 0, 1 );
85
		$item = $this->object->search( $search )->first( new \Exception( 'No order basket item found' ) );
86
87
		$item->setId( 'unittest_1' );
88
		$resultSaved = $this->object->save( $item );
89
		$itemSaved = $this->object->get( $item->getId() );
90
91
		$itemExp = clone $itemSaved;
92
		$itemExp->setName( 'unit test' );
93
		$resultUpd = $this->object->save( $itemExp );
94
		$itemUpd = $this->object->get( $itemExp->getId() );
95
96
		$this->object->delete( $itemSaved->getId() );
97
98
99
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
100
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
101
		$this->assertEquals( $item->getCustomerId(), $itemSaved->getCustomerId() );
102
		$this->assertEquals( $item->getItem(), $itemSaved->getItem() );
103
		$this->assertEquals( $item->getName(), $itemSaved->getName() );
104
105
		$this->assertEquals( $this->editor, $itemSaved->editor() );
106
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
107
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
108
109
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
110
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
111
		$this->assertEquals( $itemExp->getCustomerId(), $itemUpd->getCustomerId() );
112
		$this->assertEquals( $itemExp->getItem(), $itemUpd->getItem() );
113
		$this->assertEquals( $itemExp->getName(), $itemUpd->getName() );
114
115
		$this->assertEquals( $this->editor, $itemUpd->editor() );
116
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
117
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
118
119
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
120
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
121
122
		$this->expectException( \Aimeos\MShop\Exception::class );
123
		$this->object->get( $itemSaved->getId() );
124
	}
125
126
127
	public function testFilter()
128
	{
129
		$this->assertInstanceOf( \Aimeos\Base\Criteria\Iface::class, $this->object->filter() );
130
	}
131
132
133
	public function testSearch()
134
	{
135
		$total = 0;
136
		$search = $this->object->filter();
137
		$siteid = $this->context->locale()->getSiteId();
138
139
		$expr = [];
140
		$expr[] = $search->compare( '!=', 'order.basket.id', null );
141
		$expr[] = $search->compare( '==', 'order.basket.siteid', $siteid );
142
		$expr[] = $search->compare( '==', 'order.basket.customerid', '-1' );
143
		$expr[] = $search->compare( '>=', 'order.basket.name', '' );
144
		$expr[] = $search->compare( '>=', 'order.basket.content', '' );
145
		$expr[] = $search->compare( '>=', 'order.basket.mtime', '1970-01-01 00:00:00' );
146
		$expr[] = $search->compare( '>=', 'order.basket.ctime', '1970-01-01 00:00:00' );
147
		$expr[] = $search->compare( '==', 'order.basket.editor', $this->editor );
148
149
		$search->setConditions( $search->and( $expr ) );
150
		$result = $this->object->search( $search, [], $total )->toArray();
151
152
		$this->assertEquals( 1, count( $result ) );
153
		$this->assertEquals( 1, $total );
154
	}
155
156
157
	public function testGetSubManager()
158
	{
159
		$this->expectException( \LogicException::class );
160
		$this->object->getSubManager( 'unknown' );
161
	}
162
}
163