Completed
Push — master ( ebb2a9...d8d2d3 )
by Aimeos
10:03
created

StandardTest::testSave()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 8.72
c 0
b 0
f 0
cc 2
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2021
6
 */
7
8
9
namespace Aimeos\Admin\JQAdm\Catalog\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 = \TestHelperJqadm::getView();
22
		$this->context = \TestHelperJqadm::getContext();
23
24
		$this->object = new \Aimeos\Admin\JQAdm\Catalog\Media\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 testCreate()
38
	{
39
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
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, 'catalog' );
52
53
		$this->view->item = $manager->find( 'cafe', ['media'] );
54
		$result = $this->object->copy();
55
56
		$this->assertEmpty( $this->view->get( 'errors' ) );
57
		$this->assertStringContainsString( '&quot;media.preview&quot;:&quot;prod_123x103\/195_prod_123x103.jpg&quot;', $result );
58
	}
59
60
61
	public function testDelete()
62
	{
63
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
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, 'catalog' );
76
77
		$this->view->item = $manager->find( 'cafe', ['media'] );
78
		$result = $this->object->get();
79
80
		$this->assertEmpty( $this->view->get( 'errors' ) );
81
		$this->assertStringContainsString( '&quot;media.preview&quot;:&quot;prod_123x103\/195_prod_123x103.jpg&quot;', $result );
82
	}
83
84
85
	public function testSave()
86
	{
87
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
88
		$this->view->item = $manager->create();
89
90
		$param = array(
91
			'site' => 'unittest',
92
			'media' => [[
93
				'media.id' => '',
94
				'media.type' => 'default',
95
				'media.languageid' => 'de',
96
				'media.label' => 'test',
97
				'catalog.lists.type' => 'default',
98
			]],
99
		);
100
101
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
102
		$this->view->addHelper( 'param', $helper );
103
104
		$file = $this->getMockBuilder( \Psr\Http\Message\UploadedFileInterface::class )->getMock();
105
		$request = $this->getMockBuilder( \Psr\Http\Message\ServerRequestInterface::class )->getMock();
106
		$request->expects( $this->any() )->method( 'getUploadedFiles' )
107
			->will( $this->returnValue( ['media' => [0 => ['file' => $file]]] ) );
108
109
		$helper = new \Aimeos\MW\View\Helper\Request\Standard( $this->view, $request, '127.0.0.1', 'test' );
0 ignored issues
show
Documentation introduced by
$request is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Http\Message\ServerRequestInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
110
		$this->view ->addHelper( 'request', $helper );
111
112
113
		$name = 'AdminJQAdmCatalogMediaSave';
114
		$this->context->getConfig()->set( 'controller/common/media/name', $name );
115
116
		$cntlStub = $this->getMockBuilder( '\\Aimeos\\Controller\\Common\\Media\\Standard' )
117
			->setConstructorArgs( array( $this->context ) )
118
			->setMethods( array( 'add' ) )
119
			->getMock();
120
121
		\Aimeos\Controller\Common\Media\Factory::inject( '\\Aimeos\\Controller\\Common\\Media\\' . $name, $cntlStub );
0 ignored issues
show
Documentation introduced by
$cntlStub is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a null|object<Aimeos\Controller\Common\Media\Iface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
122
123
		$cntlStub->expects( $this->once() )->method( 'add' )->will( $this->returnArgument( 0 ) );
124
125
126
		$result = $this->object->save();
127
128
129
		$this->assertEmpty( $this->view->get( 'errors' ) );
130
		$this->assertEmpty( $result );
131
		$this->assertEquals( 1, count( $this->view->item->getListItems() ) );
132
133
		foreach( $this->view->item->getListItems( 'media' ) as $listItem )
134
		{
135
			$this->assertEquals( 'media', $listItem->getDomain() );
136
137
			$refItem = $listItem->getRefItem();
138
			$this->assertEquals( 'de', $refItem->getLanguageId() );
139
			$this->assertEquals( 'test', $refItem->getLabel() );
140
		}
141
142
143
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, ['site' => 'unittest', 'media' => []] );
144
		$this->view->addHelper( 'param', $helper );
145
146
		$result = $this->object->save();
147
148
		$this->assertEmpty( $this->view->get( 'errors' ) );
149
		$this->assertEmpty( $result );
150
		$this->assertEquals( 0, count( $this->view->item->getListItems() ) );
151
	}
152
153
154
	public function testSaveException()
155
	{
156
		$object = $this->getClientMock( 'fromArray' );
157
158
		$object->expects( $this->once() )->method( 'fromArray' )
159
			->will( $this->throwException( new \RuntimeException() ) );
160
161
		$this->expectException( \RuntimeException::class );
162
		$object->save();
0 ignored issues
show
Bug introduced by
The method save() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
163
	}
164
165
166
	public function testSaveMShopException()
167
	{
168
		$object = $this->getClientMock( 'fromArray' );
169
170
		$object->expects( $this->once() )->method( 'fromArray' )
171
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
172
173
		$this->expectException( \Aimeos\MShop\Exception::class );
174
		$object->save();
0 ignored issues
show
Bug introduced by
The method save() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
175
	}
176
177
178
	public function testSearch()
179
	{
180
		$this->assertEmpty( $this->object->search() );
181
	}
182
183
184
	public function testGetSubClient()
185
	{
186
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
187
		$this->object->getSubClient( 'unknown' );
188
	}
189
190
191
	public function getClientMock( $method )
192
	{
193
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Catalog\Media\Standard::class )
194
			->setConstructorArgs( array( $this->context, \TestHelperJqadm::getTemplatePaths() ) )
195
			->setMethods( [$method] )
196
			->getMock();
197
198
		$view = \TestHelperJqadm::getView();
199
		$view->item = \Aimeos\MShop::create( $this->context, 'catalog' )->create();
200
201
		$object->setAimeos( \TestHelperJqadm::getAimeos() );
0 ignored issues
show
Bug introduced by
The method setAimeos() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
202
		$object->setView( $view );
0 ignored issues
show
Bug introduced by
The method setView() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
203
204
		return $object;
205
	}
206
}
207