StandardTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2013
6
 * @copyright Aimeos (aimeos.org), 2015-2025
7
 */
8
9
namespace Aimeos\Client\Html\Checkout\Standard\Address\Delivery;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $object;
15
	private $context;
16
	private $view;
17
18
19
	protected function setUp() : void
20
	{
21
		\Aimeos\Controller\Frontend::cache( true );
22
		\Aimeos\MShop::cache( true );
23
24
		$this->view = \TestHelper::view();
25
		$this->context = \TestHelper::context();
26
		$this->context->setUser( \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' ) );
27
28
		$this->object = new \Aimeos\Client\Html\Checkout\Standard\Address\Delivery\Standard( $this->context );
29
		$this->object->setView( $this->view );
30
	}
31
32
33
	protected function tearDown() : void
34
	{
35
		\Aimeos\Controller\Frontend::create( $this->context, 'basket' )->clear();
36
		\Aimeos\Controller\Frontend::cache( false );
37
		\Aimeos\MShop::cache( false );
38
39
		unset( $this->object, $this->context, $this->view );
40
	}
41
42
43
	public function testBody()
44
	{
45
		$this->view->standardBasket = \Aimeos\MShop::create( $this->context, 'order' )->create();
46
		$this->object->setView( $this->object->data( $this->view ) );
47
48
		$output = $this->object->body();
49
		$this->assertStringStartsWith( '<div class="checkout-standard-address-delivery', $output );
50
51
		$this->assertGreaterThan( 0, count( $this->view->addressDeliveryCss ) );
52
	}
53
54
55
	public function testInit()
56
	{
57
		$this->object->init();
58
59
		$this->assertEmpty( $this->view->get( 'addressDeliveryError' ) );
60
	}
61
62
63
	public function testInitNewAddress()
64
	{
65
		$this->view = \TestHelper::view();
66
67
		$param = array(
68
			'ca_deliveryoption' => 'null',
69
			'ca_delivery' => array(
70
				'order.address.salutation' => 'mr',
71
				'order.address.firstname' => 'test',
72
				'order.address.lastname' => 'user',
73
				'order.address.address1' => 'mystreet 1',
74
				'order.address.postal' => '20000',
75
				'order.address.city' => 'hamburg',
76
				'order.address.languageid' => 'en',
77
			),
78
		);
79
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
80
		$this->view->addHelper( 'param', $helper );
81
82
		$this->object->setView( $this->view );
83
84
		$this->object->init();
85
86
		$basket = \Aimeos\Controller\Frontend::create( $this->context, 'basket' )->get();
87
		$this->assertEquals( 'hamburg', $basket->getAddress( 'delivery', 0 )->getCity() );
88
	}
89
90
91
	public function testInitNewAddressMissing()
92
	{
93
		$this->view = \TestHelper::view();
94
95
		$param = array(
96
			'ca_deliveryoption' => 'null',
97
			'ca_delivery' => array(
98
				'order.address.firstname' => 'test',
99
				'order.address.lastname' => 'user',
100
				'order.address.address1' => 'mystreet 1',
101
				'order.address.postal' => '20000',
102
				'order.address.city' => 'hamburg',
103
			),
104
		);
105
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
106
		$this->view->addHelper( 'param', $helper );
107
108
		$this->object->setView( $this->view );
109
110
		try
111
		{
112
			$this->object->init();
113
		}
114
		catch( \Aimeos\Client\Html\Exception $e )
115
		{
116
			$this->assertEquals( 1, count( $this->view->addressDeliveryError ) );
117
			$this->assertArrayHasKey( 'languageid', $this->view->addressDeliveryError );
118
			return;
119
		}
120
121
		$this->fail( 'Expected exception not thrown' );
122
	}
123
124
125
	public function testInitNewAddressUnknown()
126
	{
127
		$this->view = \TestHelper::view();
128
129
		$param = array(
130
			'ca_deliveryoption' => 'null',
131
			'ca_delivery' => array(
132
				'order.address.salutation' => 'mr',
133
				'order.address.firstname' => 'test',
134
				'order.address.lastname' => 'user',
135
				'order.address.address1' => 'mystreet 1',
136
				'order.address.postal' => '20000',
137
				'order.address.city' => 'hamburg',
138
				'order.address.languageid' => 'en',
139
			),
140
		);
141
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
142
		$this->view->addHelper( 'param', $helper );
143
144
		$this->object->setView( $this->view );
145
		$this->object->init();
146
147
		$basket = \Aimeos\Controller\Frontend::create( $this->context, 'basket' )->get();
148
		$this->assertEquals( 'test', $basket->getAddress( 'delivery', 0 )->getFirstName() );
149
	}
150
151
152
	public function testInitNewAddressInvalid()
153
	{
154
		$this->view = \TestHelper::view();
155
156
		$config = $this->context->config();
157
		$config->set( 'client/html/common/address/validate/postal', '^[0-9]{5}$' );
158
		$helper = new \Aimeos\Base\View\Helper\Config\Standard( $this->view, $config );
159
		$this->view->addHelper( 'config', $helper );
160
161
		$param = array(
162
			'ca_deliveryoption' => 'null',
163
			'ca_delivery' => array(
164
				'order.address.salutation' => 'mr',
165
				'order.address.firstname' => 'test',
166
				'order.address.lastname' => 'user',
167
				'order.address.address1' => 'mystreet 1',
168
				'order.address.postal' => '20AB',
169
				'order.address.city' => 'hamburg',
170
				'order.address.email' => '[email protected]',
171
				'order.address.languageid' => 'en',
172
			),
173
		);
174
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
175
		$this->view->addHelper( 'param', $helper );
176
177
		$this->object->setView( $this->view );
178
179
		try
180
		{
181
			$this->object->init();
182
		}
183
		catch( \Aimeos\Client\Html\Exception $e )
184
		{
185
			$this->assertEquals( 1, count( $this->view->addressDeliveryError ) );
186
			$this->assertArrayHasKey( 'postal', $this->view->addressDeliveryError );
187
			return;
188
		}
189
190
		$this->fail( 'Expected exception not thrown' );
191
	}
192
193
194
	public function testInitAddressDelete()
195
	{
196
		$customer = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]', ['customer/address'] );
197
		$id = $customer->getAddressItems()->first()->getId();
198
199
		$this->view = \TestHelper::view();
200
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, ['ca_delivery_delete' => $id] );
201
		$this->view->addHelper( 'param', $helper );
