Skip to content

Commit

Permalink
add console create unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
head1328 committed Apr 27, 2023
1 parent 3fc60dc commit 101e412
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:
run: composer validate --no-check-all --strict
- name: Install Composer dependencies
run: composer install --prefer-dist --no-progress
- name: Run tests
- name: Run spec tests
run: php vendor/bin/phpspec run
- name: Run unit tests
run: php vendor/bin/phpunit

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.phpunit.result.cache
studio.json
composer.phar
composer.lock
vendor/
vendor/
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"Studio\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"StudioTests\\": "tests"
}
},
"require": {
"php": ">=8.1",
"composer-plugin-api": "^2.3",
Expand All @@ -18,7 +23,9 @@
},
"require-dev": {
"composer/composer": "^2.5",
"phpspec/phpspec": "^7.4"
"phpspec/phpspec": "^7.4",
"phpunit/phpunit": "^9.6 | ^10",
"mikey179/vfsstream": "^1.6.11"
},
"replace": {
"franzliedke/studio": "self.version"
Expand Down
16 changes: 16 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap = "vendor/autoload.php" colors = "true">

<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_VERSION" value="0.15.0"/>
</php>

</phpunit>
30 changes: 30 additions & 0 deletions tests/Console/AbstractConsoleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace StudioTests\Console;

use PHPUnit\Framework\TestCase;
use Studio\Console\CreateCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;

abstract class AbstractConsoleTest extends TestCase
{
protected function executeCommand(array $arguments, array $inputs = []): CommandTester
{
$application = new Application('studio', getenv('APP_VERSION'));
$application->add(new CreateCommand);

// this uses a special testing container that allows you to fetch private services
/** @var Command $command */
$command = $application->get($this->getCommandFqcn());

$commandTester = new CommandTester($command);
$commandTester->setInputs($inputs);
$commandTester->execute($arguments);

return $commandTester;
}

abstract protected function getCommandFqcn(): string;
}
36 changes: 36 additions & 0 deletions tests/Console/CreateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace StudioTests\Console;

use org\bovigo\vfs\vfsStream;
use Symfony\Component\Console\Tester\CommandTester;

class CreateTest extends AbstractConsoleTest
{
private $root;

public function setUp(): void
{
$this->root = vfsStream::setup();
}

function testExecute(): void
{
$commandTester = $this->executeCommand(
['path' => $this->root->url() . '/company/my-package'],
[
// package name
'company/my-package',
// default namespace (psr-4)
'Company/MyPackage',
]
);

$this->assertTrue($this->root->hasChild('company/my-package'));
}

protected function getCommandFqcn(): string
{
return 'create';
}
}

0 comments on commit 101e412

Please sign in to comment.