diff --git a/src/services/HintsService.php b/src/services/HintsService.php index eca7307..71856d2 100644 --- a/src/services/HintsService.php +++ b/src/services/HintsService.php @@ -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); } /** diff --git a/tests/README.md b/tests/README.md index 6c20d04..dd555b6 100644 --- a/tests/README.md +++ b/tests/README.md @@ -2,11 +2,10 @@ ## 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 @@ -14,19 +13,25 @@ install [PHPStan for Craft CMS](https://github.com/craftcms/phpstan) and run the 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 ``` diff --git a/tests/TESTS.md b/tests/TESTS.md index 35272f5..20c3a5e 100644 --- a/tests/TESTS.md +++ b/tests/TESTS.md @@ -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. diff --git a/tests/pest/.gitignore b/tests/pest/.gitignore new file mode 100644 index 0000000..e2a69e6 --- /dev/null +++ b/tests/pest/.gitignore @@ -0,0 +1 @@ +/test-results.xml diff --git a/tests/pest/Feature/HintsTest.php b/tests/pest/Feature/HintsTest.php index 48b5527..aabb6dc 100644 --- a/tests/pest/Feature/HintsTest.php +++ b/tests/pest/Feature/HintsTest.php @@ -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); diff --git a/tests/pest/Pest.php b/tests/pest/Pest.php index 733aff1..f647ce2 100644 --- a/tests/pest/Pest.php +++ b/tests/pest/Pest.php @@ -1,6 +1,5 @@ in('./'); +uses(TestCase::class) + ->in('./'); /* |-------------------------------------------------------------------------- @@ -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, @@ -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(); } diff --git a/tests/pest/README.md b/tests/pest/README.md deleted file mode 100644 index 73671bd..0000000 --- a/tests/pest/README.md +++ /dev/null @@ -1,35 +0,0 @@ -# Testing - -## Usage - -1. Install the [Craft Pest](https://craft-pest.com) plugin. - ```shell - composer require-dev markhuot/craft-pest --dev - php craft plugin/install pest - ``` -2. Copy `phpunit.xml` to the root of your project. -3. Execute the following command from the root of your project. - ```shell - php craft pest/test --test-directory=vendor/putyourlightson/craft-blitz-hints/tests/pest - ``` - -### Makefile - -A Makefile can be used to simplify the running of tests. - -```makefile -# Default values -vendor?=putyourlightson -plugin?=blitz-hints -filter?=test -test: - php craft pest/test --test-directory=vendor/$(vendor)/craft-$(plugin)/tests/pest --filter=$(filter) -``` - -```shell -# Run tests using the default values -make test - -# Run tests using all optional values -make test vendor=putyourlightson plugin=blitz-hints filter=queue -``` diff --git a/tests/pest/phpunit.xml b/tests/pest/phpunit.xml deleted file mode 100644 index d1b4502..0000000 --- a/tests/pest/phpunit.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - vendor/putyourlightson/craft-blitz-hints/pest/tests - - - - diff --git a/tests/pest/test-results.xml b/tests/pest/test-results.xml deleted file mode 100644 index 25632cb..0000000 --- a/tests/pest/test-results.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - -