-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added factory generator to generate fake responses
- Loading branch information
Showing
7 changed files
with
238 additions
and
30 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Firebed\AadeMyData\Factories; | ||
|
||
abstract class Factory | ||
{ | ||
private string $modelName; | ||
private array $state = []; | ||
private array $except = []; | ||
|
||
public static function factoryForModel($modelName): Factory | ||
{ | ||
$namespace = dirname(__NAMESPACE__); | ||
$factoryName = $namespace . '\\Factories\\' . basename($modelName) . 'Factory'; | ||
|
||
$factory = self::newFactory($factoryName); | ||
$factory->modelName = $modelName; | ||
return $factory; | ||
} | ||
|
||
public static function newFactory($factoryName): Factory | ||
{ | ||
return new $factoryName; | ||
} | ||
|
||
public function make(array $attributes = []) | ||
{ | ||
$attributes = array_merge($this->definition(), $attributes, $this->state); | ||
|
||
if (!empty($this->except)) { | ||
$attributes = array_diff_key($attributes, array_flip($this->except)); | ||
} | ||
|
||
$instance = new $this->modelName; | ||
$instance->setAttributes($attributes); | ||
return $instance; | ||
} | ||
|
||
public function except(array $fields): self | ||
{ | ||
$this->except = $fields; | ||
|
||
return $this; | ||
} | ||
|
||
public function state(array $attributes): self | ||
{ | ||
$this->state = array_merge($attributes); | ||
|
||
return $this; | ||
} | ||
|
||
public abstract function definition(): array; | ||
} |
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,30 @@ | ||
<?php | ||
|
||
namespace Firebed\AadeMyData\Factories; | ||
|
||
class ResponseFactory extends Factory | ||
{ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'index' => 1, | ||
'invoiceUid' => strtoupper(sha1('invoice-uid')), | ||
'invoiceMark' => rand(100_000_000_000_000, 999_999_999_999_999), | ||
'statusCode' => 'Success' | ||
]; | ||
} | ||
|
||
public function uid(string $uid): self | ||
{ | ||
return $this->state([ | ||
'uid' => strtoupper(sha1($uid)) | ||
]); | ||
} | ||
|
||
public function cancelled(): self | ||
{ | ||
return $this->state([ | ||
'cancellationMark' => rand(100_000_000_000_000, 999_999_999_999_999) | ||
])->except(['index', 'invoiceUid', 'invoiceMark']); | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
namespace Firebed\AadeMyData; | ||
|
||
use Error; | ||
|
||
class Loader | ||
{ | ||
private static array $map; | ||
|
||
public static function getUrl(string $class, string $env) | ||
{ | ||
self::loadMap($env); | ||
|
||
if (!array_key_exists($class, self::$map[$env])) { | ||
throw new Error("Unspecified URL for " . basename($class)); | ||
} | ||
|
||
return self::$map[$env][$class]; | ||
} | ||
|
||
private static function loadMap(string $env): void | ||
{ | ||
if (self::mapLoaded()) { | ||
return; | ||
} | ||
|
||
self::$map = require __DIR__ . '/../config/urls.php'; | ||
|
||
if (!array_key_exists($env, self::$map)) { | ||
throw new Error("Invalid environment '$env'. Please invoke MyDataRequest::setEnvironment to set the environment value, possible values are [dev,prod]."); | ||
} | ||
} | ||
|
||
private static function mapLoaded(): bool | ||
{ | ||
return isset(self::$map); | ||
} | ||
} |
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
Oops, something went wrong.