Passed
Push — master ( d3a4a1...d29683 )
by Aimeos
04:23
created

StandardTest::getClientMock()   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
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 11
rs 10
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\Admin\JQAdm\Basket;
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\Basket\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, 'order/basket' );
0 ignored issues
show
Unused Code introduced by
The assignment to $manager is dead and can be removed.
Loading history...
40
41
		$param = ['site' => 'unittest', 'id' => $this->getBasketId()];
42
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
43
		$this->view->addHelper( 'param', $helper );
44
45
		$result = $this->object->copy();
46
47
		$this->assertStringContainsString( 'unittest name', $result );
48
	}
49
50
51
	public function testCopyException()
52
	{
53
		$object = $this->getClientMock( 'getSubClients' );
54
55
		$object->expects( $this->once() )->method( 'getSubClients' )
56
			->will( $this->throwException( new \RuntimeException() ) );
57
58
		$object->copy();
59
	}
60
61
62
	public function testDelete()
63
	{
64
		$this->assertNull( $this->getClientMock( ['redirect'], false )->delete() );
65
	}
66
67
68
	public function testDeleteException()
69
	{
70
		$object = $this->getClientMock( ['getSubClients', 'search'] );
71
72
		$object->expects( $this->once() )->method( 'getSubClients' )
73
			->will( $this->throwException( new \RuntimeException() ) );
74
		$object->expects( $this->once() )->method( 'search' );
75
76
		$object->delete();
77
	}
78
79
80
	public function testGet()
81
	{
82
		$manager = \Aimeos\MShop::create( $this->context, 'order/basket' );
0 ignored issues
show
Unused Code introduced by
The assignment to $manager is dead and can be removed.
Loading history...
83
84
		$param = ['site' => 'unittest', 'id' => $this->getBasketId()];
85
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
86
		$this->view->addHelper( 'param', $helper );
87
88
		$result = $this->object->get();
89
90
		$this->assertStringContainsString( 'unittest name', $result );
91
	}
92
93
94
	public function testGetException()
95
	{
96
		$object = $this->getClientMock( 'getSubClients' );
97
98
		$object->expects( $this->once() )->method( 'getSubClients' )
99
			->will( $this->throwException( new \RuntimeException() ) );
100
101
		$object->get();
102
	}
103
104
105
	public function testSearch()
106
	{
107
		$param = array(
108
			'site' => 'unittest', 'locale' => 'de',
109
			'filter' => array(
110
				'key' => array( 0 => 'order.basket.name' ),
111
				'op' => array( 0 => '=~' ),
112
				'val' => array( 0 => 'unittest' ),
113
			),
114
			'sort' => array( 'order.basket.ctime' ),
115
		);
116
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
117
		$this->view->addHelper( 'param', $helper );
118
119
		$result = $this->object->search();
120
121
		$this->assertStringContainsString( 'unittest', $result );
122
	}
123
124
125
	public function testSearchException()
126
	{
127
		$object = $this->getClientMock( 'initCriteria' );
128
129
		$object->expects( $this->once() )->method( 'initCriteria' )
130
			->will( $this->throwException( new \RuntimeException() ) );
131
132
		$object->search();
133
	}
134
135
136
	public function testGetSubClientInvalid()
137
	{
138
		$this->expectException( \LogicException::class );
139
		$this->object->getSubClient( '$unknown$' );
140
	}
141
142
143
	public function testGetSubClientUnknown()
144
	{
145
		$this->expectException( \LogicException::class );
146
		$this->object->getSubClient( 'unknown' );
147
	}
148
149
150
	public function getClientMock( $methods, $real = true )
151
	{
152
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Basket\Standard::class )
153
			->setConstructorArgs( array( $this->context, \TestHelper::getTemplatePaths() ) )
154
			->onlyMethods( (array) $methods )
155
			->getMock();
156
157
		$object->setAimeos( \TestHelper::getAimeos() );
158
		$object->setView( $this->getViewNoRender( $real ) );
159
160
		return $object;
161
	}
162
163
164
	protected function getViewNoRender( $real = true )
165
	{
166
		$view = $this->getMockBuilder( \Aimeos\Base\View\Standard::class )
167
			->setConstructorArgs( array( [] ) )
168
			->onlyMethods( array( 'render' ) )
169
			->getMock();
170
171
		$manager = \Aimeos\MShop::create( $this->context, 'order/basket' );
0 ignored issues
show
Unused Code introduced by
The assignment to $manager is dead and can be removed.
Loading history...
172
173
		$param = ['site' => 'unittest', 'id' => $real ? $this->getBasketId() : -1];
174
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $param );
175
		$view->addHelper( 'param', $helper );
176
177
		$helper = new \Aimeos\Base\View\Helper\Config\Standard( $view, $this->context->config() );
178
		$view->addHelper( 'config', $helper );
179
180
		$helper = new \Aimeos\Base\View\Helper\Access\Standard( $view, [] );
181
		$view->addHelper( 'access', $helper );
182
183
		return $view;
184
	}
185
186
187
	protected function getBasketId( $name = 'unittest name' )
188
	{
189
		$manager = \Aimeos\MShop::create( $this->context, 'order/basket' );
190
		$search = $manager->filter()->slice( 0, 1 );
191
		$search->setConditions( $search->compare( '==', 'order.basket.name', $name ) );
192
193
		return $manager->search( $search )
194
			->first( new \Exception( sprintf( 'No basket item found for name "%1$s"', $name ) ) )
195
			->getId();
196
	}
197
}
198