Skip to content

Commit 101e412

Browse files
committed
add console create unit test
1 parent 3fc60dc commit 101e412

File tree

6 files changed

+95
-3
lines changed

6 files changed

+95
-3
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ jobs:
1717
run: composer validate --no-check-all --strict
1818
- name: Install Composer dependencies
1919
run: composer install --prefer-dist --no-progress
20-
- name: Run tests
20+
- name: Run spec tests
2121
run: php vendor/bin/phpspec run
22+
- name: Run unit tests
23+
run: php vendor/bin/phpunit
2224

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
.phpunit.result.cache
12
studio.json
23
composer.phar
34
composer.lock
4-
vendor/
5+
vendor/

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
"Studio\\": "src"
1010
}
1111
},
12+
"autoload-dev": {
13+
"psr-4": {
14+
"StudioTests\\": "tests"
15+
}
16+
},
1217
"require": {
1318
"php": ">=8.1",
1419
"composer-plugin-api": "^2.3",
@@ -18,7 +23,9 @@
1823
},
1924
"require-dev": {
2025
"composer/composer": "^2.5",
21-
"phpspec/phpspec": "^7.4"
26+
"phpspec/phpspec": "^7.4",
27+
"phpunit/phpunit": "^9.6 | ^10",
28+
"mikey179/vfsstream": "^1.6.11"
2229
},
2330
"replace": {
2431
"franzliedke/studio": "self.version"

phpunit.xml.dist

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit bootstrap = "vendor/autoload.php" colors = "true">
4+
5+
<testsuites>
6+
<testsuite name="Project Test Suite">
7+
<directory>tests</directory>
8+
</testsuite>
9+
</testsuites>
10+
11+
<php>
12+
<env name="APP_ENV" value="testing"/>
13+
<env name="APP_VERSION" value="0.15.0"/>
14+
</php>
15+
16+
</phpunit>

tests/Console/AbstractConsoleTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace StudioTests\Console;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Studio\Console\CreateCommand;
7+
use Symfony\Component\Console\Application;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Tester\CommandTester;
10+
11+
abstract class AbstractConsoleTest extends TestCase
12+
{
13+
protected function executeCommand(array $arguments, array $inputs = []): CommandTester
14+
{
15+
$application = new Application('studio', getenv('APP_VERSION'));
16+
$application->add(new CreateCommand);
17+
18+
// this uses a special testing container that allows you to fetch private services
19+
/** @var Command $command */
20+
$command = $application->get($this->getCommandFqcn());
21+
22+
$commandTester = new CommandTester($command);
23+
$commandTester->setInputs($inputs);
24+
$commandTester->execute($arguments);
25+
26+
return $commandTester;
27+
}
28+
29+
abstract protected function getCommandFqcn(): string;
30+
}

tests/Console/CreateTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace StudioTests\Console;
4+
5+
use org\bovigo\vfs\vfsStream;
6+
use Symfony\Component\Console\Tester\CommandTester;
7+
8+
class CreateTest extends AbstractConsoleTest
9+
{
10+
private $root;
11+
12+
public function setUp(): void
13+
{
14+
$this->root = vfsStream::setup();
15+
}
16+
17+
function testExecute(): void
18+
{
19+
$commandTester = $this->executeCommand(
20+
['path' => $this->root->url() . '/company/my-package'],
21+
[
22+
// package name
23+
'company/my-package',
24+
// default namespace (psr-4)
25+
'Company/MyPackage',
26+
]
27+
);
28+
29+
$this->assertTrue($this->root->hasChild('company/my-package'));
30+
}
31+
32+
protected function getCommandFqcn(): string
33+
{
34+
return 'create';
35+
}
36+
}

0 commit comments

Comments
 (0)