1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: benedikt |
6
|
|
|
* Date: 1/3/18 |
7
|
|
|
* Time: 3:53 PM |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Tfboe\FmLib\Tests\Unit\Service { |
11
|
|
|
|
12
|
|
|
use Tfboe\FmLib\Service\AsyncExecutorService; |
13
|
|
|
use Tfboe\FmLib\Tests\Helpers\UnitTestCase; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class PlayerServiceTest |
18
|
|
|
* @package Tfboe\FmLib\Tests\Unit\Service |
19
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
20
|
|
|
*/ |
21
|
|
|
class AsyncExecutorTest extends UnitTestCase |
22
|
|
|
{ |
23
|
|
|
//<editor-fold desc="Public Methods"> |
24
|
|
|
/** |
25
|
|
|
* @covers \Tfboe\FmLib\Service\AsyncExecutorService::runBashCommand |
26
|
|
|
*/ |
27
|
|
|
public function testRunBashCommandOnLinux() |
28
|
|
|
{ |
29
|
|
|
global $uname; |
30
|
|
|
global $lastExecCommand; |
31
|
|
|
$uname = "Non-Windows"; |
32
|
|
|
$service = $this->service(); |
33
|
|
|
$lastExecCommand = null; |
34
|
|
|
$service->runBashCommand("linux-command"); |
35
|
|
|
self::assertEquals("linux-command > /dev/null &", $lastExecCommand); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @covers \Tfboe\FmLib\Service\AsyncExecutorService::runBashCommand |
40
|
|
|
*/ |
41
|
|
|
public function testRunBashCommandOnWindows() |
42
|
|
|
{ |
43
|
|
|
global $uname; |
44
|
|
|
global $lastExecCommand; |
45
|
|
|
global $lastExecFlag; |
46
|
|
|
$uname = "Windows 7.1"; |
47
|
|
|
$service = $this->service(); |
48
|
|
|
$lastExecCommand = null; |
49
|
|
|
$lastExecFlag = null; |
50
|
|
|
$service->runBashCommand("windows-command"); |
51
|
|
|
self::assertEquals("start /B windows-command", $lastExecCommand); |
52
|
|
|
self::assertEquals("r", $lastExecFlag); |
53
|
|
|
} |
54
|
|
|
//</editor-fold desc="Public Methods"> |
55
|
|
|
|
56
|
|
|
//<editor-fold desc="Protected Methods"> |
57
|
|
|
/** |
58
|
|
|
* @inheritDoc |
59
|
|
|
*/ |
60
|
|
|
protected function setUp() |
61
|
|
|
{ |
62
|
|
|
parent::setUp(); |
63
|
|
|
|
64
|
|
|
global $mockGlobalFunctions; |
65
|
|
|
$mockGlobalFunctions = true; |
66
|
|
|
} |
67
|
|
|
//</editor-fold desc="Protected Methods"> |
68
|
|
|
|
69
|
|
|
//<editor-fold desc="Private Methods"> |
70
|
|
|
/** |
71
|
|
|
* @return AsyncExecutorService |
72
|
|
|
*/ |
73
|
|
|
private function service() |
74
|
|
|
{ |
75
|
|
|
return new AsyncExecutorService(); |
76
|
|
|
} |
77
|
|
|
//</editor-fold desc="Private Methods"> |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
namespace Tfboe\FmLib\Service { |
82
|
|
|
|
83
|
|
|
$GLOBALS['mockGlobalFunctions'] = false; |
84
|
|
|
$GLOBALS['lastExecCommand'] = null; |
85
|
|
|
$GLOBALS['uname'] = null; |
86
|
|
|
$GLOBALS['lastExecFlag'] = null; |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $command |
91
|
|
|
*/ |
92
|
|
View Code Duplication |
function exec($command) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
global $mockGlobalFunctions; |
95
|
|
|
global $lastExecCommand; |
96
|
|
|
if (isset($mockGlobalFunctions) && $mockGlobalFunctions === true) { |
97
|
|
|
$lastExecCommand = $command; |
98
|
|
|
} else { |
99
|
|
|
call_user_func_array('\exec', func_get_args()); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
function php_uname() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
global $mockGlobalFunctions; |
109
|
|
|
global $uname; |
110
|
|
|
if (isset($mockGlobalFunctions) && $mockGlobalFunctions === true) { |
111
|
|
|
return $uname; |
112
|
|
|
} else { |
113
|
|
|
return call_user_func_array('\php_uname', func_get_args()); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
function pclose() |
118
|
|
|
{ |
119
|
|
|
global $mockGlobalFunctions; |
120
|
|
|
if (!isset($mockGlobalFunctions) || $mockGlobalFunctions !== true) { |
121
|
|
|
call_user_func_array('\pclose', func_get_args()); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param $command |
127
|
|
|
* @param $flag |
128
|
|
|
* @return mixed|null |
129
|
|
|
*/ |
130
|
|
|
function popen($command, $flag) |
131
|
|
|
{ |
132
|
|
|
global $mockGlobalFunctions; |
133
|
|
|
global $lastExecCommand; |
134
|
|
|
global $lastExecFlag; |
135
|
|
|
if (isset($mockGlobalFunctions) && $mockGlobalFunctions === true) { |
136
|
|
|
$lastExecCommand = $command; |
137
|
|
|
$lastExecFlag = $flag; |
138
|
|
|
return null; |
139
|
|
|
} else { |
140
|
|
|
return call_user_func_array('\popen', func_get_args()); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.