-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow running unit tests for legacy script commands
- Loading branch information
Showing
5 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
instances/example/Console/TestConsoleCommandController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |