Completed
Push — master ( a13539...bd1b6d )
by Aimeos
04:27
created

StandardTest::testGetException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2020
6
 */
7
8
9
namespace Aimeos\Admin\JQAdm\Coupon\Code;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $object;
16
	private $view;
17
18
19
	protected function setUp() : void
20
	{
21
		$this->view = \TestHelperJqadm::getView();
22
		$this->context = \TestHelperJqadm::getContext();
23
24
		$this->object = new \Aimeos\Admin\JQAdm\Coupon\Code\Standard( $this->context );
25
		$this->object = new \Aimeos\Admin\JQAdm\Common\Decorator\Page( $this->object, $this->context );
26
		$this->object->setAimeos( \TestHelperJqadm::getAimeos() );
27
		$this->object->setView( $this->view );
28
	}
29
30
31
	protected function tearDown() : void
32
	{
33
		unset( $this->object, $this->view, $this->context );
34
	}
35
36
37
	public function testCopy()
38
	{
39
		$this->view->item = $this->getCouponItem();
40
41
		$result = $this->object->copy();
42
43
		$this->assertStringContainsString( 'item-code', $result );
44
	}
45
46
47
	public function testCreate()
48
	{
49
		$result = $this->object->create();
50
51
		$this->assertStringContainsString( 'item-code', $result );
52
	}
53
54
55
	public function testGet()
56
	{
57
		$this->view->item = $this->getCouponItem();
58
59
		$result = $this->object->get();
60
61
		$this->assertStringContainsString( 'item-code', $result );
62
		$this->assertStringContainsString( 'OPQR', $result );
63
	}
64
65
66
	public function testSave()
67
	{
68
		$manager = \Aimeos\MShop::create( $this->context, 'coupon/code' );
69
70
		$param = array(
71
			'site' => 'unittest',
72
			'code' => array(
73
				'coupon.code.id' => [''],
74
				'coupon.code.code' => ['jqadm test code'],
75
				'coupon.code.count' => ['10'],
76
				'coupon.code.datestart' => ['2000-01-01T00:00:00'],
77
				'coupon.code.dateend' => ['2010-01-01T00:00'],
78
				'coupon.code.ref' => ['123'],
79
			),
80
		);
81
82
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
83
		$this->view->addHelper( 'param', $helper );
84
85
		$this->view->item = $this->getCouponItem();
86
87
		$result = $this->object->save();
88
89
		$manager->deleteItem( $manager->findItem( 'jqadm test code' )->getId() );
90
91
		$this->assertEmpty( $this->view->get( 'errors' ) );
92
		$this->assertNull( $result );
93
	}
94
95
96
	public function testSaveException()
97
	{
98
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Coupon\Standard::class )
99
			->setConstructorArgs( array( $this->context, \TestHelperJqadm::getTemplatePaths() ) )
100
			->setMethods( array( 'fromArray' ) )
101
			->getMock();
102
103
		$object->expects( $this->once() )->method( 'fromArray' )
104
			->will( $this->throwException( new \RuntimeException() ) );
105
106
		$object->setView( $this->getViewNoRender() );
107
108
		$object->save();
109
	}
110
111
112
	public function testGetSubClient()
113
	{
114
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
115
		$this->object->getSubClient( 'unknown' );
116
	}
117
118
119
	protected function getViewNoRender()
120
	{
121
		$view = $this->getMockBuilder( \Aimeos\MW\View\Standard::class )
122
			->setConstructorArgs( array( [] ) )
123
			->setMethods( array( 'render', 'config' ) )
124
			->getMock();
125
126
		$view->item = $this->getCouponItem();
0 ignored issues
show
Bug introduced by
Accessing item on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
127
128
		return $view;
129
	}
130
131
132
	protected function getCouponItem( $provider = 'Example,BasketValues' )
133
	{
134
		$manager = \Aimeos\MShop::create( $this->context, 'coupon' );
135
136
		$search = $manager->createSearch();
137
		$search->setConditions( $search->compare( '==', 'coupon.provider', $provider ) );
138
139
		if( ( $item = $manager->searchItems( $search )->first() ) === null ) {
140
			throw new \RuntimeException( 'No coupon item found' );
141
		}
142
143
		return $item;
144
	}
145
}
146