-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDTT-10: Support JSON for source and destination (#12)
- Loading branch information
1 parent
e59df84
commit 81b57d2
Showing
16 changed files
with
440 additions
and
54 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
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mdtt\Definition\Validate\DataSource; | ||
|
||
class Database implements Type | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function validate(array $rawDataSourceDefinition): bool | ||
{ | ||
return isset($rawDataSourceDefinition['database']); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mdtt\Definition\Validate\DataSource; | ||
|
||
class Json implements Type | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function validate(array $rawDataSourceDefinition): bool | ||
{ | ||
return isset($rawDataSourceDefinition['selector']); | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
namespace Mdtt\Definition\Validate\DataSource; | ||
|
||
interface Type | ||
{ | ||
/** | ||
* Validates whether all required information are mentioned in the datasource definition. | ||
* | ||
* @param array<string> $rawDataSourceDefinition | ||
* | ||
* @return bool | ||
*/ | ||
public function validate(array $rawDataSourceDefinition): bool; | ||
} |
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,116 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mdtt\Definition\Validate\DataSource; | ||
|
||
use Mdtt\DataSource; | ||
use Mdtt\Exception\SetupException; | ||
use Mdtt\Utility\DataSource\Json as JsonDataSourceUtility; | ||
|
||
class Validator | ||
{ | ||
private JsonDataSourceUtility $jsonDataSourceUtility; | ||
|
||
public function __construct(JsonDataSourceUtility $jsonDataSourceUtility) | ||
{ | ||
$this->jsonDataSourceUtility = $jsonDataSourceUtility; | ||
} | ||
|
||
/** | ||
* Validates whether the datasource information in test definition is valid. | ||
* | ||
* @param string $type | ||
* @param array<string> $rawDataSourceDefinition | ||
* | ||
* @return \Mdtt\DataSource | ||
* @throws \Mdtt\Exception\SetupException | ||
*/ | ||
public function validate(string $type, array $rawDataSourceDefinition): DataSource | ||
{ | ||
if (!in_array($type, ["source", "destination"])) { | ||
throw new SetupException("Incorrect data source type is passed."); | ||
} | ||
|
||
/** @var string $dataSourceType */ | ||
$dataSourceType = $rawDataSourceDefinition['type']; | ||
if ($dataSourceType === "database") { | ||
$this->doValidateDatabase($rawDataSourceDefinition); | ||
|
||
if ($type === "source") { | ||
return new \Mdtt\Source\Database( | ||
$rawDataSourceDefinition['data'], | ||
$rawDataSourceDefinition['database'] | ||
); | ||
} | ||
|
||
if ($type === "destination") { | ||
return new \Mdtt\Destination\Database( | ||
$rawDataSourceDefinition['data'], | ||
$rawDataSourceDefinition['database'] | ||
); | ||
} | ||
} | ||
|
||
if ($dataSourceType === "json") { | ||
$this->doValidateJson($rawDataSourceDefinition); | ||
|
||
if (isset($rawDataSourceDefinition['auth_basic'])) { | ||
$this->jsonDataSourceUtility->setAuthBasicCredential($rawDataSourceDefinition['auth_basic']); | ||
} | ||
|
||
if ($type === "source") { | ||
return new \Mdtt\Source\Json( | ||
$rawDataSourceDefinition['data'], | ||
$rawDataSourceDefinition['selector'], | ||
$this->jsonDataSourceUtility, | ||
); | ||
} | ||
|
||
if ($type === "destination") { | ||
return new \Mdtt\Destination\Json( | ||
$rawDataSourceDefinition['data'], | ||
$rawDataSourceDefinition['selector'], | ||
$this->jsonDataSourceUtility | ||
); | ||
} | ||
} | ||
|
||
throw new SetupException(sprintf("Unexpected data source type %s and data source definition passed.", $type)); | ||
} | ||
|
||
/** | ||
* @param array<string> $rawDataSourceDefinition | ||
* | ||
* @return void | ||
*/ | ||
private function doValidateDatabase(array $rawDataSourceDefinition): void | ||
{ | ||
$dbValidator = new Database(); | ||
$isValid = $dbValidator->validate($rawDataSourceDefinition); | ||
if (!$isValid) { | ||
throw new SetupException( | ||
sprintf("All information are not passed for %s", $rawDataSourceDefinition['type']) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param array<string> $rawDataSourceDefinition | ||
* | ||
* @return void | ||
*/ | ||
private function doValidateJson(array $rawDataSourceDefinition): void | ||
{ | ||
$jsonValidator = new Json(); | ||
$isValid = $jsonValidator->validate($rawDataSourceDefinition); | ||
if (!$isValid) { | ||
throw new SetupException( | ||
sprintf( | ||
"All information are not passed for %s", | ||
$rawDataSourceDefinition['type'] | ||
) | ||
); | ||
} | ||
} | ||
} |
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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mdtt\Destination; | ||
|
||
use Iterator; | ||
use JsonMachine\Items; | ||
use Mdtt\DataSource; | ||
use Mdtt\Utility\DataSource\Json as JsonDataSourceUtility; | ||
|
||
class Json extends DataSource | ||
{ | ||
private string $selector; | ||
private JsonDataSourceUtility $jsonDataSourceUtility; | ||
private Items|null $items; | ||
|
||
public function __construct( | ||
string $data, | ||
string $selector, | ||
JsonDataSourceUtility $jsonDataSourceUtility | ||
) { | ||
parent::__construct($data); | ||
$this->selector = $selector; | ||
$this->jsonDataSourceUtility = $jsonDataSourceUtility; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getSelector(): string | ||
{ | ||
return $this->selector; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getItem(): Iterator | ||
{ | ||
if (!isset($this->items)) { | ||
$this->items = $this->jsonDataSourceUtility->getItems($this->data, $this->selector); | ||
} | ||
|
||
foreach ($this->items as $item) { | ||
yield $item; | ||
} | ||
} | ||
} |
Oops, something went wrong.