StandardTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 55
c 3
b 0
f 0
dl 0
loc 102
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 3 1
A testGetName() 0 3 1
A setUp() 0 6 1
A testGetDescription() 0 3 1
A testRun() 0 68 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2025
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() : void
20
	{
21
		$this->aimeos = \TestHelper::getAimeos();
22
		$this->context = \TestHelper::context();
23
24
		$this->object = new \Aimeos\Controller\Jobs\Subscription\Export\Csv\Standard( $this->context, $this->aimeos );
25
	}
26
27
28
	protected function tearDown() : void
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\\Base\\MQueue\\Manager\\Standard' )
49
			->setConstructorArgs( [[]] )
50
			->onlyMethods( ['get'] )
51
			->getMock();
52
53
		$mqStub = $this->getMockBuilder( '\\Aimeos\\Base\\MQueue\\Standard' )
54
			->disableOriginalConstructor()
55
			->onlyMethods( ['getQueue'] )
56
			->getMock();
57
58
		$queueStub = $this->getMockBuilder( '\\Aimeos\\Base\\MQueue\\Queue\\Standard' )
59
			->disableOriginalConstructor()
60
			->onlyMethods( ['del', 'get'] )
61
			->getMock();
62
63
		$msgStub = $this->getMockBuilder( '\\Aimeos\\Base\\MQueue\\Message\\Standard' )
64
			->disableOriginalConstructor()
65
			->onlyMethods( ['getBody'] )
66
			->getMock();
67
68
69
		$this->context->setMessageQueueManager( $mqmStub );
70
71
72
		$mqmStub->expects( $this->once() )->method( 'get' )
73
			->willReturn( $mqStub );
74
75
		$mqStub->expects( $this->once() )->method( 'getQueue' )
76
			->willReturn( $queueStub );
77
78
		$queueStub->expects( $this->exactly( 2 ) )->method( 'get' )
79
			->willReturn( $msgStub, null );
80
81
		$queueStub->expects( $this->once() )->method( 'del' );
82
83
		$msgStub->expects( $this->once() )->method( 'getBody' )
84
			->willReturn( '{"sitecode":"unittest"}' );
85
86
87
		$this->object->run();
88
89
90
		$jobManager = \Aimeos\MAdmin::create( $this->context, 'job' );
91
		$jobSearch = $jobManager->filter();
92
		$jobSearch->setConditions( $jobSearch->compare( '=~', 'job.label', 'subscription-export_' ) );
93
		$jobItems = $jobManager->search( $jobSearch );
94
		$jobManager->delete( $jobItems->toArray() );
95
96
		$this->assertEquals( 1, count( $jobItems ) );
97
98
99
		$filename = dirname( dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) ) . '/tmp/' . $jobItems->first()->getLabel();
100
		$fp = fopen( $filename, 'r' );
101
102
		$subscription = fgetcsv( $fp, null, ',', '"', '' );
103
		$address1 = fgetcsv( $fp, null, ',', '"', '' );
104
		$address2 = fgetcsv( $fp, null, ',', '"', '' );
105
		$product1 = fgetcsv( $fp, null, ',', '"', '' );
106
107
		fclose( $fp );
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