Skip to content

Commit fbafe9d

Browse files
committed
test: clish command
1 parent 0aa18f1 commit fbafe9d

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

tests/Console/ClishCommandTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the CLI-SYNTAX package.
5+
*
6+
* (c) Jitendra Adhikari <jiten.adhikary@gmail.com>
7+
* <https://github.com/adhocore>
8+
*
9+
* Licensed under MIT license.
10+
*/
11+
12+
namespace Ahc\CliSyntax\Test\Console;
13+
14+
use Ahc\Cli\Application;
15+
use Ahc\Cli\IO\Interactor;
16+
use Ahc\Cli\Test\CliTestCase;
17+
use Ahc\CliSyntax\Console\ClishCommand;
18+
use PHPUnit\Framework\TestCase;
19+
20+
require_once __DIR__ . '/../../vendor/adhocore/cli/tests/CliTestCase.php';
21+
22+
class ClishCommandTest extends CliTestCase
23+
{
24+
protected $app;
25+
26+
protected static $in = __DIR__ . '/stdin';
27+
protected static $ou = __DIR__ . '/stdout';
28+
protected static $ex = __DIR__ . '/output/export.png';
29+
30+
public function setUp()
31+
{
32+
parent::setUp();
33+
\touch(static::$in);
34+
35+
$this->app = (new Application('clish', 'test', function () {}))->add(new ClishCommand, 'c', true);
36+
$this->app->io(new Interactor(static::$in, static::$ou));
37+
}
38+
39+
public static function tearDownAfterClass()
40+
{
41+
parent::tearDownAfterClass();
42+
43+
if (\is_file(static::$ex)) {
44+
\unlink(static::$ex);
45+
}
46+
if (\is_file(static::$in)) {
47+
\unlink(static::$in);
48+
}
49+
50+
if (\is_dir(\dirname(static::$ex))) {
51+
\rmdir(\dirname(static::$ex));
52+
}
53+
}
54+
55+
public function testExecute()
56+
{
57+
$this->app->handle(['clish', 'c', '--file=' . __DIR__ . '/../../example.php']);
58+
59+
$this->assertBufferContains("<?php echo 'Hello world!'; ?>");
60+
$this->assertBufferContains('This file is part of the CLI-SYNTAX package');
61+
}
62+
63+
public function testExecuteExport()
64+
{
65+
$this->app->handle(['clish', 'c', '--file=' . __DIR__ . '/../../example.php', '--output=' . static::$ex]);
66+
67+
$this->assertFileExists(static::$ex, 'Should export to a file');
68+
$this->assertEmpty($this->buffer(), 'Shouldnt write to stdout when --output is given');
69+
}
70+
71+
public function testInteractEmptyInput()
72+
{
73+
\file_put_contents(static::$in, '');
74+
75+
$this->app->handle(['clish', 'c']);
76+
77+
$this->assertBufferContains('Type in or paste PHP Code below, Press Ctrl+D when done');
78+
}
79+
80+
public function testInteractNonEmpty()
81+
{
82+
\file_put_contents(static::$in, 'echo 1234;');
83+
84+
$this->app->handle(['clish', 'c']);
85+
86+
$this->assertBufferContains('<?php');
87+
$this->assertBufferContains('echo');
88+
$this->assertBufferContains('1234');
89+
}
90+
}

0 commit comments

Comments
 (0)