Skip to content

Commit 7a354e7

Browse files
Test Improvements (#371)
* Test Improvements Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI --------- Signed-off-by: Mior Muhammad Zaki <[email protected]> Co-authored-by: StyleCI Bot <[email protected]>
1 parent 38c400b commit 7a354e7

File tree

3 files changed

+62
-40
lines changed

3 files changed

+62
-40
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Laravel\Installer\Console\Concerns;
4+
5+
use Symfony\Component\Process\Exception\ProcessStartFailedException;
6+
use Symfony\Component\Process\Process;
7+
8+
trait InteractsWithHerdOrValet
9+
{
10+
/**
11+
* Determine if the given directory is parked using Herd or Valet.
12+
*
13+
* @param string $directory
14+
* @return bool
15+
*/
16+
public function isParkedOnHerdOrValet(string $directory)
17+
{
18+
$output = $this->runOnValetOrHerd('paths');
19+
20+
return $output !== false ? in_array(dirname($directory), json_decode($output)) : false;
21+
}
22+
23+
/**
24+
* Runs the given command on the "herd" or "valet" CLI.
25+
*
26+
* @param string $command
27+
* @return string|false
28+
*/
29+
protected function runOnValetOrHerd(string $command)
30+
{
31+
foreach (['herd', 'valet'] as $tool) {
32+
$process = new Process([$tool, $command, '-v']);
33+
34+
try {
35+
$process->run();
36+
37+
if ($process->isSuccessful()) {
38+
return trim($process->getOutput());
39+
}
40+
} catch (ProcessStartFailedException) {
41+
}
42+
}
43+
44+
return false;
45+
}
46+
}

src/NewCommand.php

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Symfony\Component\Console\Input\InputInterface;
1212
use Symfony\Component\Console\Input\InputOption;
1313
use Symfony\Component\Console\Output\OutputInterface;
14-
use Symfony\Component\Process\Exception\ProcessStartFailedException;
1514
use Symfony\Component\Process\PhpExecutableFinder;
1615
use Symfony\Component\Process\Process;
1716

@@ -23,6 +22,7 @@
2322
class NewCommand extends Command
2423
{
2524
use Concerns\ConfiguresPrompts;
25+
use Concerns\InteractsWithHerdOrValet;
2626

2727
/**
2828
* The Composer instance.
@@ -247,7 +247,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
247247
$output->writeln('<fg=gray>➜</> <options=bold>cd '.$name.'</>');
248248
$output->writeln('<fg=gray>➜</> <options=bold>npm install && npm run build</>');
249249

250-
if ($this->isParked($directory)) {
250+
if ($this->isParkedOnHerdOrValet($directory)) {
251251
$url = $this->generateAppUrl($name);
252252
$output->writeln('<fg=gray>➜</> Open: <options=bold;href='.$url.'>'.$url.'</>');
253253
} else {
@@ -827,19 +827,6 @@ protected function canResolveHostname($hostname)
827827
return gethostbyname($hostname.'.') !== $hostname.'.';
828828
}
829829

830-
/**
831-
* Determine if the given directory is parked using Herd or Valet.
832-
*
833-
* @param string $directory
834-
* @return bool
835-
*/
836-
protected function isParked(string $directory)
837-
{
838-
$output = $this->runOnValetOrHerd('paths');
839-
840-
return $output !== false ? in_array(dirname($directory), json_decode($output)) : false;
841-
}
842-
843830
/**
844831
* Get the installation directory.
845832
*
@@ -892,30 +879,6 @@ protected function phpBinary()
892879
: 'php';
893880
}
894881

895-
/**
896-
* Runs the given command on the "herd" or "valet" CLI.
897-
*
898-
* @param string $command
899-
* @return string|bool
900-
*/
901-
protected function runOnValetOrHerd(string $command)
902-
{
903-
foreach (['herd', 'valet'] as $tool) {
904-
$process = new Process([$tool, $command, '-v']);
905-
906-
try {
907-
$process->run();
908-
909-
if ($process->isSuccessful()) {
910-
return trim($process->getOutput());
911-
}
912-
} catch (ProcessStartFailedException) {
913-
}
914-
}
915-
916-
return false;
917-
}
918-
919882
/**
920883
* Run the given commands.
921884
*

tests/NewCommandTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace Laravel\Installer\Console\Tests;
44

5+
use Laravel\Installer\Console\Concerns\InteractsWithHerdOrValet;
56
use Laravel\Installer\Console\NewCommand;
67
use PHPUnit\Framework\TestCase;
78
use Symfony\Component\Console\Application;
89
use Symfony\Component\Console\Tester\CommandTester;
910

1011
class NewCommandTest extends TestCase
1112
{
13+
use InteractsWithHerdOrValet;
14+
1215
public function test_it_can_scaffold_a_new_laravel_app()
1316
{
1417
$scaffoldDirectoryName = 'tests-output/my-app';
@@ -36,6 +39,10 @@ public function test_it_can_scaffold_a_new_laravel_app()
3639

3740
public function test_it_can_chops_trailing_slash_from_name()
3841
{
42+
if ($this->runOnValetOrHerd('paths') === false) {
43+
$this->markTestSkipped('Require `herd` or `valet` to resolve `APP_URL` using hostname instead of "localhost".');
44+
}
45+
3946
$scaffoldDirectoryName = 'tests-output/trailing/';
4047
$scaffoldDirectory = __DIR__.'/../'.$scaffoldDirectoryName;
4148

@@ -57,7 +64,13 @@ public function test_it_can_chops_trailing_slash_from_name()
5764
$this->assertSame(0, $statusCode);
5865
$this->assertDirectoryExists($scaffoldDirectory.'/vendor');
5966
$this->assertFileExists($scaffoldDirectory.'/.env');
60-
$this->assertStringContainsStringIgnoringLineEndings('APP_URL=http://tests-output/trailing.test', file_get_contents($scaffoldDirectory.'/.env'));
67+
68+
if ($this->isParkedOnHerdOrValet($scaffoldDirectory)) {
69+
$this->assertStringContainsStringIgnoringLineEndings(
70+
'APP_URL=http://tests-output/trailing.test',
71+
file_get_contents($scaffoldDirectory.'/.env')
72+
);
73+
}
6174
}
6275

6376
public function test_on_at_least_laravel_11()

0 commit comments

Comments
 (0)