1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-06 00:40:06 +01:00

Test Valet commands (#1256)

* First attempt at testing CLI commands

* Apply fixes from StyleCI

* Protect from running locally

* Fix test

* wip

* wip

* wip

* wip

* wip

* Update app.php

* Create config folder and files for CLI tests

* Apply fixes from StyleCI

* Fix some formatting

* Fix imports

* Update all output() calls to use the writer passed in by the command

Ugly capture of all $outputs from commands, by passing them into `writer()` to be bound into the container, where they can then be pulled out from calls to `output()` and its buddies `info()`, `table()`, and `warning()`.

* Apply fixes from StyleCI

* Flesh out park command test

* Apply fixes from StyleCI

* Drop php 7.0 and 7.1

Co-authored-by: StyleCI Bot <bot@styleci.io>
Co-authored-by: Matt Stauffer <matt@tighten.co>
This commit is contained in:
Dries Vints
2022-12-02 09:32:28 +01:00
committed by GitHub
parent 9216b9a87b
commit a0e1e02a47
18 changed files with 975 additions and 736 deletions

View File

@@ -0,0 +1,35 @@
<?php
use Symfony\Component\Console\Tester\ApplicationTester;
class BaseApplicationTestCase extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
use UsesNullWriter;
public function setUp(): void
{
$this->prepTestConfig();
$this->setNullWriter();
}
public function prepTestConfig()
{
require_once __DIR__.'/../cli/includes/helpers.php';
if (Filesystem::isDir(VALET_HOME_PATH)) {
Filesystem::rmDirAndContents(VALET_HOME_PATH);
}
Configuration::createConfigurationDirectory();
Configuration::writeBaseConfiguration();
}
public function appAndTester()
{
$app = require __DIR__.'/../cli/app.php';
$app->setAutoExit(false);
$tester = new ApplicationTester($app);
return [$app, $tester];
}
}

View File

@@ -11,11 +11,14 @@
class BrewTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
use UsesNullWriter;
public function set_up()
{
$_SERVER['SUDO_USER'] = user();
Container::setInstance(new Container);
$this->setNullWriter();
}
public function tear_down()

26
tests/CliTest.php Normal file
View File

@@ -0,0 +1,26 @@
<?php
/**
* @requires PHP >= 8.0
*/
class CliTest extends BaseApplicationTestCase
{
public function test_park_command()
{
[$app, $tester] = $this->appAndTester();
$tester->run(['command' => 'park', 'path' => './tests/output']);
$tester->assertCommandIsSuccessful();
$this->assertStringContainsString(
"The [./tests/output] directory has been added to Valet's paths.",
$tester->getDisplay()
);
$paths = data_get(Configuration::read(), 'paths');
$this->assertEquals(1, count($paths));
$this->assertEquals('./tests/output', reset($paths));
}
}

View File

@@ -11,11 +11,14 @@
class ConfigurationTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
use UsesNullWriter;
public function set_up()
{
$_SERVER['SUDO_USER'] = user();
Container::setInstance(new Container);
$this->setNullWriter();
}
public function tear_down()

View File

@@ -12,11 +12,14 @@
class DnsMasqTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
use UsesNullWriter;
public function set_up()
{
$_SERVER['SUDO_USER'] = user();
Container::setInstance(new Container);
$this->setNullWriter();
}
public function tear_down()

View File

@@ -4,6 +4,13 @@
class FilesystemTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
use UsesNullWriter;
public function set_up()
{
$this->setNullWriter();
}
public function tear_down()
{
exec('rm -rf '.__DIR__.'/output');

View File

@@ -11,11 +11,14 @@
class NginxTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
use UsesNullWriter;
public function set_up()
{
$_SERVER['SUDO_USER'] = user();
Container::setInstance(new Container);
$this->setNullWriter();
}
public function tear_down()

View File

@@ -6,11 +6,14 @@
class NgrokTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
use UsesNullWriter;
public function set_up()
{
$_SERVER['SUDO_USER'] = user();
Container::setInstance(new Container);
$this->setNullWriter();
}
public function tear_down()

View File

@@ -14,11 +14,14 @@
class PhpFpmTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
use UsesNullWriter;
public function set_up()
{
$_SERVER['SUDO_USER'] = user();
Container::setInstance(new Container);
$this->setNullWriter();
}
public function tear_down()

View File

@@ -12,11 +12,14 @@
class SiteTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
use UsesNullWriter;
public function set_up()
{
$_SERVER['SUDO_USER'] = user();
Container::setInstance(new Container);
$this->setNullWriter();
}
public function tear_down()

17
tests/UsesNullWriter.php Normal file
View File

@@ -0,0 +1,17 @@
<?php
use Illuminate\Container\Container;
trait UsesNullWriter
{
public function setNullWriter()
{
Container::getInstance()->instance('writer', new class
{
public function writeLn($msg)
{
// do nothing
}
});
}
}