Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Oct 29, 2023
1 parent 2a8f91e commit 0f5d398
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
],
"require-dev": {
"markhuot/craft-pest-core": "dev-main",
"markhuot/craft-pest-core": "dev-sequence",
"phpstan/phpstan": "^1.10",
"laravel/pint": "^1.13",
"craftcms/craft": "dev-main"
Expand Down
20 changes: 20 additions & 0 deletions src/base/ContextBag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace markhuot\keystone\base;

class ContextBag
{
public function __construct(
protected array $context
) { }

public function __isset($key)
{
return true;
}

public function __get($key)
{
return $this->context[$key] ?? null;
}
}
19 changes: 15 additions & 4 deletions src/factories/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use markhuot\keystone\models\ComponentData;
use SplObjectStorage;

use function markhuot\craftpest\helpers\test\dd;

class Component extends Factory
{
public static $tests;
Expand All @@ -20,9 +22,9 @@ public function newElement()

public function definition(int $index = 0)
{
$data = new ComponentData();
$data->type = 'keystone/text';
$data->save();
// $data = new ComponentData();
// $data->type = 'keystone/text';
// $data->save();

$field = collect(\Craft::$app->getFields()->getAllFields())
->first(fn (FieldInterface $field) => get_class($field) === Keystone::class);
Expand All @@ -35,14 +37,23 @@ public function definition(int $index = 0)
return [
'elementId' => $entry->id,
'fieldId' => $field->id,
'dataId' => $data->id,
// 'dataId' => $data->id,
'sortOrder' => 0,
'path' => null,
];
}

public function store($element)
{
if (is_null($element->data->type)) {
$element->setType('keystone/text');
}

if (is_null($element->getAttribute('dataId'))) {
$element->data->save();
$element->dataId = $element->data->id;
}

return $element->save();
}
}
5 changes: 3 additions & 2 deletions src/models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use markhuot\keystone\actions\NormalizeFieldDataForComponent;
use markhuot\keystone\base\AttributeBag;
use markhuot\keystone\base\ComponentType;
use markhuot\keystone\base\ContextBag;
use markhuot\keystone\base\SlotDefinition;
use markhuot\keystone\collections\SlotCollection;
use markhuot\keystone\db\ActiveRecord;
Expand Down Expand Up @@ -146,9 +147,9 @@ public function setContext(array $context): self
return $this;
}

public function getContext(): Collection
public function getContext(): ContextBag
{
return collect($this->context);
return new ContextBag($this->context);
}

public function safeAttributes()
Expand Down
7 changes: 4 additions & 3 deletions src/models/ComponentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use markhuot\keystone\db\ActiveRecord;
use markhuot\keystone\db\Table;

use function markhuot\craftpest\helpers\test\dd;
use function markhuot\keystone\helpers\base\throw_if;
use function markhuot\keystone\helpers\data\data_forget;

Expand Down Expand Up @@ -98,9 +99,9 @@ public function getDataAttributes(): array
return [];
}

throw_if(! is_array($this->data['_attributes']), '_attributes should always be an array of attributes => attribute values');
throw_if(! is_array($this->getData()['_attributes']), '_attributes should always be an array of attributes => attribute values');

return $this->data['_attributes'];
return $this->getData()['_attributes'];
}

/**
Expand Down Expand Up @@ -145,7 +146,7 @@ public function offsetSet(mixed $offset, mixed $value): void

public function offsetUnset(mixed $offset): void
{
$old = $this->data ?? [];
$old = $this->getData() ?? [];
unset($old[$offset]);

$this->setAttribute('data', $old);
Expand Down
5 changes: 3 additions & 2 deletions tests/ComponentDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@
$data = new ComponentData;
$data['foo'] = 'bar';

expect($data->data['foo'])->toBe('bar');
expect($data['foo'])->toBe('bar');

unset($data['foo']);

expect($data->data)->toBeArray()->toBeEmpty();
expect($data['foo'])->toBeNull();
expect($data->getData())->toBeEmpty();
});
43 changes: 43 additions & 0 deletions tests/ContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use markhuot\keystone\models\Component;

use function markhuot\craftpest\helpers\test\dd;

it('saves type', function () {
$component = Component::factory()->type('site/components/with-context')->create();
$component->refresh();

expect($component->data->type)->toBe('site/components/with-context');
});

it('renders context', function () {
$component = Component::factory()->type('site/components/with-context')->create();
$response = $component->setContext(['foo' => 'bar'])->render();

expect($response)->toBe('bar');
});

it('renders collections with context', function () {
$fragment = Component::factory()->type('keystone/fragment')->create();
Component::factory()->type('site/components/with-context')->path($fragment->id)->create();

$response = $fragment->getSlot()->render(['foo' => 'bar']);
expect(trim((string) $response))->toBe('bar');
});

it('renders object templates from context', function () {
$text = Component::factory()->type('keystone/text')->create();
$text->data->merge(['text' => '{foo}'])->save();

expect(trim(strip_tags($text->setContext(['foo' => 'bar'])->render())))->toBe('bar');
});

it('renders object templates from slot context', function () {
$fragment = Component::factory()->type('keystone/fragment')->create();
$text = Component::factory()->type('keystone/text')->path($fragment->id)->create();
$text->data->merge(['text' => '{foo}'])->save();

$response = $fragment->getSlot()->render(['foo' => 'bar']);
expect(trim(strip_tags((string) $response)))->toBe('bar');
});
1 change: 1 addition & 0 deletions tests/templates/components/with-context.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ component.getContext().foo }}

0 comments on commit 0f5d398

Please sign in to comment.