Skip to content

Commit

Permalink
Change to detecting eagerLoadHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Feb 26, 2024
1 parent cbee86a commit 171b5ec
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 101 deletions.
23 changes: 10 additions & 13 deletions src/services/HintsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,23 @@ public function clear(int $id): void
*/
public function checkElementQuery(ElementQuery $elementQuery): void
{
$join = $elementQuery->join[0] ?? null;

if ($join === null) {
if ($elementQuery->eagerLoadHandle === null) {
return;
}

$relationTypes = [
['relations' => '{{%relations}}'],
'{{%relations}} relations',
];
$fieldHandle = last(explode(':', $elementQuery->eagerLoadHandle));

if ($join[0] == 'INNER JOIN' && in_array($join[1], $relationTypes)) {
$fieldId = $join[2][2]['relations.fieldId'] ?? null;
if ($fieldHandle === null) {
return;
}

if (empty($fieldId)) {
return;
}
$fieldId = Craft::$app->getFields()->getFieldByHandle($fieldHandle)->id ?? null;

$this->addFieldHint($fieldId);
if ($fieldId === null) {
return;
}

$this->addFieldHint($fieldId);
}

/**
Expand Down
19 changes: 12 additions & 7 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,36 @@

## Static Analysis

To run static analysis on the module,
install [PHPStan for Craft CMS](https://github.com/craftcms/phpstan) and run the following command from the root of your project.
To run static analysis on the plugin, install [PHPStan for Craft CMS](https://github.com/craftcms/phpstan) and run the following command from the root of your project.

```shell
./vendor/bin/phpstan analyse -c vendor/putyourlightson/craft-blitz-hints/phpstan.neon --memory-limit 1G
php vendor/bin/phpstan analyse -c vendor/putyourlightson/craft-blitz-hints/phpstan.neon --memory-limit 1G
```

## Easy Coding Standard

To run the Easy Coding Standard on the plugin, install [ECS for Craft CMS](https://github.com/craftcms/ecs) and run the following command from the root of your project.

```shell
./vendor/bin/ecs check -c vendor/putyourlightson/craft-blitz-hints/ecs.php
php vendor/bin/ecs check -c vendor/putyourlightson/craft-blitz-hints/ecs.php
```

## Pest Tests

To run Pest tests, install [Craft Pest](https://craft-pest.com/) and run the following command from the root of your project.
To run Pest tests, first install [Craft Pest](https://craft-pest.com/) core as a dev dependency.

```shell
php craft pest/test --test-directory=vendor/putyourlightson/craft-blitz-hints/tests/pest
composer require markhuot/craft-pest-core:^2.0.0-rc2 --dev
```

Then run the following command from the root of your project.

```shell
php vendor/bin/pest vendor/putyourlightson/craft-blitz-hints/tests/pest --test-directory=vendor/putyourlightson/craft-blitz-hints/tests/pest --colors --display-deprecations
```

Or to run a specific test.

```shell
php craft pest/test --test-directory=vendor/putyourlightson/craft-blitz-hints/tests/pest --filter=CacheRequestTest
php vendor/bin/pest vendor/putyourlightson/craft-blitz-hints/tests/pest --test-directory=vendor/putyourlightson/craft-blitz-hints/tests/pest --colors --display-deprecations --filter=MySpecificTest
```
4 changes: 2 additions & 2 deletions tests/TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ This document outlines the test specification for the Blitz Hints module.

_Tests hints functionality._

![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Hints in templates are saved.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Hints in templates that exist in the vendor folder path are ignored.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Hint is recorded for a related element query that is lazy-loaded with the correct field ID.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Hint is not recorded for a related element query that is lazy-loaded in a template that exist in the vendor folder path.
1 change: 1 addition & 0 deletions tests/pest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/test-results.xml
16 changes: 11 additions & 5 deletions tests/pest/Feature/HintsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@
HintRecord::deleteAll();
});

test('Hints in templates are saved', function() {
saveHint('abc');
test('Hint is recorded for a related element query that is lazy-loaded with the correct field ID', function() {
saveHint();

/** @var HintRecord $hint */
$hint = HintRecord::find()->one();
$field = Craft::$app->getFields()->getFieldByHandle('relatedTo');

expect(HintRecord::find()->count())
->toEqual(1);
->toBe(1)
->and($hint->fieldId)
->toEqual($field->id);
});

test('Hints in templates that exist in the vendor folder path are ignored', function() {
saveHint(Craft::getAlias('@vendor/abc'));
test('Hint is not recorded for a related element query that is lazy-loaded in a template that exist in the vendor folder path', function() {
saveHint(Craft::getAlias('@vendor/templates/test'));

expect(HintRecord::find()->count())
->toEqual(0);
Expand Down
23 changes: 7 additions & 16 deletions tests/pest/Pest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use craft\elements\db\ElementQuery;
use craft\elements\Entry;
use markhuot\craftpest\test\TestCase;
use putyourlightson\blitzhints\BlitzHints;
Expand All @@ -18,7 +17,8 @@
|
*/

uses(TestCase::class)->in('./');
uses(TestCase::class)
->in('./');

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -48,9 +48,12 @@
|
*/

function saveHint(string $template): void
function saveHint(?string $template = null): void
{
$fieldId = Craft::$app->getFields()->getAllFields()[0]->id;
$template = $template ?? 'templates/test';
$elementQuery = Entry::find()->section('single')->one()->relatedTo;

$fieldId = Craft::$app->getFields()->getFieldByHandle('relatedTo')->id;
$hint = new HintModel([
'fieldId' => $fieldId,
'template' => $template,
Expand All @@ -61,18 +64,6 @@ function saveHint(string $template): void
$hints->shouldReceive('createHintWithTemplateLine')->andReturn($hint);
BlitzHints::getInstance()->set('hints', $hints);

/**
* @see HintsService::_checkBaseRelations
*/
$elementQuery = new ElementQuery(Entry::class);
$elementQuery->join = [
[
'INNER JOIN',
['relations' => '{{%relations}}'],
[null, null, ['relations.fieldId' => $fieldId]],
],
];

BlitzHints::getInstance()->hints->checkElementQuery($elementQuery);
BlitzHints::getInstance()->hints->save();
}
35 changes: 0 additions & 35 deletions tests/pest/README.md

This file was deleted.

14 changes: 0 additions & 14 deletions tests/pest/phpunit.xml

This file was deleted.

9 changes: 0 additions & 9 deletions tests/pest/test-results.xml

This file was deleted.

0 comments on commit 171b5ec

Please sign in to comment.