Skip to content

Commit

Permalink
fixing tests and some breaking issues
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Nov 15, 2023
1 parent c38788b commit b6883d5
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/actions/GetComponentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function byType(string $type): ComponentType
}

if ($fqcn) {
return Craft::$container->get($fqcn, $this->context);
return Craft::$container->get($fqcn, ['context' => $this->context]);
}

throw new \RuntimeException('Could not find a component type definition for '.$type);
Expand Down
10 changes: 5 additions & 5 deletions src/actions/NormalizeFieldDataForComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public function handle(mixed $value, string $handle)
}

// If the field is editable, return an editable div
if ($field?->getBehavior(AttachFieldBehavior::INTERACTS_WITH_KEYSTONE)) {
if ($field->isEditableInLivePreview() && Craft::$app->getRequest()->getQueryParam('x-craft-live-preview') !== null) {
return new InlineEditData($this->component, $field, $value);
}
}
// if ($field?->getBehavior(AttachFieldBehavior::INTERACTS_WITH_KEYSTONE)) {
// if ($field->isEditableInLivePreview() && Craft::$app->getRequest()->getQueryParam('x-craft-live-preview') !== null) {
// return new InlineEditData($this->component, $field, $value);
// }
// }

return $value;
}
Expand Down
48 changes: 27 additions & 21 deletions src/models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use markhuot\keystone\events\AfterPopulateTree;
use yii\base\Event;

use function markhuot\craftpest\helpers\test\dump;
use yii\db\conditions\OrCondition;
use function markhuot\keystone\helpers\base\app;
use function markhuot\keystone\helpers\base\throw_if;

Expand Down Expand Up @@ -203,8 +203,12 @@ public function rules(): array
];
}

public function setPath(?string $path): void
public function setPath(string|array|null $path): void
{
if (is_array($path)) {
$path = implode('/', array_filter($path));
}

if (is_string($path)) {
$path = trim($path, '/');
}
Expand Down Expand Up @@ -272,34 +276,36 @@ public function getSlot(string $name = null): SlotCollection
{
$this->getType()->defineSlot($name);

if ($this->slotted !== null) {
$components = collect($this->slotted)
->where(fn (Component $component) => $component->isDirectDiscendantOf($this, $name))
->each(function (Component $component) {
$components = collect($this->slotted)
->where(fn (Component $c) => str_starts_with($c->path ?? '', $component->getChildPath() ?? ''))
->all();

$component->setSlotted($components);
});
} elseif ($this->elementId && $this->fieldId) {
if ($this->slotted === null && $this->elementId && $this->fieldId) {
$components = Component::find()
->with('data')
->where([
'elementId' => $this->elementId,
'fieldId' => $this->fieldId,
'path' => $this->getChildPath(),
'slot' => $name,
->where(['and',
['elementId' => $this->elementId],
['fieldId' => $this->fieldId],
['slot' => $name],
new OrCondition(array_filter([
! $this->getChildPath() ? ['path' => null] : null,
['like', 'path', $this->getChildPath().'%', false],
])),
])
->orderBy('sortOrder')
->collect();
// dd($components);

$this->afterPopulateTree($components);
$this->setSlotted($components->all());
} else {
$components = collect();
}

$components = collect($this->slotted)
->where(fn (Component $component) => $component->isDirectDiscendantOf($this, $name))
->each(function (Component $component) {
$components = collect($this->slotted)
->where(fn (Component $c) => str_starts_with($c->path ?? '', $component->getChildPath() ?? ''))
->all();

$component->setSlotted($components);
});

// As we delve through the render tree pass some state around so we know
// where each child is rendering and can act accordingly. For example,
//
Expand All @@ -313,7 +319,7 @@ public function getSlot(string $name = null): SlotCollection
->mergeContext($this->context)
->setRenderParent($this)
)
->toArray();
->values()->all();

return new SlotCollection($components, $this, $name);
}
Expand Down
8 changes: 4 additions & 4 deletions src/templates/attributes/alignment.twig
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@
<svg xmlns="http://www.w3.org/2000/svg" class="k-w-6" viewBox="0 0 256 256"><path d="M128,26A102,102,0,1,0,230,128,102.12,102.12,0,0,0,128,26Zm90,102a89.6,89.6,0,0,1-22.29,59.22L68.78,60.29A89.95,89.95,0,0,1,218,128ZM38,128A89.6,89.6,0,0,1,60.29,68.78L187.22,195.71A89.95,89.95,0,0,1,38,128Z"></path></svg>
</label>
<label class="btn [&:has(:checked)]:k-bg-ui-control-active">
<input type="radio" name="{{ name }}[align-items]" value="flex-start" class="k-hidden" {{ value['align-items']|default == 'left' ? 'checked' }}>
<input type="radio" name="{{ name }}[align-items]" value="flex-start" class="k-hidden" {{ value['align-items']|default == 'flex-start' ? 'checked' }}>
<svg xmlns="http://www.w3.org/2000/svg" class="k-w-6" viewBox="0 0 256 256"><path d="M222,40a6,6,0,0,1-6,6H40a6,6,0,0,1,0-12H216A6,6,0,0,1,222,40ZM206,80v96a14,14,0,0,1-14,14H152a14,14,0,0,1-14-14V80a14,14,0,0,1,14-14h40A14,14,0,0,1,206,80Zm-12,0a2,2,0,0,0-2-2H152a2,2,0,0,0-2,2v96a2,2,0,0,0,2,2h40a2,2,0,0,0,2-2Zm-76,0V216a14,14,0,0,1-14,14H64a14,14,0,0,1-14-14V80A14,14,0,0,1,64,66h40A14,14,0,0,1,118,80Zm-12,0a2,2,0,0,0-2-2H64a2,2,0,0,0-2,2V216a2,2,0,0,0,2,2h40a2,2,0,0,0,2-2Z"></path></svg>
</label>
<label class="btn [&:has(:checked)]:k-bg-ui-control-active">
<input type="radio" name="{{ name }}[align-items]" value="center" class="k-hidden" {{ value['align-items']|default == 'center' ? 'checked' }}>
<svg xmlns="http://www.w3.org/2000/svg" class="k-w-6" viewBox="0 0 256 256"><path d="M224,122H206V72a14,14,0,0,0-14-14H152a14,14,0,0,0-14,14v50H118V48a14,14,0,0,0-14-14H64A14,14,0,0,0,50,48v74H32a6,6,0,0,0,0,12H50v74a14,14,0,0,0,14,14h40a14,14,0,0,0,14-14V134h20v50a14,14,0,0,0,14,14h40a14,14,0,0,0,14-14V134h18a6,6,0,0,0,0-12ZM106,208a2,2,0,0,1-2,2H64a2,2,0,0,1-2-2V48a2,2,0,0,1,2-2h40a2,2,0,0,1,2,2Zm88-24a2,2,0,0,1-2,2H152a2,2,0,0,1-2-2V72a2,2,0,0,1,2-2h40a2,2,0,0,1,2,2Z"></path></svg>
</label>
<label class="btn [&:has(:checked)]:k-bg-ui-control-active">
<input type="radio" name="{{ name }}[align-items]" value="flex-end" class="k-hidden" {{ value['align-items']|default == 'right' ? 'checked' }}>
<input type="radio" name="{{ name }}[align-items]" value="flex-end" class="k-hidden" {{ value['align-items']|default == 'flex-end' ? 'checked' }}>
<svg xmlns="http://www.w3.org/2000/svg" class="k-w-6" viewBox="0 0 256 256"><path d="M222,216a6,6,0,0,1-6,6H40a6,6,0,0,1,0-12H216A6,6,0,0,1,222,216Zm-84-40V80a14,14,0,0,1,14-14h40a14,14,0,0,1,14,14v96a14,14,0,0,1-14,14H152A14,14,0,0,1,138,176Zm12,0a2,2,0,0,0,2,2h40a2,2,0,0,0,2-2V80a2,2,0,0,0-2-2H152a2,2,0,0,0-2,2ZM50,176V40A14,14,0,0,1,64,26h40a14,14,0,0,1,14,14V176a14,14,0,0,1-14,14H64A14,14,0,0,1,50,176Zm12,0a2,2,0,0,0,2,2h40a2,2,0,0,0,2-2V40a2,2,0,0,0-2-2H64a2,2,0,0,0-2,2Z"></path></svg>
</label>
<label class="btn [&:has(:checked)]:k-bg-ui-control-active">
<input type="radio" name="{{ name }}[align-items]" value="baseline" class="k-hidden" {{ value['align-items']|default == 'right' ? 'checked' }}>
<input type="radio" name="{{ name }}[align-items]" value="baseline" class="k-hidden" {{ value['align-items']|default == 'baseline' ? 'checked' }}>
<svg xmlns="http://www.w3.org/2000/svg" class="k-w-6" viewBox="0 0 256 256"><path d="M61.45,173.43a6,6,0,0,0,8-2.88L86.63,134h82.74l17.2,36.55A6,6,0,0,0,192,174a5.92,5.92,0,0,0,2.55-.57,6,6,0,0,0,2.88-8l-64-136a6,6,0,0,0-10.86,0l-64,136A6,6,0,0,0,61.45,173.43ZM128,46.09,163.72,122H92.28ZM222,216a6,6,0,0,1-6,6H40a6,6,0,0,1,0-12H216A6,6,0,0,1,222,216Z"></path></svg>
</label>
<label class="btn [&:has(:checked)]:k-bg-ui-control-active">
<input type="radio" name="{{ name }}[align-items]" value="stretch" class="k-hidden" {{ value['align-items']|default == 'right' ? 'checked' }}>
<input type="radio" name="{{ name }}[align-items]" value="stretch" class="k-hidden" {{ value['align-items']|default == 'stretch' ? 'checked' }}>
<svg xmlns="http://www.w3.org/2000/svg" class="k-w-6" viewBox="0 0 256 256"><path d="M34,64a6,6,0,0,1,6-6H216a6,6,0,0,1,0,12H40A6,6,0,0,1,34,64ZM216,98H40a6,6,0,0,0,0,12H216a6,6,0,0,0,0-12Zm0,40H40a6,6,0,0,0,0,12H216a6,6,0,0,0,0-12Zm0,40H40a6,6,0,0,0,0,12H216a6,6,0,0,0,0-12Z"></path></svg>
</label>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/twig/ExportTokenNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function compile(\Twig\Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write('$cb=function() use ($context){')
->write('$cb=function() use ($context, $macros){')
->write($this->getAttribute('capture') ? 'ob_start();'.PHP_EOL : '$value=')
->subcompile($this->getNode('value'))
->write($this->getAttribute('capture') ? '$value=ob_get_contents();ob_end_clean();'.PHP_EOL : '')
Expand Down
27 changes: 26 additions & 1 deletion tests/EagerLoadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@
use markhuot\craftpest\factories\Asset;
use markhuot\keystone\models\Component;

it('passes loaded components down so they\'re not refetched', function () {
$grandparent = Component::factory()->type('keystone/section')->create();
$parent = Component::factory()->type('keystone/section')->path($grandparent->id)->create();
$child = Component::factory()->type('keystone/section')->path([$grandparent->id, $parent->id])->create();
$grandchild = Component::factory()->type('keystone/section')->path([$grandparent->id, $parent->id, $child->id])->create();

// Load our data in outside of our benchmark so we can ensure there are no
// database queries below this point
$grandparent->getSlot();

$this->beginBenchmark();
expect($grandparent->getSlot())

// Check that only the parent is slotted in to the grandparent
->toHaveCount(1)

// Check that only the child is slotted in to the parent
->first()->getSlot()->toHaveCount(1)

// Check that only the grandchild is slotted in to the child
->first()->getSlot()->first()->getSlot()->toHaveCount(1);

$this->endBenchmark()->assertQueryCount(0);
});

it('eager loads assets', function () {
$fragment = Component::factory()
->type('keystone/fragment')
Expand All @@ -20,5 +45,5 @@
$this->beginBenchmark();
$fragment->getSlot()->each(fn ($c) => $c->getProp('asset')->one());
$this->endBenchmark()
->assertQueryCount(3);
->assertQueryCount(4);
});
2 changes: 1 addition & 1 deletion tests/ParseTwigForSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
it('caches component types by modification date', function () {
$fqcn = (new CompileTwigComponent('site:component-with-fields.twig', 'test/component-with-fields'))->handle();
$hash = sha1('test/component-with-fields');
$filemtime = filemtime(Craft::$app->getView()->resolveTemplate('component-with-fields.twig', \craft\web\View::TEMPLATE_MODE_SITE));
$filemtime = (new GetFileMTime())->handle(Craft::$app->getView()->resolveTemplate('component-with-fields.twig', \craft\web\View::TEMPLATE_MODE_SITE));
expect(App::parseEnv('@runtime/compiled_classes/ComponentType'.$hash.$filemtime.'.php'))->toBeFile();
});

Expand Down

0 comments on commit b6883d5

Please sign in to comment.