Skip to content

Commit

Permalink
Allow running unit tests for legacy script commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kpicaza committed May 26, 2022
1 parent a6985f3 commit f8fa1cb
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
},
"autoload-dev": {
"psr-4": {
"Sifo\\Test\\": "test/"
"Sifo\\Test\\": "test/",
"Sifo\\Example\\": "instances/example/"
}
}
}
17 changes: 17 additions & 0 deletions instances/example/Console/TestConsoleCommandController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Sifo\Example\Console;

use Common\SharedCommandLineController;

class TestConsoleCommandController extends SharedCommandLineController
{
function init()
{
}

function exec()
{
echo 'Hello World!';
}
}
2 changes: 0 additions & 2 deletions src/Sifo/CLBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*/
namespace Sifo;

require_once ROOT_PATH . '/vendor/sifophp/sifo/src/Sifo/Bootstrap.php';

class CLBootstrap extends Bootstrap
{
static $script_controller;
Expand Down
50 changes: 50 additions & 0 deletions test/Common/TestConsoleScriptRunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Sifo\Test\Common;

use Psr\Container\ContainerInterface;
use Sifo\CLBootstrap;
use Sifo\Domains;
use Sifo\FilterServer;

class TestConsoleScriptRunner
{
/** @var string */
private $domainName;
/** @var string */
private $scriptFQCN;
/** @var string */
private $scriptLocation;
/** @var array */
private $argv;

public function __construct(string $domainName, string $scriptFQCN, string $scriptLocation, array $argv)
{
$this->domainName = $domainName;
$this->scriptFQCN = $scriptFQCN;
$this->scriptLocation = $scriptLocation;
$this->argv = $argv;
}

public function run(?ContainerInterface $container = null): string
{
Domains::getInstanceFromDomainName($this->domainName)->setDebugMode(false);
$originalServer = $_SERVER;
$_SERVER['PHP_SELF'] = $this->scriptLocation;
$_SERVER['argv'] = $this->argv;
$filter_server = FilterServer::getInstance();
$filter_server->setVar('argv', $this->argv);

ob_start();
ob_start();
CLBootstrap::$script_controller = $this->scriptFQCN;
CLBootstrap::execute(null, null, $container);

$output = ob_get_clean();

$_SERVER = $originalServer;
$filter_server->setVar('argv', $_SERVER['argv']);

return $output;
}
}
32 changes: 32 additions & 0 deletions test/Sifo/CLBootstrapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Sifo\Test\Test;

use Psr\Container\ContainerInterface;
use PHPUnit\Framework\TestCase;
use Sifo\Test\Common\TestConsoleScriptRunner;

/**
* @runTestsInSeparateProcesses
*/
class CLBootstrapTest extends TestCase
{
public function testRunFakeCommand(): void
{
$testRunner = new TestConsoleScriptRunner(
'sifo.local',
'\Sifo\Example\Console\TestConsoleCommand',
ROOT_PATH . '/example/Console/TestConsoleCommandController.php',
[
ROOT_PATH . '/example/Console/TestConsoleCommandController.php',
'sifo.local',
]
);

$output = $testRunner->run($this->createMock(ContainerInterface::class));

self::assertContains('Hello World!', $output);
}
}

0 comments on commit f8fa1cb

Please sign in to comment.