StandardTest::getClientMock()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 14
rs 9.9666
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-2025
6
 */
7
8
9
namespace Aimeos\Admin\JQAdm\Attribute\Media;
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 = \TestHelper::view();
22
		$this->context = \TestHelper::context();
23
24
		$this->object = new \Aimeos\Admin\JQAdm\Attribute\Media\Standard( $this->context );
25
		$this->object = new \Aimeos\Admin\JQAdm\Common\Decorator\Page( $this->object, $this->context );
26
		$this->object->setAimeos( \TestHelper::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 testCreate()
38
	{
39
		$manager = \Aimeos\MShop::create( $this->context, 'attribute' );
40
41
		$this->view->item = $manager->create();
42
		$result = $this->object->create();
43
44
		$this->assertStringContainsString( 'item-media', $result );
45
		$this->assertEmpty( $this->view->get( 'errors' ) );
46
	}
47
48
49
	public function testCopy()
50
	{
51
		$manager = \Aimeos\MShop::create( $this->context, 'attribute' );
52
53
		$this->view->item = $manager->find( 'xs', ['media'], 'product', 'size' );
54
		$result = $this->object->copy();
55
56
		$this->assertEmpty( $this->view->get( 'errors' ) );
57
		$this->assertStringContainsString( '&quot;media.preview&quot;:&quot;\/prod_97x93\/199_prod_97x93.jpg', $result );
58
	}
59
60
61
	public function testDelete()
62
	{
63
		$manager = \Aimeos\MShop::create( $this->context, 'attribute' );
64
65
		$this->view->item = $manager->create();
66
		$result = $this->object->delete();
67
68
		$this->assertEmpty( $this->view->get( 'errors' ) );
69
		$this->assertEmpty( $result );
70
	}
71
72
73
	public function testGet()
74
	{
75
		$manager = \Aimeos\MShop::create( $this->context, 'attribute' );
76
77
		$this->view->item = $manager->find( 'xs', ['media'], 'product', 'size' );
78
		$result = $this->object->get();
79
80
		$this->assertEmpty( $this->view->get( 'errors' ) );
81
		$this->assertStringContainsString( '&quot;media.preview&quot;:&quot;\/prod_97x93\/199_prod_97x93.jpg', $result );
82
	}
83
84
85
	public function testSave()
86
	{
87
		$manager = \Aimeos\MShop::create( $this->context, 'attribute' );
88
		$this->view->item = $manager->create();
89
90
91
		$param = array(
92
			'site' => 'unittest',
93
			'media' => [[
94
				'media.id' => '',
95
				'media.type' => 'default',
96
				'media.languageid' => 'de',
97
				'media.label' => 'test',
98
				'attribute.lists.type' => 'default',
99
			]],
100
		);
101
102
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
103
		$this->view->addHelper( 'param', $helper );
104
105
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
106
		$request = $this->getMockBuilder( \Psr\Http\Message\ServerRequestInterface::class )->getMock();
107
		$request->expects( $this->any() )->method( 'getUploadedFiles' )
108
			->willReturn( ['media' => [0 => ['file' => $file]]] );
109
110
		$helper = new \Aimeos\Base\View\Helper\Request\Standard( $this->view, $request, '127.0.0.1', 'test' );
111
		$this->view ->addHelper( 'request', $helper );
112
113
114
		$managerStub = $this->getMockBuilder( \Aimeos\MShop\Media\Manager\Standard::class )
115
			->setConstructorArgs( array( $this->context ) )
116
			->onlyMethods( ['upload', 'type'] )
117
			->getMock();
118
119
		\Aimeos\MShop::inject( \Aimeos\MShop\Media\Manager\Standard::class, $managerStub );
120
121
		$managerStub->method( 'type' )->willReturn( ['media'] );
122
		$managerStub->expects( $this->once() )->method( 'upload' )->willReturnArgument( 0 );
123
124
125
		$result = $this->object->save();
126
127
128
		$this->assertEmpty( $this->view->get( 'errors' ) );
129
		$this->assertEmpty( $result );
130
		$this->assertEquals( 1, count( $this->view->item->getListItems() ) );
131
132
		foreach( $this->view->item->getListItems( 'media' ) as $listItem )
133
		{
134
			$this->assertEquals( 'media', $listItem->getDomain() );
135
136
			$refItem = $listItem->getRefItem();
137
			$this->assertEquals( 'de', $refItem->getLanguageId() );
138
			$this->assertEquals( 'test', $refItem->getLabel() );
139
		}
140
141
142
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, ['site' => 'unittest', 'media' => []] );
143
		$this->view->addHelper( 'param', $helper );
144
145
		$result = $this->object->save();
146
147
		$this->assertEmpty( $this->view->get( 'errors' ) );
148
		$this->assertEmpty( $result );
149
		$this->assertEquals( 0, count( $this->view->item->getListItems() ) );
150
	}
151
152
153
	public function testSaveException()
154
	{
155
		$object = $this->getClientMock( 'fromArray' );
156
157
		$object->expects( $this->once() )->method( 'fromArray' )
158
			->will( $this->throwException( new \RuntimeException() ) );
159
160
		$this->expectException( \RuntimeException::class );
161
		$object->save();
162
	}
163
164
165
	public function testSaveMShopException()
166
	{
167
		$object = $this->getClientMock( 'fromArray' );
168
169
		$object->expects( $this->once() )->method( 'fromArray' )
170
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
171
172
		$this->expectException( \Aimeos\MShop\Exception::class );
173
		$object->save();
174
	}
175
176
177
	public function testSearch()
178
	{
179
		$this->assertEmpty( $this->object->search() );
180
	}
181
182
183
	public function testGetSubClient()
184
	{
185
		$this->expectException( \LogicException::class );
186
		$this->object->getSubClient( 'unknown' );
187
	}
188
189
190
	public function getClientMock( $method )
191
	{
192
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Attribute\Media\Standard::class )
193
			->setConstructorArgs( array( $this->context, \TestHelper::getTemplatePaths() ) )
194
			->onlyMethods( [$method] )
195
			->getMock();
196
197
		$view = \TestHelper::view();
198
		$view->item = \Aimeos\MShop::create( $this->context, 'attribute' )->create();
199
200
		$object->setAimeos( \TestHelper::getAimeos() );
201
		$object->setView( $view );
202
203
		return $object;
204
	}
205
}
206