Skip to content

Commit 199bf85

Browse files
authored
Make tests less brittle in Windows environments (#32971)
1 parent 73f18a6 commit 199bf85

File tree

6 files changed

+72
-27
lines changed

6 files changed

+72
-27
lines changed

tests/Console/Scheduling/EventTest.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,54 @@ protected function tearDown(): void
1414
m::close();
1515
}
1616

17-
public function testBuildCommand()
17+
public function testBuildCommandUsingUnix()
1818
{
19-
$isWindows = DIRECTORY_SEPARATOR == '\\';
20-
$quote = ($isWindows) ? '"' : "'";
19+
if (windows_os()) {
20+
$this->markTestSkipped('Skipping since operating system is Windows');
21+
}
2122

2223
$event = new Event(m::mock(EventMutex::class), 'php -i');
2324

24-
$defaultOutput = ($isWindows) ? 'NUL' : '/dev/null';
25-
$this->assertSame("php -i > {$quote}{$defaultOutput}{$quote} 2>&1", $event->buildCommand());
25+
$this->assertSame("php -i > '/dev/null' 2>&1", $event->buildCommand());
26+
}
27+
28+
public function testBuildCommandUsingWindows()
29+
{
30+
if (! windows_os()) {
31+
$this->markTestSkipped('Skipping since operating system is not Windows');
32+
}
33+
34+
$event = new Event(m::mock(EventMutex::class), 'php -i');
35+
36+
$this->assertSame('php -i > "NUL" 2>&1', $event->buildCommand());
37+
}
38+
39+
public function testBuildCommandInBackgroundUsingUnix()
40+
{
41+
if (windows_os()) {
42+
$this->markTestSkipped('Skipping since operating system is Windows');
43+
}
2644

2745
$event = new Event(m::mock(EventMutex::class), 'php -i');
2846
$event->runInBackground();
2947

30-
$commandSeparator = ($isWindows ? '&' : ';');
3148
$scheduleId = '"framework'.DIRECTORY_SEPARATOR.'schedule-eeb46c93d45e928d62aaf684d727e213b7094822"';
32-
$this->assertSame("(php -i > {$quote}{$defaultOutput}{$quote} 2>&1 {$commandSeparator} {$quote}".PHP_BINARY."{$quote} artisan schedule:finish {$scheduleId} \"$?\") > {$quote}{$defaultOutput}{$quote} 2>&1 &", $event->buildCommand());
49+
50+
$this->assertSame("(php -i > '/dev/null' 2>&1 ; '".PHP_BINARY."' artisan schedule:finish {$scheduleId} \"$?\") > '/dev/null' 2>&1 &", $event->buildCommand());
51+
}
52+
53+
public function testBuildCommandInBackgroundUsingWindows()
54+
{
55+
if (! windows_os()) {
56+
$this->markTestSkipped('Skipping since operating system is not Windows');
57+
}
58+
59+
$event = new Event(m::mock(EventMutex::class), 'php -i');
60+
$event->runInBackground();
61+
62+
$scheduleId = '"framework'.DIRECTORY_SEPARATOR.'schedule-eeb46c93d45e928d62aaf684d727e213b7094822"';
63+
64+
$this->assertSame('start /b cmd /c "(php -i & "'.PHP_BINARY.'" artisan schedule:finish '.$scheduleId.' "%errorlevel%") > "NUL" 2>&1"', $event->buildCommand());
3365
}
3466

3567
public function testBuildCommandSendOutputTo()

