Completed
Push — master ( 7394d7...0c53b7 )
by Aimeos
02:18
created

StandardTest::testRunCollapsedLines()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 85
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 58
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 85
rs 8.9163

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), 2018
6
 */
7
8
9
namespace Aimeos\Controller\Jobs\Subscription\Export\Csv;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $aimeos;
15
	private $context;
16
	private $object;
17
18
19
	protected function setUp()
20
	{
21
		$this->aimeos = \TestHelperJobs::getAimeos();
22
		$this->context = \TestHelperJobs::getContext();
23
24
		$this->object = new \Aimeos\Controller\Jobs\Subscription\Export\Csv\Standard( $this->context, $this->aimeos );
25
	}
26
27
28
	protected function tearDown()
29
	{
30
		unset( $this->object, $this->context, $this->aimeos );
31
	}
32
33
34
	public function testGetName()
35
	{
36
		$this->assertEquals( 'Subscription export CSV', $this->object->getName() );
37
	}
38
39
40
	public function testGetDescription()
41
	{
42
		$this->assertEquals( 'Exports subscriptions to CSV file', $this->object->getDescription() );
43
	}
44
45
46
	public function testRun()
47
	{
48
		$mqmStub = $this->getMockBuilder( '\\Aimeos\\MW\\MQueue\\Manager\\Standard' )
49
			->setConstructorArgs( [$this->context->getConfig()] )
50
			->setMethods( ['get'] )
51
			->getMock();
52
53
		$mqStub = $this->getMockBuilder( '\\Aimeos\\MW\\MQueue\\Standard' )
54
			->disableOriginalConstructor()
55
			->setMethods( ['getQueue'] )
56
			->getMock();
57
58
		$queueStub = $this->getMockBuilder( '\\Aimeos\\MW\\MQueue\\Queue\\Standard' )
59
			->disableOriginalConstructor()
60
			->setMethods( ['del', 'get'] )
61
			->getMock();
62
63
		$msgStub = $this->getMockBuilder( '\\Aimeos\\MW\\MQueue\\Message\\Standard' )
64
			->disableOriginalConstructor()
65
			->setMethods( ['getBody'] )
66
			->getMock();
67
68
69
		$this->context->setMessageQueueManager( $mqmStub );
70
71
72
		$mqmStub->expects( $this->once() )->method( 'get' )
73
			->will( $this->returnValue( $mqStub ) );
74
75
		$mqStub->expects( $this->once() )->method( 'getQueue' )
76
			->will( $this->returnValue( $queueStub ) );
77
78
		$queueStub->expects( $this->exactly( 2 ) )->method( 'get' )
79
			->will( $this->onConsecutiveCalls( $msgStub, null ) );
80
81
		$queueStub->expects( $this->once() )->method( 'del' );
82
83
		$msgStub->expects( $this->once() )->method( 'getBody' )
84
			->will( $this->returnValue( '{"sitecode":"unittest"}' ) );
85
86
87
		$this->object->run();
88
89
90
		$jobManager = \Aimeos\MAdmin::create( $this->context, 'job' );
91
		$jobSearch = $jobManager->createSearch();
92
		$jobSearch->setConditions( $jobSearch->compare( '=~', 'job.label', 'subscription-export_' ) );
93
		$jobItems = $jobManager->searchItems( $jobSearch );
94
		$jobManager->deleteItems( array_keys( $jobItems ) );
95
96
		$this->assertEquals( 1, count( $jobItems ) );
97
98
99
		$filename = dirname( dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) ) . '/tmp/' . reset( $jobItems )->getLabel();
100
		$fp = fopen( $filename, 'r' );
101
102
		$subscription = fgetcsv( $fp );
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fgetcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
		$subscription = fgetcsv( /** @scrutinizer ignore-type */ $fp );
Loading history...
103
		$address1 = fgetcsv( $fp );
104
		$address2 = fgetcsv( $fp );
105
		$product1 = fgetcsv( $fp );
106
107
		fclose( $fp );
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
		fclose( /** @scrutinizer ignore-type */ $fp );
Loading history...
108
		unlink( $filename );
109
110
		$this->assertEquals( 'subscription', $subscription[0] );
111
		$this->assertEquals( 'address', $address1[0] );
112
		$this->assertEquals( 'address', $address2[0] );
113
		$this->assertEquals( 'product', $product1[0] );
