Completed
Push — master ( cc87a0...64b84e )
by Aimeos
07:07
created

StandardTest::testProcessMShopException()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace Aimeos\Client\Html\Checkout\Standard\Process;
4
5
6
/**
7
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
8
 * @copyright Metaways Infosystems GmbH, 2013
9
 * @copyright Aimeos (aimeos.org), 2015
10
 */
11
class StandardTest extends \PHPUnit_Framework_TestCase
12
{
13
	private $object;
14
	private $context;
15
16
17
	/**
18
	 * Sets up the fixture, for example, opens a network connection.
19
	 * This method is called before a test is executed.
20
	 *
21
	 * @access protected
22
	 */
23
	protected function setUp()
24
	{
25
		\Aimeos\MShop\Factory::setCache( true );
26
27
		$this->context = \TestHelperHtml::getContext();
28
29
		$paths = \TestHelperHtml::getHtmlTemplatePaths();
30
		$this->object = new \Aimeos\Client\Html\Checkout\Standard\Process\Standard( $this->context, $paths );
31
		$this->object->setView( \TestHelperHtml::getView() );
32
	}
33
34
35
	/**
36
	 * Tears down the fixture, for example, closes a network connection.
37
	 * This method is called after a test is executed.
38
	 *
39
	 * @access protected
40
	 */
41
	protected function tearDown()
42
	{
43
		\Aimeos\Controller\Frontend\Basket\Factory::createController( $this->context )->clear();
44
		\Aimeos\MShop\Factory::setCache( false );
45
		unset( $this->object );
46
	}
47
48
49
	public function testGetHeader()
50
	{
51
		$view = $this->object->getView();
52
		$view->standardStepActive = 'process';
53
54
		$output = $this->object->getHeader();
55
		$this->assertNotNull( $output );
56
	}
57
58
59
	public function testGetHeaderSkip()
60
	{
61
		$output = $this->object->getHeader();
62
		$this->assertNotNull( $output );
63
	}
64
65
66
	public function testGetBody()
67
	{
68
		$view = $this->object->getView();
69
		$view->standardStepActive = 'process';
70
71
		$output = $this->object->getBody();
72
		$this->assertStringStartsWith( '<div class="checkout-standard-process">', $output );
73
		$this->assertEquals( 'http://baseurl/checkout/standard/?c_step=payment', $view->standardUrlPayment );
74
	}
75
76
77
	public function testGetSubClientInvalid()
78
	{
79
		$this->setExpectedException( '\\Aimeos\\Client\\Html\\Exception' );
80
		$this->object->getSubClient( 'invalid', 'invalid' );
81
	}
82
83
84
	public function testGetSubClientInvalidName()
85
	{
86
		$this->setExpectedException( '\\Aimeos\\Client\\Html\\Exception' );
87
		$this->object->getSubClient( '$$$', '$$$' );
88
	}
89
90
91
	public function testProcessNoService()
92
	{
93
		$view = $this->object->getView();
94
		$param = array( 'c_step' => 'process' );
95
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
96
		$view->addHelper( 'param', $helper );
97
98
		$orderid = $this->getOrder( '2008-02-15 12:34:56' )->getId();
99
		$this->context->getSession()->set( 'aimeos/orderid', $orderid );
100
101
		$paths = \TestHelperHtml::getHtmlTemplatePaths();
102
		$mock = $this->getMockBuilder( '\\Aimeos\\Client\\Html\\Checkout\\Standard\\Process\\Standard' )
103
			->setConstructorArgs( array( $this->context, $paths ) )
104
			->setMethods( array( 'getOrderServiceCode' ) )
105
			->getMock();
106
107
		$mock->expects( $this->once() )->method( 'getOrderServiceCode' )
108
			->will( $this->returnValue( null ) );
109
110
		$mock->setView( $view );
111
		$mock->process();
112
113
		$this->assertEquals( 0, count( $view->get( 'standardErrorList', array() ) ) );
114
		$this->assertEquals( 0, count( $view->get( 'standardProcessParams', array() ) ) );
115
		$this->assertEquals( 'GET', $view->standardMethod );
116
	}
117
118
119
	public function testProcessDirectDebit()
120
	{
121
		$mock = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Standard' )
122
			->setConstructorArgs( array( $this->context ) )
123
			->setMethods( array( 'saveItem', ) )
124
			->getMock();
125
126
		\Aimeos\MShop\Factory::injectManager( $this->context, 'order', $mock );
127
128
		$view = $this->object->getView();
129
		$param = array( 'c_step' => 'process' );
130
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
131
		$view->addHelper( 'param', $helper );
132
133
		$request = $this->getMock( '\Psr\Http\Message\ServerRequestInterface' );
134
		$helper = new \Aimeos\MW\View\Helper\Request\Standard( $view, $request, '127.0.0.1', 'test' );
135
		$view->addHelper( 'request', $helper );
136
137
		$orderid = $this->getOrder( '2009-03-18 16:14:32' )->getId();
138
		$this->context->getSession()->set( 'aimeos/orderid', $orderid );
139
140
		$this->object->process();
141
142
		$this->assertEquals( 0, count( $view->get( 'standardErrorList', array() ) ) );
143
		$this->assertEquals( 'POST', $view->standardMethod );
144
		$this->assertEquals( array(), $view->standardProcessParams );
145
		$this->assertEquals( true, $view->standardUrlExternal );
146
	}
147
148
149
	public function testProcessNoStep()
150
	{
151
		$this->assertNull( $this->object->process() );
152
	}
153
154
155
	public function testProcessHtmlException()
156
	{
157
		$view = $this->object->getView();
158
		$param = array( 'c_step' => 'process' );
159
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
160
		$view->addHelper( 'param', $helper );
161
162
		$orderid = $this->getOrder( '2008-02-15 12:34:56' )->getId();
163
		$this->context->getSession()->set( 'aimeos/orderid', $orderid );
164
165
		$paths = \TestHelperHtml::getHtmlTemplatePaths();
166
		$mock = $this->getMockBuilder( '\\Aimeos\\Client\\Html\\Checkout\\Standard\\Process\\Standard' )
167
			->setConstructorArgs( array( $this->context, $paths ) )
168
			->setMethods( array( 'getOrderServiceCode' ) )
169
			->getMock();
170
171
		$mock->expects( $this->once() )->method( 'getOrderServiceCode' )
172
			->will( $this->throwException( new \Aimeos\Client\Html\Exception() ) );
173
174
		$mock->setView( $view );
175
		$mock->process();
176
177
		$this->assertInternalType( 'array', $view->standardErrorList );
178
	}
179
180
181
	public function testProcessFrontendException()
182
	{
183
		$view = $this->object->getView();
184
		$param = array( 'c_step' => 'process' );
185
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
186
		$view->addHelper( 'param', $helper );
187
188
		$orderid = $this->getOrder( '2008-02-15 12:34:56' )->getId();
189
		$this->context->getSession()->set( 'aimeos/orderid', $orderid );
190
191
		$paths = \TestHelperHtml::getHtmlTemplatePaths();
192
		$mock = $this->getMockBuilder( '\\Aimeos\\Client\\Html\\Checkout\\Standard\\Process\\Standard' )
193
			->setConstructorArgs( array( $this->context, $paths ) )
194
			->setMethods( array( 'getOrderServiceCode' ) )
195
			->getMock();
196
197
		$mock->expects( $this->once() )->method( 'getOrderServiceCode' )
198
			->will( $this->throwException( new \Aimeos\Controller\Frontend\Exception() ) );
199
200
		$mock->setView( $view );
201
		$mock->process();
202
203
		$this->assertInternalType( 'array', $view->standardErrorList );
204
	}
205
206
207
	public function testProcessMShopException()
208
	{
209
		$view = $this->object->getView();
210
		$param = array( 'c_step' => 'process' );
211
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
212
		$view->addHelper( 'param', $helper );
213
214
		$orderid = $this->getOrder( '2008-02-15 12:34:56' )->getId();
215
		$this->context->getSession()->set( 'aimeos/orderid', $orderid );
216
217
		$paths = \TestHelperHtml::getHtmlTemplatePaths();
218
		$mock = $this->getMockBuilder( '\\Aimeos\\Client\\Html\\Checkout\\Standard\\Process\\Standard' )
219
			->setConstructorArgs( array( $this->context, $paths ) )
220
			->setMethods( array( 'getOrderServiceCode' ) )
221
			->getMock();
222
223
		$mock->expects( $this->once() )->method( 'getOrderServiceCode' )
224
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
225
226
		$mock->setView( $view );
227
		$mock->process();
228
229
		$this->assertInternalType( 'array', $view->standardErrorList );
230
	}
231
232
233
	public function testProcessException()
234
	{
235
		$view = $this->object->getView();
236
		$param = array( 'c_step' => 'process' );
237
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
238
		$view->addHelper( 'param', $helper );
239
240
		$orderid = $this->getOrder( '2008-02-15 12:34:56' )->getId();
241
		$this->context->getSession()->set( 'aimeos/orderid', $orderid );
242
243
		$paths = \TestHelperHtml::getHtmlTemplatePaths();
244
		$mock = $this->getMockBuilder( '\\Aimeos\\Client\\Html\\Checkout\\Standard\\Process\\Standard' )
245
			->setConstructorArgs( array( $this->context, $paths ) )
246
			->setMethods( array( 'getOrderServiceCode' ) )
247
			->getMock();
248
249
		$mock->expects( $this->once() )->method( 'getOrderServiceCode' )
250
			->will( $this->throwException( new \Exception() ) );
251
252
		$mock->setView( $view );
253
		$mock->process();
254
255
		$this->assertInternalType( 'array', $view->standardErrorList );
256
	}
257
258
259
	/**
260
	 * @param string $date
261
	 */
262
	protected function getOrder( $date )
263
	{
264
		$orderManager = \Aimeos\MShop\Order\Manager\Factory::createManager( $this->context );
265
266
		$search = $orderManager->createSearch();
267
		$search->setConditions( $search->compare( '==', 'order.datepayment', $date ) );
268
269
		$result = $orderManager->searchItems( $search );
270
271
		if( ( $item = reset( $result ) ) === false ) {
272
			throw new \Exception( 'No order found' );
273
		}
274
275
		return $item;
276
	}
277
}
278