Ensure that the directory doesn't already exist

This commit is contained in:
Oliver Davies 2020-02-09 12:11:20 +00:00
parent d17c1defcf
commit 0ece90c6cf
7 changed files with 217 additions and 4 deletions

14
app.php Normal file
View file

@ -0,0 +1,14 @@
<?php
use Opdavies\DrupalModuleGenerator\Command\GenerateDrupal7Command;
use Symfony\Component\Console\Application;
require_once __DIR__.'/vendor/autoload.php';
$app = new Application();
$app->addCommands([
new GenerateDrupal7Command(),
]);
$app->run();

View file

@ -7,7 +7,9 @@
"symfony/dependency-injection": "^5.0" "symfony/dependency-injection": "^5.0"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^9.0" "php": "5.6 || ^7.0",
"phpunit/phpunit": "^9.0",
"symfony/var-dumper": "^5.0"
}, },
"license": "MIT", "license": "MIT",
"authors": [ "authors": [
@ -15,5 +17,18 @@
"name": "Oliver Davies", "name": "Oliver Davies",
"email": "oliver@oliverdavies.uk" "email": "oliver@oliverdavies.uk"
} }
] ],
"autoload": {
"psr-4": {
"Opdavies\\DrupalModuleGenerator\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Opdavies\\Tests\\DrupalModuleGenerator\\": "tests/"
}
},
"config": {
"sort-packages": true
}
} }

81
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "c857126b9f1440b76b7f240733f85adb", "content-hash": "21bd996a5a5331085c6854ddc3f384f2",
"packages": [ "packages": [
{ {
"name": "psr/container", "name": "psr/container",
@ -1875,6 +1875,81 @@
], ],
"time": "2019-11-27T13:56:44+00:00" "time": "2019-11-27T13:56:44+00:00"
}, },
{
"name": "symfony/var-dumper",
"version": "v5.0.4",
"source": {
"type": "git",
"url": "https://github.com/symfony/var-dumper.git",
"reference": "923591cfb78a935f0c98968fedfad05bfda9d01f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/var-dumper/zipball/923591cfb78a935f0c98968fedfad05bfda9d01f",
"reference": "923591cfb78a935f0c98968fedfad05bfda9d01f",
"shasum": ""
},
"require": {
"php": "^7.2.5",
"symfony/polyfill-mbstring": "~1.0"
},
"conflict": {
"phpunit/phpunit": "<5.4.3",
"symfony/console": "<4.4"
},
"require-dev": {
"ext-iconv": "*",
"symfony/console": "^4.4|^5.0",
"symfony/process": "^4.4|^5.0",
"twig/twig": "^2.4|^3.0"
},
"suggest": {
"ext-iconv": "To convert non-UTF-8 strings to UTF-8 (or symfony/polyfill-iconv in case ext-iconv cannot be used).",
"ext-intl": "To show region name in time zone dump",
"symfony/console": "To use the ServerDumpCommand and/or the bin/var-dump-server script"
},
"bin": [
"Resources/bin/var-dump-server"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "5.0-dev"
}
},
"autoload": {
"files": [
"Resources/functions/dump.php"
],
"psr-4": {
"Symfony\\Component\\VarDumper\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony mechanism for exploring and dumping PHP variables",
"homepage": "https://symfony.com",
"keywords": [
"debug",
"dump"
],
"time": "2020-01-25T15:56:29+00:00"
},
{ {
"name": "theseer/tokenizer", "name": "theseer/tokenizer",
"version": "1.1.3", "version": "1.1.3",
@ -1970,5 +2045,7 @@
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": [], "platform": [],
"platform-dev": [] "platform-dev": {
"php": "5.6 || ^7.0"
}
} }

18
phpunit.xml Normal file
View file

@ -0,0 +1,18 @@
<?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>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>

View file

@ -0,0 +1,51 @@
<?php
namespace Opdavies\DrupalModuleGenerator\Command;
use Opdavies\DrupalModuleGenerator\Exception\CannotCreateModuleException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class GenerateDrupal7Command extends Command
{
private $moduleName;
/**
* {@inheritdoc}
*/
protected static $defaultName = 'generate-drupal-7-module';
/**
* {@inheritDoc}
*/
protected function configure()
{
$this
->setDescription('Generate a new Drupal 7 module.')
->addArgument('module-name', InputArgument::REQUIRED, 'The name of the module to create');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->moduleName = $input->getArgument('module-name');
$this->ensureDirectoryDoesNotExist();
return 0;
}
/**
* Ensure that the directory name for the module doesn't already exist.
*/
private function ensureDirectoryDoesNotExist()
{
if (is_dir($this->moduleName)) {
throw CannotCreateModuleException::directoryAlreadyExists();
}
}
}

View file

@ -0,0 +1,11 @@
<?php
namespace Opdavies\DrupalModuleGenerator\Exception;
class CannotCreateModuleException extends \RuntimeException
{
public static function directoryAlreadyExists()
{
return new static('The given directory name for the module already exists.');
}
}

View file

@ -0,0 +1,27 @@
<?php
namespace Opdavies\Tests\DrupalModuleGenerator\Command;
use Opdavies\DrupalModuleGenerator\Command\GenerateDrupal7Command;
use Opdavies\DrupalModuleGenerator\Exception\CannotCreateModuleException;
use Symfony\Component\Console\Tester\CommandTester;
use PHPUnit\Framework\TestCase;
class GenerateDrupal7ModuleCommandTest extends TestCase
{
/** @test */
public function it_throws_an_exception_if_the_directory_already_exists() {
mkdir('my-existing-drupal-module');
try {
$commandTester = new CommandTester(new GenerateDrupal7Command());
$commandTester->execute([
'module-name' => 'my-existing-drupal-module',
]);
} catch (CannotCreateModuleException $e) {
$this->addToAssertionCount(1);
}
rmdir('my-existing-drupal-module');
}
}