202
		$this->object->setView( $this->view );
203
204
		$customerStub = $this->getMockBuilder( \Aimeos\Controller\Frontend\Customer\Standard::class )
205
			->setConstructorArgs( array( $this->context ) )
206
			->onlyMethods( array( 'deleteAddressItem', 'store' ) )
207
			->getMock();
208
209
		$customerStub->expects( $this->once() )->method( 'deleteAddressItem' )->willReturn( $customerStub );
210
		$customerStub->expects( $this->once() )->method( 'store' )->willReturn( $customerStub );
211
212
		\Aimeos\Controller\Frontend::inject( \Aimeos\Controller\Frontend\Customer\Standard::class, $customerStub );
213
214
		$this->expectException( \Aimeos\Client\Html\Exception::class );
215
		$this->object->init();
216
	}
217
218
219
	public function testInitExistingAddress()
220
	{
221
		$customer = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]', ['customer/address'] );
222
223
		$this->view = \TestHelper::view();
224
		$param = array( 'ca_deliveryoption' => $customer->getAddressItems()->first()->getId() );
225
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
226
		$this->view->addHelper( 'param', $helper );
227
		$this->object->setView( $this->view );
228
229
		$this->object->init();
230
231
		$basket = \Aimeos\Controller\Frontend::create( $this->context, 'basket' )->get();
232
		$this->assertEquals( 'Example company', $basket->getAddress( 'delivery', 0 )->getCompany() );
233
	}
234
235
236
	public function testInitExistingAddressInvalid()
237
	{
238
		$this->view = \TestHelper::view();
239
		$param = [
240
			'ca_deliveryoption' => -2,
241
			'ca_delivery_-2' => [
242
				'order.address.languageid' => 'de',
243
				'order.address.salutation' => 'mr',
244
				'order.address.firstname' => 'test',
245
				'order.address.lastname' => 'user',
246
				'order.address.address1' => 'street',
247
				'order.address.postal' => '1234',
248
				'order.address.city' => 'test city',
249
			]
250
		];
251
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
252
		$this->view->addHelper( 'param', $helper );
253
		$this->object->setView( $this->view );
254
255
		$this->object->init();
256
257
		$basket = \Aimeos\Controller\Frontend::create( $this->context, 'basket' )->get();
258
		$this->assertEquals( 'mr', $basket->getAddress( 'delivery', 0 )->getSalutation() );
259
	}
260
261
262
	public function testInitRemoveAddress()
263
	{
264
		$this->view = \TestHelper::view();
265
		$param = array( 'ca_delivery_delete' => -1 );
266
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
267
		$this->view->addHelper( 'param', $helper );
268
		$this->object->setView( $this->view );
269
270
		$this->object->init();
271
272
		$basket = \Aimeos\Controller\Frontend::create( $this->context, 'basket' )->get();
273
		$this->assertCount( 0, $basket->getAddress( 'delivery' ) );
274
	}
275
}
276