Skip to content

Commit

Permalink
Merge pull request #4 from tithely/enum-yaml-support
Browse files Browse the repository at this point in the history
YAML, ENUM support
  • Loading branch information
joshmcrae authored Mar 30, 2020
2 parents 919806e + 85c7980 commit 7247165
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.phpunit.result.cache
.idea/
vendor/
tests/db.yml
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
"ext-pdo": "*"
},
"require-dev": {
"phpunit/phpunit": "8.3.4"
"phpunit/phpunit": "8.3.4",
"symfony/yaml": "^5.0"
},
"scripts":{
"test": "./vendor/bin/phpunit -c phpunit.xml"
},
"autoload": {
"psr-4": {
"Exo\\": "src/"
"Exo\\": "src/",
"Exo\\Tests\\": "tests/"
}
}
}
68 changes: 64 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions doc/03-testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Testing

## Data Source Config

- Copy `test/db.yml.example` to `tests/db.yml`
- Add the credentials for your data source(s)

## Create Database
Ensure the database(s) referenced in `db.yml` exist and that the configured credentials can access it.

## Run tests
`composer test`
2 changes: 2 additions & 0 deletions src/Statement/StatementBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ protected function buildType(array $options): string
return 'TIMESTAMP';
case 'uuid':
return 'CHAR(36)';
case 'enum':
return sprintf("ENUM('%s')", implode("','", $options['values']));
default:
throw new \InvalidArgumentException(sprintf('Unknown column type "%s".', $type));
}
Expand Down
12 changes: 11 additions & 1 deletion tests/MysqlHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@

namespace Exo;

use Exo\Tests\Traits\UsesYamlConfig;

class MysqlHandlerTest extends \PHPUnit\Framework\TestCase
{
use UsesYamlConfig;

/**
* @var \PDO
*/
private $pdo;

public function setUp(): void
{
$this->pdo = new \PDO('mysql:dbname=test;host=127.0.0.1', 'root', '');
$mysql = self::yaml('handlers.mysql');

$this->pdo = new \PDO(
sprintf('mysql:dbname=%s;host=%s;port=%s', $mysql['name'], $mysql['host'], $mysql['port']),
$mysql['user'],
$mysql['pass']
);
$this->pdo->exec('DROP TABLE IF EXISTS users;');
$this->pdo->exec('DROP TABLE IF EXISTS users_sessions;');
}
Expand Down
9 changes: 6 additions & 3 deletions tests/Statement/MysqlStatementBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ public function provider()
new TableOperation('users', TableOperation::CREATE, [
new ColumnOperation('id', ColumnOperation::ADD, ['type' => 'uuid', 'primary' => true]),
new ColumnOperation('username', ColumnOperation::ADD, ['type' => 'string', 'length' => 64, 'null' => false]),
new ColumnOperation('password', ColumnOperation::ADD, ['type' => 'string'])
new ColumnOperation('password', ColumnOperation::ADD, ['type' => 'string']),
new ColumnOperation('gender', ColumnOperation::ADD, ['type' => 'enum', 'values' => ['male', 'female']])
], [
new IndexOperation('username', IndexOperation::ADD, ['username'], ['unique' => true])
]),
'CREATE TABLE `users` (`id` CHAR(36) PRIMARY KEY, `username` VARCHAR(64) NOT NULL, `password` VARCHAR(255), INDEX `username` (`username`) UNIQUE);'
'CREATE TABLE `users` (`id` CHAR(36) PRIMARY KEY, `username` VARCHAR(64) NOT NULL, ' .
'`password` VARCHAR(255), `gender` ENUM(\'male\',\'female\'), INDEX `username` (`username`) UNIQUE);'
],
[
new TableOperation('users', TableOperation::ALTER, [
Expand All @@ -39,7 +41,8 @@ public function provider()
new IndexOperation('meta', IndexOperation::ADD, ['meta'], []),
new IndexOperation('username', IndexOperation::DROP, [], [])
]),
'ALTER TABLE `users` ADD COLUMN `meta` JSON AFTER `password`, MODIFY COLUMN `username` VARCHAR(255), DROP COLUMN `created_at`, ADD INDEX `meta` (`meta`), DROP INDEX `username`;'
'ALTER TABLE `users` ADD COLUMN `meta` JSON AFTER `password`, MODIFY COLUMN `username` VARCHAR(255), ' .
'DROP COLUMN `created_at`, ADD INDEX `meta` (`meta`), DROP INDEX `username`;'
],
[
new TableOperation('users', TableOperation::DROP, [], []),
Expand Down
62 changes: 62 additions & 0 deletions tests/Traits/UsesYamlConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Exo\Tests\Traits;

use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
use Exception;

trait UsesYamlConfig
{
/**
* @param string $keys A top-level key from the parsed yaml array, or
* a dot-notation set of keys, e.g. toplevel.second.third
* @return array|string|null Depending on the sort of key given, and how
* deeply the full yaml array is plumbed, a string or array may be returned.
* When the value cannot be found by the given key, we default to null.
* @throws Exception
*/
protected static function yaml(string $keys = null)
{
$yaml = self::readYamlFile();

if (empty($keys)) {
return $yaml;
}

// parse the keys, and return the config from the appropriate level (following dot-notation)
foreach (explode('.', $keys) as $segment) {
if (array_key_exists($segment, $yaml)) {
$yaml = $yaml[$segment];
} else {
return null;
}
}

return $yaml;
}

/**
* Handle validating the existence of the yaml file, parse and returning its contents
*
* @return mixed
* @throws Exception
*/
private static function readYamlFile(): array
{
$path = sprintf('%s/tests/%s', getcwd(), 'db.yml');

if (!is_readable($path)) {
throw new Exception('tests/db.yml file does not exist, or cannot be read. See README for instructions');
}
try {
$yaml = Yaml::parseFile($path);
} catch (ParseException $e) {
throw new Exception(
'Could not parse tests/db.yml file. See README for instructions. YAML parse error: ' . $e->getMessage()
);
}

return $yaml;
}
}
32 changes: 32 additions & 0 deletions tests/Util/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Exo\Util;

use Exo\Tests\Traits\UsesYamlConfig;

class ConfigTest extends \PHPUnit\Framework\TestCase
{
use UsesYamlConfig;

/**
* @throws
*/
public function testYamlExists()
{
$yaml = self::yaml();

// check that the file exists
$this->assertIsArray($yaml);
// check that at least the top-level handlers array is present
$this->assertArrayHasKey('handlers', $yaml);
// check that at least one handler is present
$this->assertNotTrue(empty($yaml['handlers']));

// check dot-notation access
$dotKey = sprintf(
'handlers.%s',
current(array_keys($yaml['handlers']))
);
$this->assertNotEmpty(self::yaml($dotKey));
}
}
7 changes: 7 additions & 0 deletions tests/db.yml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
handlers:
mysql:
host: '127.0.0.1'
port: 3306
name: 'test'
user: 'root'
pass: ''

0 comments on commit 7247165

Please sign in to comment.