Passed
Push — master ( ed6c8e...268125 )
by Aimeos
03:53
created

StandardTest::testProcessNewAddressInvalid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 39
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 26
nc 2
nop 0
dl 0
loc 39
rs 9.504
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-2021
7
 */
8
9
10
namespace Aimeos\Client\Html\Checkout\Standard\Address\Billing;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $object;
16
	private $context;
17
18
19
	protected function setUp() : void
20
	{
21
		\Aimeos\Controller\Frontend::cache( true );
22
23
		$this->context = \TestHelperHtml::getContext();
24
		$this->context->setUserId( \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' )->getId() );
25
26
		$this->object = new \Aimeos\Client\Html\Checkout\Standard\Address\Billing\Standard( $this->context );
27
		$this->object->setView( \TestHelperHtml::getView() );
28
	}
29
30
31
	protected function tearDown() : void
32
	{
33
		\Aimeos\Controller\Frontend\Basket\Factory::create( $this->context )->clear();
34
		\Aimeos\Controller\Frontend::cache( false );
35
36
		unset( $this->object, $this->context );
37
	}
38
39
40
	public function testBody()
41
	{
42
		$view = \TestHelperHtml::getView();
43
		$view->standardBasket = \Aimeos\MShop::create( $this->context, 'order/base' )->create();
44
		$view->addressPaymentItem = \Aimeos\MShop::create( $this->context, 'order/base/address' )->create();
45
		$this->object->setView( $this->object->data( $view ) );
46
47
		$output = $this->object->body();
48
		$this->assertStringStartsWith( '<div class="checkout-standard-address-billing', $output );
49
		$this->assertRegexp( '/form-item form-group city.*form-item form-group postal/smU', $output );
50
51
		$this->assertGreaterThan( 0, count( $view->addressBillingMandatory ) );
52
		$this->assertGreaterThan( 0, count( $view->addressBillingOptional ) );
53
	}
54
55
56
	public function testGetSubClientInvalid()
57
	{
58
		$this->expectException( '\\Aimeos\\Client\\Html\\Exception' );
59
		$this->object->getSubClient( 'invalid', 'invalid' );
60
	}
61
62
63
	public function testGetSubClientInvalidName()
64
	{
65
		$this->expectException( '\\Aimeos\\Client\\Html\\Exception' );
66
		$this->object->getSubClient( '$$$', '$$$' );
67
	}
68
69
70
	public function testInit()
71
	{
72
		$this->object->init();
73
74
		$this->assertEmpty( $this->object->getView()->get( 'addressBillingError' ) );
75
	}
76
77
78
	public function testInitNewAddress()
79
	{
80
		$view = \TestHelperHtml::getView();
81
82
		$param = array(
83
			'ca_billingoption' => 'null',
84
			'ca_billing' => array(
85
				'order.base.address.salutation' => 'mr',
86
				'order.base.address.firstname' => 'test',
87
				'order.base.address.lastname' => 'user',
88
				'order.base.address.address1' => 'mystreet 1',
89
				'order.base.address.postal' => '20000',
90
				'order.base.address.city' => 'hamburg',
91
				'order.base.address.email' => '[email protected]',
92
				'order.base.address.languageid' => 'en',
93
			),
94
		);
95
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
96
		$view->addHelper( 'param', $helper );
97
98
		$this->object->setView( $view );
99
100
		$this->object->init();
101
102
		$basket = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->context )->get();
103
		$this->assertEquals( 'hamburg', $basket->getAddress( 'payment', 0 )->getCity() );
104
	}
105
106
107
	public function testInitNewAddressMissing()
