forked from tractorcow/cow
-
Notifications
You must be signed in to change notification settings - Fork 8
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 #72 from creative-commoners/pulls/2.1/schema-valid…
…ator NEW Add schema:validate command and return command exit code
- Loading branch information
Showing
8 changed files
with
190 additions
and
6 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
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,66 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Cow\Commands\Schema; | ||
|
||
use Exception; | ||
use JsonSchema\Validator; | ||
use SilverStripe\Cow\Commands\Command; | ||
use SilverStripe\Cow\Utility\Config; | ||
use SilverStripe\Cow\Utility\SchemaValidator; | ||
|
||
/** | ||
* Validates the cow configuration file against the cow schema | ||
*/ | ||
class Validate extends Command | ||
{ | ||
protected $name = 'schema:validate'; | ||
|
||
protected $description = 'Validate the cow configuration file'; | ||
|
||
/** | ||
* Get the contents of the cow configuration file in the current directory | ||
* | ||
* @return array | ||
* @throws Exception | ||
*/ | ||
protected function getCowData() | ||
{ | ||
$directory = getcwd(); | ||
$cowFile = $directory . '/.cow.json'; | ||
if (!file_exists($cowFile)) { | ||
throw new Exception('.cow.json does not exist in current directory'); | ||
} | ||
|
||
return Config::parseContent(file_get_contents($cowFile), false); | ||
} | ||
|
||
/** | ||
* Validate the current directory's cow configuration file against the schema | ||
* | ||
* @return int Process exit code | ||
* @throws Exception | ||
*/ | ||
protected function fire() | ||
{ | ||
$cowData = $this->getCowData(); | ||
|
||
/** @var Validator $validator */ | ||
$validator = SchemaValidator::validate($cowData); | ||
|
||
if ($validator->isValid()) { | ||
$this->output->writeln('<info>Cow schema is valid!</info>'); | ||
return 0; | ||
} | ||
|
||
$this->output->writeln('<info>Cow schema failures:</info>'); | ||
foreach ($validator->getErrors() as $error) { | ||
$this->output->writeln('<error>' . $error['property'] . ': ' . $error['message'] . '</error>'); | ||
} | ||
return 1; | ||
} | ||
|
||
protected function configureOptions() | ||
{ | ||
// noop | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Cow\Utility; | ||
|
||
use Exception; | ||
use JsonSchema\Validator; | ||
|
||
/** | ||
* Validates input data against the cow schema file | ||
*/ | ||
class SchemaValidator | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
const SCHEMA_FILENAME = 'cow.schema.json'; | ||
|
||
/** | ||
* Loads and return the cow schema | ||
* | ||
* @return array | ||
* @throws Exception | ||
*/ | ||
public static function getSchema() | ||
{ | ||
$schemaPath = dirname(dirname(__DIR__)) . '/' . self::SCHEMA_FILENAME; | ||
return Config::loadFromFile($schemaPath); | ||
} | ||
|
||
/** | ||
* Validate the incoming object data against the given schema. If no schema is provided then the default cow | ||
* schema will be loaded. | ||
* | ||
* @param array $objectData | ||
* @param array $schema If not provided, the default will be used | ||
* @return Validator | ||
*/ | ||
public static function validate($objectData, $schema = null) | ||
{ | ||
if (is_null($schema)) { | ||
$schema = self::getSchema(); | ||
} | ||
|
||
$validator = new Validator(); | ||
$validator->validate($objectData, $schema); | ||
|
||
return $validator; | ||
} | ||
} |
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,59 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Cow\Tests\Utility; | ||
|
||
use PHPUnit_Framework_TestCase; | ||
use SilverStripe\Cow\Utility\SchemaValidator; | ||
|
||
class SchemaValidatorTest extends PHPUnit_Framework_TestCase | ||
{ | ||
protected $cowSchema; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->cowSchema = file_get_contents(dirname(dirname(__DIR__)) . '/cow.schema.json'); | ||
} | ||
|
||
public function testValidSchema() | ||
{ | ||
$cowConfig = <<<JSON | ||
{ | ||
"child-stability-inherit": [ | ||
"cwp/cwp", | ||
"cwp/cwp-core" | ||
], | ||
"upgrade-only": [ | ||
"silverstripe/cms", | ||
"silverstripe/framework", | ||
"silverstripe/siteconfig", | ||
"silverstripe/reports", | ||
"symbiote/silverstripe-gridfieldextensions" | ||
], | ||
"vendors": [ | ||
"cwp", | ||
"silverstripe", | ||
"symbiote" | ||
] | ||
} | ||
JSON; | ||
|
||
$validator = SchemaValidator::validate(json_decode($cowConfig)); | ||
|
||
$this->assertTrue($validator->isValid()); | ||
} | ||
|
||
public function testInvalidSchema() | ||
{ | ||
$cowConfig = <<<JSON | ||
{ | ||
"vendors": "all-the-things" | ||
} | ||
JSON; | ||
|
||
$validator = SchemaValidator::validate(json_decode($cowConfig)); | ||
|
||
$this->assertFalse($validator->isValid()); | ||
} | ||
} |