Skip to content

Commit

Permalink
ACMS-1925: update error message and throw exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakmishra2 committed Aug 21, 2023
1 parent 98c9248 commit 31b229f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 30 deletions.
40 changes: 13 additions & 27 deletions src/Validation/StarterKitValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,41 @@

namespace AcquiaCMS\Cli\Validation;

use AcquiaCMS\Cli\Exception\AcmsCliException;
use JsonSchema\Validator;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Validation service to validate each key in starter kit.
*/
class StarterKitValidation {

/**
* Holds the symfony console output object.
*
* @var \Symfony\Component\Console\Output\OutputInterface
*/
protected $output;

/**
* Constructs an object.
*
* @param \Symfony\Component\Console\Output\OutputInterface $output
* Returns an absolute root path to project.
*/
public function __construct(
OutputInterface $output) {
$this->output = $output;
}

/**
* Loop each starter-kit and send it for validation.
*/
public function validateStarterKits(array $schema, array &$starterkits): void {
foreach ($starterkits as $starterkit) {
$this->validateStarterKit($schema, $starterkit);
$errorMessage = '';
foreach ($starterkits as $name => $starterkit) {
$errorMessage .= $this->validateStarterKit($schema, $starterkit, $name);
}
if ($errorMessage) {
throw new AcmsCliException("\n" . $errorMessage . "\n");
}
}

/**
* Validates starter-kit.
*/
public function validateStarterKit(array $schema, array $starterkit): void {
public function validateStarterKit(array $schema, array $starterkit, string $name): ?string {
$validator = new Validator();
$validator->validate($starterkit, $schema);
if (!$validator->isValid()) {
$errors = [];
foreach ($validator->getErrors() as $key => $error) {
$errors[] = "Property " . $error['property'] . " " . $error['constraint'] . " " . $error['message'];
$this->output->writeln("<error>" . $errors[$key] . "</error>");
$errors = '';
foreach ($validator->getErrors() as $error) {
$errors .= $error['message'] . " in '" . $error['property'] . "' property type." . "\n";
}
exit;
return $name . ':' . "\n" . $errors;
}
return '';
}

}
4 changes: 2 additions & 2 deletions tests/unit/CliTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function setUp(): void {
$this->projectDirectory = getcwd();
$this->rootDirectory = $this->projectDirectory;
$container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
$this->starterKitValidation = new StarterKitValidation($output);
$this->starterKitValidation = new StarterKitValidation();
$this->acquiaCli = new Cli($this->projectDirectory, $this->rootDirectory, $output, $container, $this->starterKitValidation);
}

Expand All @@ -75,7 +75,7 @@ public function testExecute() :void {
* Tests starter-kit validation.
*/
public function testValidateStarterKit(): void {
$starterKits = $this->getAcmsFileContents();
$starterKits = $this->getAcmsFileContents()['starter_kits'];
$schema = $this->acquiaCli->getAcquiaCmsFile($this->projectDirectory . '/acms/schema.json');
$this->starterKitValidation->validateStarterKits($schema, $starterKits);
$this->assertIsArray($starterKits);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Helpers/InstallerQuestionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected function setUp(): void {
$this->projectDirectory = getcwd();
$this->rootDirectory = $this->projectDirectory;
$container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
$this->starterKitValidation = new StarterKitValidation($output);
$this->starterKitValidation = new StarterKitValidation();
$this->acquiaCli = new Cli($this->projectDirectory, $this->rootDirectory, $output, $container, $this->starterKitValidation);
$this->installerQuestions = new InstallerQuestions();
$this->acmsBuildQuestions = $this->acquiaCli->getInstallerQuestions('build');
Expand Down

0 comments on commit 31b229f

Please sign in to comment.