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

StandardTest::testSave()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 8.6981
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\Service\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\Service\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, 'service' );
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, 'service' );
52
53
		$this->view->item = $manager->find( 'unitcode', ['media'] );
54
		$result = $this->object->copy();
55
56
		$this->assertEmpty( $this->view->get( 'errors' ) );
57
		$this->assertStringContainsString( '&quot;media.preview&quot;:&quot;path\/to\/service.png&quot;', $result );
58
	}
59
60
61
	public function testDelete()
62
	{
63
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
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, 'service' );
76
77
		$this->view->item = $manager->find( 'unitcode', ['media'] );
78
		$result = $this->object->get();
79
80
		$this->assertEmpty( $this->view->get( 'errors' ) );
81
		$this->assertStringContainsString( '&quot;media.preview&quot;:&quot;path\/to\/service.png&quot;', $result );
82
	}
83
84
85
	public function testSave()
86
	{
87
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
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
				'service.lists.type' => 'default',
99
			]],
100
		);
101
102
		$helper = new \Aimeos\MW\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
			->will( $this->returnValue( ['media' => [0 => ['file' => $file]]] ) );
109
110
		$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...
111
		$this->view ->addHelper( 'request', $helper );
112
113
114
		$name = 'AdminJQAdmServiceMediaSave';
115
		$this->context->getConfig()->set( 'controller/common/media/name', $name );
116
117
		$cntlStub = $this->getMockBuilder( '\\Aimeos\\Controller\\Common\\Media\\Standard' )
118
			->setConstructorArgs( array( $this->context ) )
119
			->setMethods( array( 'add' ) )
120
			->getMock();
121
122
		\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...
123
124
		$cntlStub->expects( $this->once() )->method( 'add' )->will( $this->returnArgument( 0 ) );
125
126
127
		$result = $this->object->save();
128
129
130
		$this->assertEmpty( $this->view->get( 'errors' ) );
131
		$this->assertEmpty( $result );
132
		$this->assertEquals( 1, count( $this->view->item->getListItems() ) );
133
134
		foreach( $this->view->item->getListItems( 'media' ) as $listItem )
135
		{
136
			$this->assertEquals( 'media', $listItem->getDomain() );
137
138
			$refItem = $listItem->getRefItem();
139
			$this->assertEquals( 'de', $refItem->getLanguageId() );
140
			$this->assertEquals( 'test', $refItem->getLabel() );
141
		}
142
143
144
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, ['site' => 'unittest', 'media' => []] );
145
		$this->view->addHelper( 'param', $helper );
146
147
		$result = $this->object->save();
148
149
		$this->assertEmpty( $this->view->get( 'errors' ) );
150
		$this->assertEmpty( $result );
151
		$this->assertEquals( 0, count( $this->view->item->getListItems() ) );
152
	}
153
154
155
	public function testSaveException()
156
	{
157
		$object = $this->getClientMock( 'fromArray' );
158
159
		$object->expects( $this->once() )->method( 'fromArray' )
160
			->will( $this->throwException( new \RuntimeException() ) );
161
162
		$this->expectException( \RuntimeException::class );
163
		$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...
164
	}
165
166
167
	public function testSaveMShopException()
168
	{
169
		$object = $this->getClientMock( 'fromArray' );
170
171
		$object->expects( $this->once() )->method( 'fromArray' )
172
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
173
174
		$this->expectException( \Aimeos\MShop\Exception::class );
175
		$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...
176
	}
177
178
179
	public function testSearch()
180
	{
181
		$this->assertEmpty( $this->object->search() );
182
	}
183
184
185
	public function testGetSubClient()
186
	{
187
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
188
		$this->object->getSubClient( 'unknown' );
189
	}
190
191
192
	public function getClientMock( $method )
193
	{
194
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Service\Media\Standard::class )
195
			->setConstructorArgs( array( $this->context, \TestHelperJqadm::getTemplatePaths() ) )
196
			->setMethods( [$method] )
197
			->getMock();
198
199
		$view = \TestHelperJqadm::getView();
200
		$view->item = \Aimeos\MShop::create( $this->context, 'service' )->create();
201
202
		$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...
203
		$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...
204
205
		return $object;
206
	}
207
}
208