Skip to content

Commit

Permalink
Merge pull request #1 from zoilomora/master
Browse files Browse the repository at this point in the history
Integration with docker, update to PHP 7.4, coding standard.
  • Loading branch information
AaronBernabeu authored Feb 14, 2020
2 parents 4ab4f86 + 3cf2008 commit b53b2fd
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 89 deletions.
19 changes: 18 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# phpstorm project files
.idea

# netbeans project files
nbproject/*

# zend studio for eclipse project files
.buildpath
.project
.settings

# windows thumbnail cache
Thumbs.db

# Mac DS_Store Files
.DS_Store

/vendor/
composer.lock
/.idea
.phpunit.result.cache
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM php:7.4-cli-alpine3.11

RUN apk update && \
apk add --no-cache \
libzip-dev \
openssl-dev && \
docker-php-ext-install -j$(nproc) \
zip

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer

ENV PATH /var/app/bin:/var/app/vendor/bin:$PATH

WORKDIR /var/app
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
UID=$(shell id -u)
GID=$(shell id -g)
DOCKER_PHP_SERVICE=php

start: erase cache-folders build composer-install bash

erase:
docker-compose down -v

build:
docker-compose build && \
docker-compose pull

cache-folders:
mkdir -p ~/.composer && chown ${UID}:${GID} ~/.composer

composer-install:
docker-compose run --rm -u ${UID}:${GID} ${DOCKER_PHP_SERVICE} composer install

bash:
docker-compose run --rm -u ${UID}:${GID} ${DOCKER_PHP_SERVICE} sh

logs:
docker-compose logs -f ${DOCKER_PHP_SERVICE}
13 changes: 6 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@
"type": "library",
"autoload": {
"psr-4": {
"Pccomponentes\\Migration\\": "src/"
"PcComponentes\\Migration\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Pccomponentes\\Migration\\Tests\\": "tests/"
"PcComponentes\\Migration\\Tests\\": "tests/"
}
},
"require": {
"php": "^7.1",
"symfony/console": "^4.1"
"php": "^7.4",
"symfony/console": "^4.4"
},
"require-dev": {
"pccomponentes/ganchudo": "^1.0",
"phpunit/phpunit": "^7.3",
"squizlabs/php_codesniffer": "^3.3"
"phpunit/phpunit": "^8.0",
"pccomponentes/coding-standard": "^1.0"
}
}
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3.7'
services:
php:
build: .
volumes:
- .:/var/app
12 changes: 4 additions & 8 deletions migration/MigrationTested.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
<?php
/**
* This disaster was designed by
* @author Juan G. Rodríguez Carrión <[email protected]>
*/
declare(strict_types=1);

use Pccomponentes\Migration\Migration;
use PcComponentes\Migration\Migration;

