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 0ecf477
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 2 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,20 @@ jobs:
run: composer install --prefer-dist --no-progress
- name: Run tests
run: php vendor/bin/phpspec run
phpunit:
runs-on: ubuntu-latest
strategy:
matrix:
php: [8.1, 8.2]
steps:
- uses: actions/checkout@v1
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
- name: Validate Composer files
run: composer validate --no-check-all --strict
- name: Install Composer dependencies
run: composer install --prefer-dist --no-progress
- name: Run 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
17 changes: 17 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>

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

<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
<exclude>tests/**/stubs</exclude>
</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;
}
69 changes: 69 additions & 0 deletions tests/Console/CreateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace StudioTests\Console;

use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\visitor\vfsStreamStructureVisitor;

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',
// set up PhpUnit
'yes',
// set up PhpSpec
'yes',
// set up TravisCI
'yes'
]
);

$this->assertEquals(
self::getStructure(),
vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure()
);
}

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

protected static function getStructure(): array
{
return [
'root' => [
'company' => [
'my-package' => [
'spec' => [],
'src' => [
'Example.php' => file_get_contents(__DIR__ . '/stubs/company/my-package/src/Example.php'),
],
'tests' => [
'ExampleTest.php' => file_get_contents(__DIR__ . '/stubs/company/my-package/tests/ExampleTest.php'),
],
'.gitignore' => file_get_contents(__DIR__ . '/stubs/company/my-package/.gitignore'),
'.travis.yml' => file_get_contents(__DIR__ . '/stubs/company/my-package/.travis.yml'),
'composer.json' => file_get_contents(__DIR__ . '/stubs/company/my-package/composer.json'),
'phpspec.yml' => file_get_contents(__DIR__ . '/stubs/company/my-package/phpspec.yml'),
'phpunit.xml' => file_get_contents(__DIR__ . '/stubs/company/my-package/phpunit.xml'),
],
],
],
];
}
}
3 changes: 3 additions & 0 deletions tests/Console/stubs/company/my-package/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.phar
composer.lock
vendor
13 changes: 13 additions & 0 deletions tests/Console/stubs/company/my-package/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: php

php:
- 5.5
- 5.6
- 7.0
- hhvm

before_script:
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction --dev

script: phpunit
17 changes: 17 additions & 0 deletions tests/Console/stubs/company/my-package/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "company/my-package",
"autoload": {
"psr-4": {
"Company\\MyPackage\\": "src/"
}
},
"require-dev": {
"phpunit/phpunit": "^9.6 || ^10",
"phpspec/phpspec": "^7.4"
},
"autoload-dev": {
"psr-4": {
"Company\\MyPackage\\Tests\\": "tests/"
}
}
}
4 changes: 4 additions & 0 deletions tests/Console/stubs/company/my-package/phpspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
suites:
library_suite:
namespace: Company\MyPackage\Tests
psr4_prefix: Company\MyPackage\Tests
19 changes: 19 additions & 0 deletions tests/Console/stubs/company/my-package/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
verbose="true"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
8 changes: 8 additions & 0 deletions tests/Console/stubs/company/my-package/src/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Company\MyPackage;

class Example
{

}
10 changes: 10 additions & 0 deletions tests/Console/stubs/company/my-package/tests/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Company\MyPackage\Tests;

use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{

}

0 comments on commit 0ecf477

Please sign in to comment.