-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from tithely/enum-yaml-support
YAML, ENUM support
- Loading branch information
Showing
10 changed files
with
204 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.phpunit.result.cache | ||
.idea/ | ||
vendor/ | ||
tests/db.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: '' |