class MigrationTested implements Migration
{
private $constructorArgs;
private $upOperationCalled;
private $downOperationCalled;
private array $constructorArgs;
private bool $upOperationCalled;
private bool $downOperationCalled;

public function __construct()
{
Expand Down
15 changes: 3 additions & 12 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?xml version="1.0"?>
<?xml version="1.0" ?>
<ruleset name="Project rules">
<description>Project rules.</description>
<file>src</file>
<file>tests</file>
<rule ref="PSR2"/>
<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="Generic.Files.LineLength.TooLong">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<arg name="colors" />
</ruleset>
<rule ref="vendor/pccomponentes/coding-standard/PccomponentesCodingStandard/ruleset.xml" />
</ruleset>
7 changes: 2 additions & 5 deletions src/Migration.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<?php
/**
* This disaster was designed by
* @author Juan G. Rodríguez Carrión <[email protected]>
*/
declare(strict_types=1);
namespace Pccomponentes\Migration;

namespace PcComponentes\Migration;

interface Migration
{
Expand Down
20 changes: 9 additions & 11 deletions src/MigrationCommand.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<?php
/**
* This disaster was designed by
* @author Juan G. Rodríguez Carrión <[email protected]>
*/
declare(strict_types=1);
namespace Pccomponentes\Migration;

namespace PcComponentes\Migration;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -14,14 +11,15 @@

final class MigrationCommand extends Command
{
private $loader;
private MigrationLoader $loader;

private const OPERATION_UP = 'up';
private const OPERTATION_DOWN = 'down';
private const OPERATION_DOWN = 'down';

public function __construct(string $commandName, string $migrationDir, array $migrationArgs)
{
parent::__construct("migration:{$commandName}");

$this->loader = new MigrationLoader($migrationDir, $migrationArgs);
}

Expand All @@ -33,7 +31,7 @@ protected function configure(): void
'operation',
'op',
InputOption::VALUE_REQUIRED,
sprintf('Available operations: %s, %s', self::OPERATION_UP, self::OPERTATION_DOWN)
sprintf('Available operations: %s, %s', self::OPERATION_UP, self::OPERATION_DOWN)
)
->addArgument(
'migrations',
Expand All @@ -53,16 +51,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
case self::OPERATION_UP:
$executor->upOperation();
return 0;
case self::OPERTATION_DOWN:
case self::OPERATION_DOWN:
$executor->downOperation();
return 0;
default:
$output->writeln(sprintf('<error>Invalid operation %s</error>', $operation));
$output->write('<comment>Available operations: </comment>');
$output->writeln(sprintf('<options=bold>%s, %s</>', self::OPERATION_UP, self::OPERTATION_DOWN));
$output->writeln(sprintf('<options=bold>%s, %s</>', self::OPERATION_UP, self::OPERATION_DOWN));
return 1;
}
} catch (\Exception $e) {
} catch (\Throwable $e) {
$output->writeln(sprintf('<error>Exception: %s</error>', $e->getMessage()));
return 1;
}
Expand Down
13 changes: 5 additions & 8 deletions src/MigrationExecutor.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<?php
/**
* This disaster was designed by
* @author Juan G. Rodríguez Carrión <[email protected]>
*/
declare(strict_types=1);
namespace Pccomponentes\Migration;

namespace PcComponentes\Migration;

final class MigrationExecutor
{
private $migrations;
private array $migrations;

public function __construct(array $migrations)
{
Expand All @@ -24,7 +21,7 @@ public function upOperation(): void
{
\array_walk(
$this->migrations,
function (Migration $migration) {
static function (Migration $migration) {
$migration->upOperation();
}
);
Expand All @@ -34,7 +31,7 @@ public function downOperation(): void
{
\array_walk(
$this->migrations,
function (Migration $migration) {
static function (Migration $migration) {
$migration->downOperation();
}
);
Expand Down
22 changes: 11 additions & 11 deletions src/MigrationLoader.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?php
/**
* This disaster was designed by
* @author Juan G. Rodríguez Carrión <[email protected]>
*/
declare(strict_types=1);
namespace Pccomponentes\Migration;

namespace PcComponentes\Migration;

final class MigrationLoader
{
private $dir;
private $migrationArgs;
private string $dir;
private array $migrationArgs;

public function __construct(string $dir, array $migrationArgs)
{
Expand All @@ -19,10 +16,12 @@ public function __construct(string $dir, array $migrationArgs)

public function load(array $migrationClassNames)
{
return \array_map(
[$this, 'loadOne'],
$migrationClassNames
);
$migrations = [];
foreach ($migrationClassNames as $migrationClassName) {
$migrations[] = $this->loadOne($migrationClassName);
}

return $migrations;
}

private function loadOne(string $className): Migration
Expand All @@ -36,6 +35,7 @@ private function loadOne(string $className): Migration
}

require_once $classFile;

return (new \ReflectionClass($className))->newInstanceArgs($this->migrationArgs);
}
}
21 changes: 9 additions & 12 deletions tests/MigrationCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
<?php
/**
* This disaster was designed by
* @author Juan G. Rodríguez Carrión <[email protected]>
*/
declare(strict_types=1);
namespace Pccomponentes\Migration\Tests;

use Pccomponentes\Migration\MigrationCommand;
namespace PcComponentes\Migration\Tests;

use PcComponentes\Migration\MigrationCommand;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;

class MigrationCommandTest extends TestCase
{
private const DIR = __DIR__ . '/../migration';

private $command;
private $tester;
private MigrationCommand $command;
private CommandTester $tester;

public function setUp()
public function setUp(): void
{
$this->command = new MigrationCommand('test', self::DIR, []);
$this->tester = new CommandTester($this->command);
Expand All @@ -31,7 +28,7 @@ public function given_a_command_when_execute_it_then_success()
$this->tester->execute(
[
'migrations' => ['MigrationTested'],
'--operation' => 'down'
'--operation' => 'down',
]
);

Expand All @@ -46,7 +43,7 @@ public function given_a_invalid_migration_when_execute_the_command_then_fail()
$this->tester->execute(
[
'migrations' => ['Unkonw'],
'--operation' => 'down'
'--operation' => 'down',
]
);

Expand All @@ -61,7 +58,7 @@ public function given_a_invalid_operation_when_execute_the_command_then_fail()
$this->tester->execute(
[
'migrations' => ['MigrationTested'],
'--operation' => 'unkonw'
'--operation' => 'unkonw',
]
);

Expand Down
11 changes: 4 additions & 7 deletions tests/MigrationExecutorTest.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<?php
/**
* This disaster was designed by
* @author Juan G. Rodríguez Carrión <[email protected]>
*/
declare(strict_types=1);
namespace Pccomponentes\Migration\Tests;

use Pccomponentes\Migration\MigrationExecutor;
namespace PcComponentes\Migration\Tests;

use PcComponentes\Migration\MigrationExecutor;
use PHPUnit\Framework\TestCase;

class MigrationExecutorTest extends TestCase
{
private const DIR = __DIR__ . '/../migration';

public function setUp()
public function setUp(): void
{
include_once self::DIR . '/MigrationTested.php';
}
Expand Down
Loading

0 comments on commit b53b2fd

Please sign in to comment.