tests/Foundation/Bootstrap/LoadEnvironmentVariablesTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class LoadEnvironmentVariablesTest extends TestCase
1111
{
1212
protected function tearDown(): void
1313
{
14+
unset($_ENV['FOO']);
15+
unset($_SERVER['FOO']);
16+
putenv('FOO');
1417
m::close();
1518
}
1619

tests/Foundation/FoundationApplicationTest.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,12 @@ public function testCachePathsResolveToBootstrapCacheDirectory()
359359
{
360360
$app = new Application('/base/path');
361361

362-
$this->assertSame('/base/path/bootstrap/cache/services.php', $app->getCachedServicesPath());
363-
$this->assertSame('/base/path/bootstrap/cache/packages.php', $app->getCachedPackagesPath());
364-
$this->assertSame('/base/path/bootstrap/cache/config.php', $app->getCachedConfigPath());
365-
$this->assertSame('/base/path/bootstrap/cache/routes.php', $app->getCachedRoutesPath());
366-
$this->assertSame('/base/path/bootstrap/cache/events.php', $app->getCachedEventsPath());
362+
$ds = DIRECTORY_SEPARATOR;
363+
$this->assertSame('/base/path'.$ds.'bootstrap'.$ds.'cache/services.php', $app->getCachedServicesPath());
364+
$this->assertSame('/base/path'.$ds.'bootstrap'.$ds.'cache/packages.php', $app->getCachedPackagesPath());
365+
$this->assertSame('/base/path'.$ds.'bootstrap'.$ds.'cache/config.php', $app->getCachedConfigPath());
366+
$this->assertSame('/base/path'.$ds.'bootstrap'.$ds.'cache/routes.php', $app->getCachedRoutesPath());
367+
$this->assertSame('/base/path'.$ds.'bootstrap'.$ds.'cache/events.php', $app->getCachedEventsPath());
367368
}
368369

369370
public function testEnvPathsAreUsedForCachePathsWhenSpecified()
@@ -375,6 +376,7 @@ public function testEnvPathsAreUsedForCachePathsWhenSpecified()
375376
$_SERVER['APP_ROUTES_CACHE'] = '/absolute/path/routes.php';
376377
$_SERVER['APP_EVENTS_CACHE'] = '/absolute/path/events.php';
377378

379+
$ds = DIRECTORY_SEPARATOR;
378380
$this->assertSame('/absolute/path/services.php', $app->getCachedServicesPath());
379381
$this->assertSame('/absolute/path/packages.php', $app->getCachedPackagesPath());
380382
$this->assertSame('/absolute/path/config.php', $app->getCachedConfigPath());
@@ -399,11 +401,12 @@ public function testEnvPathsAreUsedAndMadeAbsoluteForCachePathsWhenSpecifiedAsRe
399401
$_SERVER['APP_ROUTES_CACHE'] = 'relative/path/routes.php';
400402
$_SERVER['APP_EVENTS_CACHE'] = 'relative/path/events.php';
401403

402-
$this->assertSame('/base/path/relative/path/services.php', $app->getCachedServicesPath());
403-
$this->assertSame('/base/path/relative/path/packages.php', $app->getCachedPackagesPath());
404-
$this->assertSame('/base/path/relative/path/config.php', $app->getCachedConfigPath());
405-
$this->assertSame('/base/path/relative/path/routes.php', $app->getCachedRoutesPath());
406-
$this->assertSame('/base/path/relative/path/events.php', $app->getCachedEventsPath());
404+
$ds = DIRECTORY_SEPARATOR;
405+
$this->assertSame('/base/path'.$ds.'relative/path/services.php', $app->getCachedServicesPath());
406+
$this->assertSame('/base/path'.$ds.'relative/path/packages.php', $app->getCachedPackagesPath());
407+
$this->assertSame('/base/path'.$ds.'relative/path/config.php', $app->getCachedConfigPath());
408+
$this->assertSame('/base/path'.$ds.'relative/path/routes.php', $app->getCachedRoutesPath());
409+
$this->assertSame('/base/path'.$ds.'relative/path/events.php', $app->getCachedEventsPath());
407410

408411
unset(
409412
$_SERVER['APP_SERVICES_CACHE'],
@@ -423,11 +426,12 @@ public function testEnvPathsAreUsedAndMadeAbsoluteForCachePathsWhenSpecifiedAsRe
423426
$_SERVER['APP_ROUTES_CACHE'] = 'relative/path/routes.php';
424427
$_SERVER['APP_EVENTS_CACHE'] = 'relative/path/events.php';
425428

426-
$this->assertSame('/relative/path/services.php', $app->getCachedServicesPath());
427-
$this->assertSame('/relative/path/packages.php', $app->getCachedPackagesPath());
428-
$this->assertSame('/relative/path/config.php', $app->getCachedConfigPath());
429-
$this->assertSame('/relative/path/routes.php', $app->getCachedRoutesPath());
430-
$this->assertSame('/relative/path/events.php', $app->getCachedEventsPath());
429+
$ds = DIRECTORY_SEPARATOR;
430+
$this->assertSame($ds.'relative/path/services.php', $app->getCachedServicesPath());
431+
$this->assertSame($ds.'relative/path/packages.php', $app->getCachedPackagesPath());
432+
$this->assertSame($ds.'relative/path/config.php', $app->getCachedConfigPath());
433+
$this->assertSame($ds.'relative/path/routes.php', $app->getCachedRoutesPath());
434+
$this->assertSame($ds.'relative/path/events.php', $app->getCachedEventsPath());
431435

432436
unset(
433437
$_SERVER['APP_SERVICES_CACHE'],

tests/Integration/Mail/RenderingMailWithLocaleTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ public function testMailableRendersInDefaultLocale()
3131
{
3232
$mail = new RenderedTestMail;
3333

34-
$this->assertStringContainsString('name'.PHP_EOL, $mail->render());
34+
$this->assertStringContainsString('name', $mail->render());
3535
}
3636

3737
public function testMailableRendersInSelectedLocale()
3838
{
3939
$mail = (new RenderedTestMail)->locale('es');
4040

41-
$this->assertStringContainsString('nombre'.PHP_EOL, $mail->render());
41+
$this->assertStringContainsString('nombre', $mail->render());
4242
}
4343

4444
public function testMailableRendersInAppSelectedLocale()
@@ -47,7 +47,7 @@ public function testMailableRendersInAppSelectedLocale()
4747

4848
$mail = new RenderedTestMail;
4949

50-
$this->assertStringContainsString('nombre'.PHP_EOL, $mail->render());
50+
$this->assertStringContainsString('nombre', $mail->render());
5151
}
5252
}
5353

tests/Queue/RedisQueueIntegrationTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,20 @@ public function testExpiredJobsArePopped($driver)
6767

6868
/**
6969
* @dataProvider redisDriverProvider
70+
* @requires extension pcntl
7071
*
7172
* @param mixed $driver
7273
*
7374
* @throws \Exception
7475
*/
7576
public function testBlockingPop($driver)
7677
{
78+
if (! function_exists('pcntl_fork')) {
79+
$this->markTestSkipped('Skipping since the pcntl extension is not available');
80+
}
81+
7782
$this->tearDownRedis();
83+
7884
if ($pid = pcntl_fork() > 0) {
7985
$this->setUpRedis();
8086
$this->setQueue($driver, 'default', null, 60, 10);

tests/Support/SupportHelpersTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ public function testRetry()
492492
$this->assertEquals(2, $attempts);
493493

494494
// Make sure we waited 100ms for the first attempt
495-
$this->assertTrue(microtime(true) - $startTime >= 0.1);
495+
$this->assertEqualsWithDelta(0.1, microtime(true) - $startTime, 0.01);
496496
}
497497

498498
public function testRetryWithPassingWhenCallback()
@@ -513,7 +513,7 @@ public function testRetryWithPassingWhenCallback()
513513
$this->assertEquals(2, $attempts);
514514

515515
// Make sure we waited 100ms for the first attempt
516-
$this->assertTrue(microtime(true) - $startTime >= 0.1);
516+
$this->assertEqualsWithDelta(0.1, microtime(true) - $startTime, 0.01);
517517
}
518518

519519
public function testRetryWithFailingWhenCallback()

0 commit comments

Comments
 (0)