StandardTest::testInitNewAddress()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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