114
	}
115
116
117
	public function testRunCollapsedLines()
118
	{
119
		$mqmStub = $this->getMockBuilder( '\\Aimeos\\MW\\MQueue\\Manager\\Standard' )
120
			->setConstructorArgs( [$this->context->getConfig()] )
121
			->setMethods( ['get'] )
122
			->getMock();
123
124
		$mqStub = $this->getMockBuilder( '\\Aimeos\\MW\\MQueue\\Standard' )
125
			->disableOriginalConstructor()
126
			->setMethods( ['getQueue'] )
127
			->getMock();
128
129
		$queueStub = $this->getMockBuilder( '\\Aimeos\\MW\\MQueue\\Queue\\Standard' )
130
			->disableOriginalConstructor()
131
			->setMethods( ['del', 'get'] )
132
			->getMock();
133
134
		$msgStub = $this->getMockBuilder( '\\Aimeos\\MW\\MQueue\\Message\\Standard' )
135
			->disableOriginalConstructor()
136
			->setMethods( ['getBody'] )
137
			->getMock();
138
139
140
		$this->context->setMessageQueueManager( $mqmStub );
141
		$this->context->getConfig()->set( 'controller/jobs/subscription/export/csv/collapse', true );
142
		$this->context->getConfig()->set( 'controller/jobs/subscription/export/csv/mapping', [
143
			'subscription' => array(
144
				0 => 'subscription.interval',
145
				1 => 'subscription.period',
146
				2 => 'subscription.ordbaseid',
147
			),
148
			'address' => array(
149
				3 => 'order.base.address.firstname',
150
				4 => 'order.base.address.lastname',
151
			),
152
			'product' => array(
153
				5 => 'order.base.product.prodcode',
154
				6 => 'order.base.product.price',
155
			),
156
		] );
157
158
159
		$mqmStub->expects( $this->once() )->method( 'get' )
160
			->will( $this->returnValue( $mqStub ) );
161
162
		$mqStub->expects( $this->once() )->method( 'getQueue' )
163
			->will( $this->returnValue( $queueStub ) );
164
165
		$queueStub->expects( $this->exactly( 2 ) )->method( 'get' )
166
			->will( $this->onConsecutiveCalls( $msgStub, null ) );
167
168
		$queueStub->expects( $this->once() )->method( 'del' );
169
170
		$msgStub->expects( $this->once() )->method( 'getBody' )
171
			->will( $this->returnValue( '{"sitecode":"unittest"}' ) );
172
173
174
		$object = new \Aimeos\Controller\Jobs\Subscription\Export\Csv\Standard( $this->context, $this->aimeos );
175
		$object->run();
176
177
178
		$jobManager = \Aimeos\MAdmin::create( $this->context, 'job' );
179
		$jobSearch = $jobManager->createSearch();
180
		$jobSearch->setConditions( $jobSearch->compare( '=~', 'job.label', 'subscription-export_' ) );
181
		$jobItems = $jobManager->searchItems( $jobSearch );
182
		$jobManager->deleteItems( array_keys( $jobItems ) );
183
184
		$this->assertEquals( 1, count( $jobItems ) );
185
186
187
		$filename = dirname( dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) ) . '/tmp/' . reset( $jobItems )->getLabel();
188
		$fp = fopen( $filename, 'r' );
189
190
		$line1 = fgetcsv( $fp );
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fgetcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

190
		$line1 = fgetcsv( /** @scrutinizer ignore-type */ $fp );
Loading history...
191
		$line2 = fgetcsv( $fp );
192
		$line3 = fgetcsv( $fp );
193
194
		fclose( $fp );
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

194
		fclose( /** @scrutinizer ignore-type */ $fp );
Loading history...
195
		unlink( $filename );
196
197
		$this->assertEquals( 'P0Y1M0W0D', $line1[0] );
198
		$this->assertEquals( 'Unittest', $line1[4] );
199
		$this->assertEquals( 'CNE', $line1[5] );
200
		$this->assertEquals( 'P1Y0M0W0D', $line2[0] );
201
		$this->assertFalse( $line3 );
0 ignored issues
show
Bug introduced by
It seems like $line3 can also be of type array; however, parameter $condition of PHPUnit_Framework_Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

201
		$this->assertFalse( /** @scrutinizer ignore-type */ $line3 );
Loading history...
202
	}
203
}
204