Skip to content

Commit 7473835

Browse files
authored
Merge pull request jobbyphp#61 from garethellis36/make-tests-pass-on-windows
Make tests pass on Windows
2 parents a0ce115 + 1790c48 commit 7473835

File tree

3 files changed

+75
-11
lines changed

3 files changed

+75
-11
lines changed

tests/BackgroundJobTest.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class BackgroundJobTest extends \PHPUnit_Framework_TestCase
2121
*/
2222
private $logFile;
2323

24+
/**
25+
* @var Helper
26+
*/
27+
private $helper;
28+
2429
/**
2530
* {@inheritdoc}
2631
*/
@@ -30,6 +35,8 @@ protected function setUp()
3035
if (file_exists($this->logFile)) {
3136
unlink($this->logFile);
3237
}
38+
39+
$this->helper = new Helper();
3340
}
3441

3542
/**
@@ -89,11 +96,19 @@ public function testInvalidCommand()
8996
$this->runJob(['command' => 'invalid-command']);
9097

9198
$this->assertContains('invalid-command', $this->getLogContent());
92-
$this->assertContains('not found', $this->getLogContent());
93-
$this->assertContains(
94-
"ERROR: Job exited with status '127'",
95-
$this->getLogContent()
96-
);
99+
100+
if ($this->helper->getPlatform() === Helper::UNIX) {
101+
$this->assertContains('not found', $this->getLogContent());
102+
$this->assertContains(
103+
"ERROR: Job exited with status '127'",
104+
$this->getLogContent()
105+
);
106+
} else {
107+
$this->assertContains(
108+
'not recognized as an internal or external command',
109+
$this->getLogContent()
110+
);
111+
}
97112
}
98113

99114
/**
@@ -207,6 +222,10 @@ public function testMailShouldTriggerHelper()
207222
*/
208223
public function testCheckMaxRuntime()
209224
{
225+
if ($this->helper->getPlatform() !== Helper::UNIX) {
226+
$this->markTestSkipped("'maxRuntime' is not supported on Windows");
227+
}
228+
210229
$helper = $this->getMock('Jobby\Helper', ['getLockLifetime']);
211230
$helper->expects($this->once())
212231
->method('getLockLifetime')
@@ -229,6 +248,10 @@ public function testCheckMaxRuntime()
229248
*/
230249
public function testCheckMaxRuntimeShouldFailIsExceeded()
231250
{
251+
if ($this->helper->getPlatform() !== Helper::UNIX) {
252+
$this->markTestSkipped("'maxRuntime' is not supported on Windows");
253+
}
254+
232255
$helper = $this->getMock('Jobby\Helper', ['getLockLifetime']);
233256
$helper->expects($this->once())
234257
->method('getLockLifetime')

tests/HelperTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ class HelperTest extends \PHPUnit_Framework_TestCase
2525
*/
2626
private $lockFile;
2727

28+
/**
29+
* @var string
30+
*/
31+
private $copyOfLockFile;
32+
2833
/**
2934
* {@inheritdoc}
3035
*/
@@ -33,6 +38,7 @@ protected function setUp()
3338
$this->helper = new Helper();
3439
$this->tmpDir = $this->helper->getTempDir();
3540
$this->lockFile = $this->tmpDir . '/test.lock';
41+
$this->copyOfLockFile = $this->tmpDir . "/test.lock.copy";
3642
}
3743

3844
/**
@@ -106,7 +112,17 @@ public function testAquireAndReleaseLock()
106112
public function testLockFileShouldContainCurrentPid()
107113
{
108114
$this->helper->acquireLock($this->lockFile);
109-
$this->assertEquals(getmypid(), file_get_contents($this->lockFile));
115+
116+
//on Windows, file locking is mandatory not advisory, so you can't do file_get_contents on a locked file
117+
//therefore, we need to make a copy of the lock file in order to read its contents
118+
if ($this->helper->getPlatform() === Helper::WINDOWS) {
119+
copy($this->lockFile, $this->copyOfLockFile);
120+
$lockFile = $this->copyOfLockFile;
121+
} else {
122+
$lockFile = $this->lockFile;
123+
}
124+
125+
$this->assertEquals(getmypid(), file_get_contents($lockFile));
110126

111127
$this->helper->releaseLock($this->lockFile);
112128
$this->assertEmpty(file_get_contents($this->lockFile));
@@ -136,6 +152,10 @@ public function testLockLifetimeShouldBeZeroIfFileIsEmpty()
136152
*/
137153
public function testLockLifetimeShouldBeZeroIfItContainsAInvalidPid()
138154
{
155+
if ($this->helper->getPlatform() === Helper::WINDOWS) {
156+
$this->markTestSkipped("Test relies on posix_ functions");
157+
}
158+
139159
file_put_contents($this->lockFile, 'invalid-pid');
140160
$this->assertEquals(0, $this->helper->getLockLifetime($this->lockFile));
141161
}
@@ -145,6 +165,10 @@ public function testLockLifetimeShouldBeZeroIfItContainsAInvalidPid()
145165
*/
146166
public function testGetLocklifetime()
147167
{
168+
if ($this->helper->getPlatform() === Helper::WINDOWS) {
169+
$this->markTestSkipped("Test relies on posix_ functions");
170+
}
171+
148172
$this->helper->acquireLock($this->lockFile);
149173

150174
$this->assertEquals(0, $this->helper->getLockLifetime($this->lockFile));

tests/JobbyTest.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Jobby\Tests;
44

5+
use Jobby\Helper;
56
use Jobby\Jobby;
67
use SuperClosure\SerializableClosure;
78

@@ -15,6 +16,11 @@ class JobbyTest extends \PHPUnit_Framework_TestCase
1516
*/
1617
private $logFile;
1718

19+
/**
20+
* @var Helper
21+
*/
22+
private $helper;
23+
1824
/**
1925
* {@inheritdoc}
2026
*/
@@ -24,6 +30,8 @@ protected function setUp()
2430
if (file_exists($this->logFile)) {
2531
unlink($this->logFile);
2632
}
33+
34+
$this->helper = new Helper();
2735
}
2836

2937
/**
@@ -54,7 +62,7 @@ public function testShell()
5462
$jobby->run();
5563

5664
// Job runs asynchronously, so wait a bit
57-
sleep(1);
65+
sleep($this->getSleepTime());
5866

5967
$this->assertEquals('Hello World!', $this->getLogContent());
6068
}
@@ -83,7 +91,7 @@ public function testSuperClosure()
8391
$jobby->run();
8492

8593
// Job runs asynchronously, so wait a bit
86-
sleep(1);
94+
sleep($this->getSleepTime());
8795

8896
$this->assertEquals('Another function!', $this->getLogContent());
8997
}
@@ -110,7 +118,7 @@ public function testClosure()
110118
$jobby->run();
111119

112120
// Job runs asynchronously, so wait a bit
113-
sleep(1);
121+
sleep($this->getSleepTime());
114122

115123
$this->assertEquals('A function!', $this->getLogContent());
116124
}
@@ -147,7 +155,7 @@ public function testShouldRunAllJobsAdded()
147155
$jobby->run();
148156

149157
// Job runs asynchronously, so wait a bit
150-
sleep(1);
158+
sleep($this->getSleepTime());
151159

152160
$this->assertContains('job-1', $this->getLogContent());
153161
$this->assertContains('job-2', $this->getLogContent());
@@ -174,7 +182,7 @@ public function testDefaultOptionsShouldBeMerged()
174182
$jobby->run();
175183

176184
// Job runs asynchronously, so wait a bit
177-
sleep(1);
185+
sleep($this->getSleepTime());
178186

179187
$this->assertEquals('A function!', $this->getLogContent());
180188
}
@@ -272,6 +280,10 @@ public function testShouldRunJobsAsync()
272280

273281
public function testShouldFailIfMaxRuntimeExceeded()
274282
{
283+
if ($this->helper->getPlatform() === Helper::WINDOWS) {
284+
$this->markTestSkipped("'maxRuntime' is not supported on Windows");
285+
}
286+
275287
$jobby = new Jobby();
276288
$jobby->add(
277289
'slow job',
@@ -298,4 +310,9 @@ private function getLogContent()
298310
{
299311
return file_get_contents($this->logFile);
300312
}
313+
314+
private function getSleepTime()
315+
{
316+
return $this->helper->getPlatform() === Helper::UNIX ? 1 : 2;
317+
}
301318
}

0 commit comments

Comments
 (0)