Passed
Push — master ( 27f7d3...7fa2fb )
by Aimeos
05:29
created

BaseTest::testCreateEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 19
rs 9.7666
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2023
6
 */
7
8
9
namespace Aimeos\MShop\Test\Manager;
10
11
12
class BaseTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $object;
16
17
18
	protected function setUp() : void
19
	{
20
		$this->context = \TestHelper::context();
21
		$this->object = new \Aimeos\MShop\Test\Manager\Standard( $this->context );
22
	}
23
24
25
	protected function tearDown() : void
26
	{
27
		unset( $this->context, $this->object );
28
	}
29
30
31
	public function testClear()
32
	{
33
		$result = $this->object->clear( [$this->context->locale()->getSiteId()] );
34
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $result );
35
	}
36
37
38
	public function testCreate()
39
	{
40
		$item = $this->object->create( [
41
			'id' => 123,
42
			'siteid' => '1.',
43
			'key' => 'somekey',
44
			'value' => 'someval',
45
			'json' => ['key' => 'value'],
46
			'mtime' => '2000-01-01 00:00:00',
47
			'ctime' => '2000-01-01 00:00:00',
48
			'editor' => 'testeditor'
49
		] );
50
51
		$this->assertEquals( 123, $item->getId() );
52
		$this->assertEquals( '1.', $item->getSiteId() );
53
		$this->assertEquals( 'somekey', $item->get( 'key' ) );
54
		$this->assertEquals( 'someval', $item->get( 'value' ) );
55
		$this->assertEquals( ['key' => 'value'], $item->get( 'json' ) );
56
		$this->assertEquals( '2000-01-01 00:00:00', $item->getTimeModified() );
57
		$this->assertEquals( '2000-01-01 00:00:00', $item->getTimeCreated() );
58
		$this->assertEquals( 'testeditor', $item->editor() );
59
	}
60
61
62
	public function testCreateEmpty()
63
	{
64
		$data = [
65
			'id' => 123,
66
			'siteid' => '1.',
67
			'key' => 'somekey',
68
			'value' => 'someval',
69
			'json' => ['key' => 'value'],
70
			'mtime' => '2000-01-01 00:00:00',
71
			'ctime' => '2000-01-01 00:00:00',
72
			'editor' => 'testeditor'
73
		];
74
		$item = $this->object->create()->fromArray( $data, true );
75
76
		$this->assertEquals( 123, $item->getId() );
77
		$this->assertEquals( '1.', $item->getSiteId() );
78
		$this->assertEquals( 'somekey', $item->get( 'key' ) );
79
		$this->assertEquals( 'someval', $item->get( 'value' ) );
80
		$this->assertEquals( ['key' => 'value'], $item->get( 'json' ) );
81
	}
82
83
84
	public function testDelete()
85
	{
86
		$result = $this->object->delete( -1 );
87
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $result );
88
	}
89
90
91
	public function testFilter()
92
	{
93
		$result = $this->object->filter();
94
		$this->assertInstanceOf( \Aimeos\Base\Criteria\Iface::class, $result );
95
	}
96
97
98
	public function testSave()
99
	{
100
		$item = $this->object->create()
101
			->set( 'key', 'somekey' )
102
			->set( 'value', 'someval' )
103
			->set( 'json', ['somekey' => 'someval'] );
104
105
		$result = $this->object->save( $item );
106
107
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $result );
108
109
		$this->object->clear( [$this->context->locale()->getSiteId()] );
110
	}
111
112
113
	public function testSearch()
114
	{
115
		$item = $this->object->create()
116
			->set( 'key', 'somekey' )
117
			->set( 'value', 'someval' );
118
119
		$this->object->save( $item );
120
		$result = $this->object->search( $this->object->filter() );
121
122
		$this->assertInstanceOf( \Aimeos\Map::class, $result );
123
		$this->assertEquals( 'somekey', $result->first()?->get( 'key' ) );
124
		$this->assertEquals( 'someval', $result->first()?->get( 'value' ) );
125
126
		$this->object->clear( [$this->context->locale()->getSiteId()] );
127
	}
128
129
130
	public function testIterate()
131
	{
132
		$item = $this->object->create()
133
			->set( 'key', 'somekey' )
134
			->set( 'value', 'someval' );
135
136
		$this->object->save( $item );
137
138
		$cursor = $this->object->cursor( $this->object->filter() );
139
		$result = $this->object->iterate( $cursor );
140
141
		$this->assertInstanceOf( \Aimeos\Map::class, $result );
142
		$this->assertEquals( 'somekey', $result->first()?->get( 'key' ) );
143
		$this->assertEquals( 'someval', $result->first()?->get( 'value' ) );
144
145
		$this->object->clear( [$this->context->locale()->getSiteId()] );
146
	}
147
148
149
	public function testGet()
150
	{
151
		$item = $this->object->create()
152
			->set( 'key', 'somekey' )
153
			->set( 'value', 'someval' );
154
155
		$item = $this->object->save( $item );
156
		$result = $this->object->get( $item->getId() );
157
158
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $result );
159
		$this->assertEquals( 'somekey', $result->get( 'key' ) );
160
		$this->assertEquals( 'someval', $result->get( 'value' ) );
161
162
		$this->object->clear( [$this->context->locale()->getSiteId()] );
163
	}
164
165
166
	public function testGetResourceType()
167
	{
168
		$this->assertEquals( ['test'], $this->object->getResourceType() );
169
	}
170
}
171
172
173
174
class Standard extends \Aimeos\MShop\Common\Manager\Base implements \Aimeos\MShop\Common\Manager\Iface
175
{
176
	public function getSaveAttributes() : array
177
	{
178
		return $this->createAttributes( [
179
			'key' => [],
180
			'value' => [],
181
			'json' => [
182
				'type' => 'json'
183
			],
184
		] );
185
	}
186
}
187