Passed
Push — master ( 56acc7...6ec9fa )
by Aimeos
02:41
created

StandardTest::testGetSubClientInvalid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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