Skip to content

Commit c9726c1

Browse files
committed
Added unit tests
1 parent 5967538 commit c9726c1

File tree

7 files changed

+208
-2
lines changed

7 files changed

+208
-2
lines changed

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: php
2+
3+
php:
4+
- 7.1
5+
- 7.2
6+
- 7.3
7+
8+
notifications:
9+
email:
10+
11+
12+
before_script: composer install
13+
14+
script:
15+
- vendor/bin/phpunit --configuration phpunit.xml

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"psr-4": {"Utopia\\CLI\\": "src/CLI"}
1515
},
1616
"require": {
17-
"php": ">=7.1"
17+
"php": ">=7.1",
18+
"utopia-php/framework": "master"
1819
},
1920
"require-dev": {
2021
"phpunit/phpunit": "^7.0"

phpunit.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<phpunit backupGlobals="false"
2+
backupStaticAttributes="false"
3+
bootstrap="vendor/autoload.php"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
processIsolation="false"
9+
stopOnFailure="false"
10+
>
11+
<testsuites>
12+
<testsuite name="Application Test Suite">
13+
<directory>./tests/</directory>
14+
</testsuite>
15+
</testsuites>
16+
</phpunit>

src/CLI/CLI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function __construct(array $args = [])
7373
throw new Exception('CLI tasks can only work from the command line');
7474
}
7575

76-
$this->args = $this->parse((isset($_SERVER['argv'])) ? $_SERVER['argv'] : $args);
76+
$this->args = $this->parse((!empty($args) || !isset($_SERVER['argv'])) ? $args: $_SERVER['argv']);
7777

7878
$this->error = function (Exception $error) {
7979
Console::error($error->getMessage());

tests/CLI/CLITest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Utopia PHP Framework
4+
*
5+
* @package Abuse
6+
* @subpackage Tests
7+
*
8+
* @link https://github.com/utopia-php/framework
9+
* @author Eldad Fux <[email protected]>
10+
* @version 1.0 RC4
11+
* @license The MIT License (MIT) <http://www.opensource.org/licenses/mit-license.php>
12+
*/
13+
14+
namespace Utopia\Tests;
15+
16+
use Utopia\CLI\CLI;
17+
use Utopia\Validator\Email;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class CLITest extends TestCase
21+
{
22+
public function setUp()
23+
{
24+
}
25+
26+
public function tearDown()
27+
{
28+
}
29+
30+
public function testAppSuccess()
31+
{
32+
ob_start();
33+
34+
$cli = new CLI(['test.php', 'build', '[email protected]']); // Mock command request
35+
36+
$cli
37+
->task('build')
38+
->param('email', null, new Email(), 'Valid email address')
39+
->action(function ($email) {
40+
echo $email;
41+
});
42+
43+
$cli->run();
44+
45+
$result = ob_get_clean();
46+
47+
$this->assertEquals('[email protected]', $result);
48+
}
49+
50+
public function testAppFailure()
51+
{
52+
ob_start();
53+
54+
$cli = new CLI(['test.php', 'build', '--email=me.example.com']); // Mock command request
55+
56+
$cli
57+
->task('build')
58+
->param('email', null, new Email(), 'Valid email address')
59+
->action(function ($email) {
60+
echo $email;
61+
});
62+
63+
$cli->run();
64+
65+
$result = ob_get_clean();
66+
67+
$this->assertEquals('', $result);
68+
}
69+
}

tests/CLI/ConsoleTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Utopia PHP Framework
4+
*
5+
* @package Abuse
6+
* @subpackage Tests
7+
*
8+
* @link https://github.com/utopia-php/framework
9+
* @author Eldad Fux <[email protected]>
10+
* @version 1.0 RC4
11+
* @license The MIT License (MIT) <http://www.opensource.org/licenses/mit-license.php>
12+
*/
13+
14+
namespace Utopia\Tests;
15+
16+
use Utopia\CLI\Console;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class ConsoleTest extends TestCase
20+
{
21+
public function setUp()
22+
{
23+
}
24+
25+
public function tearDown()
26+
{
27+
}
28+
29+
public function testLogs()
30+
{
31+
// Use vars to resolve adapter key
32+
$this->assertEquals(4, Console::log('log'));
33+
$this->assertEquals(17, Console::success('success'));
34+
$this->assertEquals(15, Console::error('error'));
35+
$this->assertEquals(14, Console::info('info'));
36+
$this->assertEquals(19, Console::warning('warning'));
37+
}
38+
}

tests/CLI/TaskTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Utopia PHP Framework
4+
*
5+
* @package Abuse
6+
* @subpackage Tests
7+
*
8+
* @link https://github.com/utopia-php/framework
9+
* @author Eldad Fux <[email protected]>
10+
* @version 1.0 RC4
11+
* @license The MIT License (MIT) <http://www.opensource.org/licenses/mit-license.php>
12+
*/
13+
14+
namespace Utopia\Tests;
15+
16+
use Utopia\CLI\Task;
17+
use PHPUnit\Framework\TestCase;
18+
use Utopia\Validator\Email;
19+
20+
class TaskTest extends TestCase
21+
{
22+
/**
23+
* @var Task
24+
*/
25+
protected $task;
26+
27+
public function setUp()
28+
{
29+
$this->task = new Task('test');
30+
}
31+
32+
public function tearDown()
33+
{
34+
$this->task = null;
35+
}
36+
37+
public function testDescription()
38+
{
39+
$this->task->desc('test task');
40+
41+
$this->assertEquals('test task', $this->task->getDesc());
42+
}
43+
44+
public function testAction()
45+
{
46+
$this->task->action(function () {
47+
return 'result';
48+
});
49+
50+
$this->assertEquals('result', $this->task->getAction()());
51+
}
52+
53+
public function testLabel()
54+
{
55+
$this->task->label('key', 'value');
56+
57+
$this->assertEquals('value', $this->task->getLabel('key', 'default'));
58+
$this->assertEquals('default', $this->task->getLabel('unknown', 'default'));
59+
}
60+
61+
public function testParam()
62+
{
63+
$this->task->param('email', '[email protected]', new Email(), 'Param with valid email address', false);
64+
65+
$this->assertCount(1, $this->task->getParams());
66+
}
67+
}

0 commit comments

Comments
 (0)