StandardTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2025
6
 */
7
8
9
namespace Aimeos\Admin\JQAdm\Catalog\Product;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $object;
16
	private $view;
17
18
19
	protected function setUp() : void
20
	{
21
		$this->view = \TestHelper::view();
22
		$this->context = \TestHelper::context();
23
24
		$this->object = new \Aimeos\Admin\JQAdm\Catalog\Product\Standard( $this->context );
25
		$this->object = new \Aimeos\Admin\JQAdm\Common\Decorator\Page( $this->object, $this->context );
26
		$this->object->setAimeos( \TestHelper::getAimeos() );
27
		$this->object->setView( $this->view );
28
	}
29
30
31
	protected function tearDown() : void
32
	{
33
		unset( $this->object, $this->view, $this->context );
34
	}
35
36
37
	public function testCopy()
38
	{
39
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
40
		$this->view->item = $manager->find( 'cafe' );
41
42
		$result = $this->object->copy();
43
44
		$this->assertStringContainsString( 'item-product', $result );
45
	}
46
47
48
	public function testCreate()
49
	{
50
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
51
		$this->view->item = $manager->find( 'cafe' );
52
53
		$result = $this->object->create();
54
55
		$this->assertStringContainsString( 'item-product', $result );
56
	}
57
58
59
	public function testGet()
60
	{
61
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
62
		$this->view->item = $manager->find( 'cafe' );
63
64
		$result = $this->object->get();
65
66
		$this->assertStringContainsString( 'item-product', $result );
67
	}
68
69
70
	public function testSave()
71
	{
72
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
73
		$prodId = $manager->save( $manager->create()->setCode( 'jqadm_catalog_test' ) )->getId();
74
		$item = \Aimeos\MShop::create( $this->context, 'catalog' )->find( 'cafe' );
75
76
		$param = array(
77
			'site' => 'unittest',
78
			'product' => array(
79
				'product.lists.id' => [0 => ''],
80
				'product.lists.parentid' => [0 => $prodId],
81
				'product.lists.status' => [0 => 1],
82
				'product.lists.refid' => [0 => 'test'],
83
				'product.lists.type' => [0 => 'promotion'],
84
				'product.lists.datestart' => [0 => '2000-01-01 00:00:00'],
85
				'product.lists.dateend' => [0 => '2100-01-01 00:00:00'],
86
				'product.lists.config' => [0 => '{"test": "value"}'],
87
			),
88
		);
89
90
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
91
		$this->view->addHelper( 'param', $helper );
92
		$this->view->item = $item;
93
94
		$result = $this->object->save();
95
96
		$product = $manager->get( $prodId, ['catalog'] );
0 ignored issues
show
Bug introduced by
It seems like $prodId can also be of type Aimeos\Map and null; however, parameter $id of Aimeos\MShop\Common\Manager\Iface::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
		$product = $manager->get( /** @scrutinizer ignore-type */ $prodId, ['catalog'] );
Loading history...
97
		$manager->delete( $prodId );
98
99
		$this->assertEmpty( $this->view->get( 'errors' ) );
100
		$this->assertEmpty( $result );
101
		$this->assertEquals( 1, count( $product->getListItems() ) );
102
103
		foreach( $product->getListItems( 'catalog' ) as $listItem )
104
		{
105
			$this->assertEquals( $prodId, $listItem->getParentId() );
106
			$this->assertEquals( 'promotion', $listItem->getType() );
107
			$this->assertEquals( 'catalog', $listItem->getDomain() );
108
			$this->assertEquals( '2000-01-01 00:00:00', $listItem->getDateStart() );
109
			$this->assertEquals( '2100-01-01 00:00:00', $listItem->getDateEnd() );
110
			$this->assertEquals( ['test' => 'value'], $listItem->getConfig() );
111
			$this->assertEquals( $item->getId(), $listItem->getRefId() );
112
			$this->assertEquals( 1, $listItem->getStatus() );
113
		}
114
	}
115
116
117
	public function testSaveException()
118
	{
119
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Catalog\Product\Standard::class )
120
			->setConstructorArgs( array( $this->context, \TestHelper::getTemplatePaths() ) )
121
			->onlyMethods( array( 'fromArray' ) )
122
			->getMock();
123
124
		$object->expects( $this->once() )->method( 'fromArray' )
125
			->will( $this->throwException( new \RuntimeException() ) );
126
127
		$object->setView( $this->getViewNoRender() );
128
129
		$this->expectException( \RuntimeException::class );
130
		$object->save();
131
	}
132
133
134
	public function testGetSubClient()
135
	{
136
		$this->expectException( \LogicException::class );
137
		$this->object->getSubClient( 'unknown' );
138
	}
139
140
141
	protected function getViewNoRender()
142
	{
143
		$view = $this->getMockBuilder( \Aimeos\Base\View\Standard::class )
144
			->setConstructorArgs( array( [] ) )
145
			->onlyMethods( array( 'render' ) )
146
			->getMock();
147
148
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
149
		$view->item = $manager->find( 'cafe' );
0 ignored issues
show
Bug introduced by
Accessing item on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
150
151
		return $view;
152
	}
153
}
154