108
	{
109
		$view = \TestHelperHtml::getView();
110
111
		$param = array(
112
			'ca_billingoption' => 'null',
113
			'ca_billing' => array(
114
				'order.base.address.firstname' => 'test',
115
				'order.base.address.lastname' => 'user',
116
				'order.base.address.address1' => 'mystreet 1',
117
				'order.base.address.postal' => '20000',
118
				'order.base.address.city' => 'hamburg',
119
			),
120
		);
121
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
122
		$view->addHelper( 'param', $helper );
123
124
		$this->object->setView( $view );
125
126
		try
127
		{
128
			$this->object->init();
129
		}
130
		catch( \Aimeos\Client\Html\Exception $e )
131
		{
132
			$this->assertEquals( 2, count( $view->addressBillingError ) );
133
			$this->assertArrayHasKey( 'order.base.address.email', $view->addressBillingError );
134
			$this->assertArrayHasKey( 'order.base.address.languageid', $view->addressBillingError );
135
			return;
136
		}
137
138
		$this->fail( 'Expected exception not thrown' );
139
	}
140
141
142
	public function testInitNewAddressUnknown()
143
	{
144
		$view = \TestHelperHtml::getView();
145
146
		$param = array(
147
			'ca_billingoption' => 'null',
148
			'ca_billing' => array(
149
				'order.base.address.salutation' => 'mr',
150
				'order.base.address.firstname' => 'test',
151
				'order.base.address.lastname' => 'user',
152
				'order.base.address.address1' => 'mystreet 1',
153
				'order.base.address.postal' => '20000',
154
				'order.base.address.city' => 'hamburg',
155
				'order.base.address.email' => '[email protected]',
156
				'order.base.address.languageid' => 'en',
157
			),
158
		);
159
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
160
		$view->addHelper( 'param', $helper );
161
162
		$this->object->setView( $view );
163
		$this->object->init();
164
165
		$basket = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->context )->get();
166
		$this->assertEquals( 'test', $basket->getAddress( 'payment', 0 )->getFirstName() );
167
	}
168
169
170
	public function testInitNewAddressInvalid()
171
	{
172
		$view = \TestHelperHtml::getView();
173
174
		$config = $this->context->getConfig();
175
		$config->set( 'client/html/checkout/standard/address/validate/postal', '^[0-9]{5}$' );
176
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
177
		$view->addHelper( 'config', $helper );
178
179
		$param = array(
180
			'ca_billingoption' => 'null',
181
			'ca_billing' => array(
182
				'order.base.address.salutation' => 'mr',
183
				'order.base.address.firstname' => 'test',
184
				'order.base.address.lastname' => 'user',
185
				'order.base.address.address1' => 'mystreet 1',
186
				'order.base.address.postal' => '20AB',
187
				'order.base.address.city' => 'hamburg',
188
				'order.base.address.email' => '[email protected]',
189
				'order.base.address.languageid' => 'en',
190
			),
191
		);
192
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
193
		$view->addHelper( 'param', $helper );
194
195
		$this->object->setView( $view );
196
197
		try
198
		{
199
			$this->object->init();
200
		}
201
		catch( \Aimeos\Client\Html\Exception $e )
202
		{
203
			$this->assertEquals( 1, count( $view->addressBillingError ) );
204
			$this->assertArrayHasKey( 'order.base.address.postal', $view->addressBillingError );
205
			return;
206
		}
207
208
		$this->fail( 'Expected exception not thrown' );
209
	}
210
211
212
	public function testInitExistingAddress()
213
	{
214
		$customer = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]', ['customer/address'] );
215
		$id = $customer->getAddressItems()->first()->getId();
216
217
		$view = \TestHelperHtml::getView();
218
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, ['ca_billingoption' => $id] );
219
		$view->addHelper( 'param', $helper );
220
		$this->object->setView( $view );
221
222
		$customerStub = $this->getMockBuilder( \Aimeos\Controller\Frontend\Customer\Standard::class )
223
			->setConstructorArgs( array( $this->context ) )
224
			->setMethods( ['store'] )
225
			->getMock();
226
227
		$customerStub->expects( $this->once() )->method( 'store' )->will( $this->returnSelf() );
228
229
		\Aimeos\Controller\Frontend::inject( 'customer', $customerStub );
230
231
		$this->object->init();
232
233
		$basket = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->context )->get();
234
		$this->assertEquals( 'Example company', $basket->getAddress( 'payment', 0 )->getCompany() );
235
	}
236
}
237