Passed
Push — master ( 2cc80f...ef78f2 )
by Aimeos
05:36
created

StandardTest::testClear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
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\Basket\Manager;
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\Basket\Manager\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 testCreate()
46
	{
47
		$this->assertInstanceOf( \Aimeos\MShop\Basket\Item\Iface::class, $this->object->create() );
48
	}
49
50
51
	public function testCreateOrder()
52
	{
53
		$product = \Aimeos\MShop::create( $this->context, 'product' )->find( 'CNE' );
54
		$orderProduct = \Aimeos\MShop::create( $this->context, 'order/product' )->create()->copyFrom( $product );
55
		$order = \Aimeos\MShop::create( $this->context, 'order' )->create()->addProduct( $orderProduct );
56
		$basket = $this->object->save( $this->object->create()->setItem( $order )->setId( 'unittest-100' ) );
57
58
		$basket2 = $this->object->get( $basket->getId() );
59
		$this->object->delete( $basket );
60
61
		$this->assertEquals( 1, count( $basket2->getItem()->getProducts() ) );
62
	}
63
64
65
	public function testGet()
66
	{
67
		$search = $this->object->filter()->slice( 0, 1 );
68
		$expected = $this->object->search( $search )->first( new \Exception( 'No order basket item found' ) );
69
70
		$this->assertEquals( $expected, $this->object->get( $expected->getId() ) );
71
	}
72
73
74
	public function testSaveUpdateDelete()
75
	{
76
		$search = $this->object->filter()->add( ['basket.customerid' => -1] )->slice( 0, 1 );
77
		$item = $this->object->search( $search )->first( new \Exception( 'No order basket item found' ) );
78
79
		$item->setId( 'unittest_1' );
80
		$resultSaved = $this->object->save( $item );
81
		$itemSaved = $this->object->get( $item->getId() );
82
83
		$itemExp = clone $itemSaved;
84
		$itemExp->setName( 'unit test' );
85
		$resultUpd = $this->object->save( $itemExp );
86
		$itemUpd = $this->object->get( $itemExp->getId() );
87
88
		$this->object->delete( $itemSaved->getId() );
89
90
91
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
92
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
93
		$this->assertEquals( $item->getCustomerId(), $itemSaved->getCustomerId() );
94
		$this->assertEquals( $item->getItem(), $itemSaved->getItem() );
95
		$this->assertEquals( $item->getName(), $itemSaved->getName() );
96
97
		$this->assertEquals( $this->editor, $itemSaved->editor() );
98
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
99
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
100
101
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
102
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
103
		$this->assertEquals( $itemExp->getCustomerId(), $itemUpd->getCustomerId() );
104
		$this->assertEquals( $itemExp->getItem(), $itemUpd->getItem() );
105
		$this->assertEquals( $itemExp->getName(), $itemUpd->getName() );
106
107
		$this->assertEquals( $this->editor, $itemUpd->editor() );
108
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
109
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
110
111
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
112
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
113
114
		$this->expectException( \Aimeos\MShop\Exception::class );
115
		$this->object->get( $itemSaved->getId() );
116
	}
117
118
119
	public function testFilter()
120
	{
121
		$this->assertInstanceOf( \Aimeos\Base\Criteria\Iface::class, $this->object->filter() );
122
	}
123
124
125
	public function testSearch()
126
	{
127
		$total = 0;
128
		$search = $this->object->filter();
129
		$siteid = $this->context->locale()->getSiteId();
130
131
		$expr = [];
132
		$expr[] = $search->compare( '!=', 'basket.id', null );
133
		$expr[] = $search->compare( '==', 'basket.siteid', $siteid );
134
		$expr[] = $search->compare( '==', 'basket.customerid', '-1' );
135
		$expr[] = $search->compare( '>=', 'basket.name', '' );
136
		$expr[] = $search->compare( '>=', 'basket.content', '' );
137
		$expr[] = $search->compare( '>=', 'basket.mtime', '1970-01-01 00:00:00' );
138
		$expr[] = $search->compare( '>=', 'basket.ctime', '1970-01-01 00:00:00' );
139
		$expr[] = $search->compare( '==', 'basket.editor', $this->editor );
140
141
		$search->setConditions( $search->and( $expr ) );
142
		$result = $this->object->search( $search, [], $total )->toArray();
143
144
		$this->assertEquals( 1, count( $result ) );
145
		$this->assertEquals( 1, $total );
146
	}
147
148
149
	public function testGetSubManager()
150
	{
151
		$this->expectException( \LogicException::class );
152
		$this->object->getSubManager( 'unknown' );
153
	}
154
}
155