Skip to content

Commit

Permalink
🔧 added tests to improve coverage
Browse files Browse the repository at this point in the history
Signed-off-by: bnomei <[email protected]>
  • Loading branch information
bnomei committed Jul 24, 2024
1 parent 3dd01a4 commit 2bb11b5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
19 changes: 12 additions & 7 deletions classes/Blueprints/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Blueprint
public function __construct(private readonly string $modelClass, private ?bool $defer = null, private ?int $cache = null)
{
if (! class_exists($modelClass)) {
throw new \Exception('Model class ['.$modelClass.'] does not exist.');
throw new \Exception('Model class ['.$modelClass.'] does not exist.'); // @codeCoverageIgnore
}

$isCacheable = null;
Expand All @@ -38,14 +38,19 @@ public function __construct(private readonly string $modelClass, private ?bool $
}

public function __toString(): string
{
return $this->toYaml();
}

public function toYaml(): string
{
return Yaml::encode($this->toArray());
}

public function blueprintCacheKeyFromModel(): string
{
if (! class_exists($this->modelClass)) {
throw new \Exception('Model class ['.$this->modelClass.'] does not exist.');
throw new \Exception('Model class ['.$this->modelClass.'] does not exist.'); // @codeCoverageIgnore
}

$ref = new ReflectionClass($this->modelClass);
Expand Down Expand Up @@ -85,7 +90,7 @@ public function toArray(): array
public static function getBlueprintFieldsFromReflection(string $class): array
{
if (! class_exists($class)) {
throw new \Exception('Model class ['.$class.'] does not exist.');
throw new \Exception('Model class ['.$class.'] does not exist.'); // @codeCoverageIgnore
}

$fields = [];
Expand Down Expand Up @@ -128,13 +133,13 @@ public static function getBlueprintFieldsFromReflection(string $class): array
public static function getBlueprintFromYamlFile(string $class): array
{
if (! class_exists($class)) {
throw new \Exception('Model class ['.$class.'] does not exist.');
throw new \Exception('Model class ['.$class.'] does not exist.'); // @codeCoverageIgnore
}

$rc = new ReflectionClass($class);
$filename = $rc->getFileName();
if (! $filename) {
throw new \Exception('Model class ['.$class.'] does not have a filename.');
throw new \Exception('Model class ['.$class.'] does not have a filename.'); // @codeCoverageIgnore
}
$yamlFile = str_replace('.php', '.yml', $filename);
if (F::exists($yamlFile)) {
Expand All @@ -147,7 +152,7 @@ public static function getBlueprintFromYamlFile(string $class): array
public static function getBlueprintFromClass(string $class): array
{
if (! class_exists($class)) {
throw new \Exception('Model class ['.$class.'] does not exist.');
throw new \Exception('Model class ['.$class.'] does not exist.'); // @codeCoverageIgnore
}

$blueprint = [];
Expand All @@ -172,7 +177,7 @@ public static function getBlueprintFromClass(string $class): array
public static function getBlueprintUsingReflection(string $class): array
{
if (! class_exists($class)) {
throw new \Exception('Model class ['.$class.'] does not exist.');
throw new \Exception('Model class ['.$class.'] does not exist.'); // @codeCoverageIgnore
}

$fields = self::getBlueprintFieldsFromReflection($class);
Expand Down
2 changes: 1 addition & 1 deletion classes/Blueprints/HasProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function toArray(): array
}
$data = json_decode($json_encode, true);
if (! is_array($data)) {
$data = [];
$data = []; // @codeCoverageIgnore
}
ksort($data);

Expand Down
2 changes: 1 addition & 1 deletion classes/Blueprints/IsArrayable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function toArray(): array

$data = json_decode($json_encode, true);
if (! is_array($data)) {
$data = [];
$data = []; // @codeCoverageIgnore
}
ksort($data);

Expand Down
25 changes: 25 additions & 0 deletions tests/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,28 @@
$count = BlueprintCache::preloadCachedBlueprints();
expect($count)->toBeGreaterThan(0);
});

it('can Ink a section', function () {
$fields = \Bnomei\Ink::fields(fields: [

]);

expect($fields->toArray())->toBeArray();

$invalid = \Bnomei\Ink::bogus();
expect($invalid)->toBeNull();
});

it('can have deferred blueprints which load on ready()', function () {
$blueprint = DynamoPage::blueprintFromAttributes();
expect($blueprint)->toBeNull();

Blueprint::loadPluginsAfter();
expect(\Kirby\Cms\Blueprint::$loaded)->toHaveKey('pages/dynamo');
});

it('can be exported as yaml', function () {
$blueprint = new Blueprint(HomePage::class);

expect($blueprint->toYaml())->toBeString();
});

0 comments on commit 2bb11b5

Please sign in to comment.