From 90cc5adcd901f33b0265d86a03a2dbbeaa5ee7b9 Mon Sep 17 00:00:00 2001 From: Alexander Dmitryuk Date: Mon, 31 Oct 2022 13:45:51 +0600 Subject: [PATCH] Topic 112 (#328) * Topic 112 * cs-fixer * disable get_class_to_class_keyword * Fix tests Co-authored-by: a.dmitryuk --- .github/workflows/ci-5.x.yml | 6 ++ .php-cs-fixer.php | 1 + composer.json | 3 +- .../views/Audit/helpers/helper.html.twig | 5 +- tests/Twig/Views/MacrosTest.php | 76 +++++++++++++++++++ 5 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 tests/Twig/Views/MacrosTest.php diff --git a/.github/workflows/ci-5.x.yml b/.github/workflows/ci-5.x.yml index ca17968e..d0c1f7b4 100644 --- a/.github/workflows/ci-5.x.yml +++ b/.github/workflows/ci-5.x.yml @@ -89,6 +89,9 @@ jobs: if: matrix.symfony == '5.*' run: SYMFONY_REQUIRE="^5.4" composer update --no-progress --ansi --prefer-stable + - name: Install auditor from source + run: composer reinstall damienharper/auditor --prefer-install=source + - name: Install PHPStan run: composer install --no-progress --ansi --working-dir=tools/phpstan @@ -153,6 +156,9 @@ jobs: if: matrix.symfony == '6.*' run: SYMFONY_REQUIRE="^6.0" composer update --no-progress --ansi --prefer-stable + - name: Install auditor from source + run: composer reinstall damienharper/auditor --prefer-install=source + - name: Install PHPStan run: composer install --no-progress --ansi --working-dir=tools/phpstan diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index 0d90edd2..f8488e83 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -41,6 +41,7 @@ 'simplified_if_return' => true, 'simplified_null_return' => false, 'static_lambda' => true, + 'get_class_to_class_keyword' => false, ]) ->setFinder(PhpCsFixer\Finder::create() ->in(__DIR__) diff --git a/composer.json b/composer.json index 16caa6b2..ba698106 100644 --- a/composer.json +++ b/composer.json @@ -48,7 +48,8 @@ "symfony/framework-bundle": "^4.4|^5.0|^6.0", "symfony/var-dumper": "^4.0|^5.0|^6.0", "symfony/webpack-encore-bundle": "^1.12", - "twig/extensions": "^1.5" + "twig/extensions": "^1.5", + "doctrine/data-fixtures": "^1.4" }, "conflict": { "doctrine/persistence": "<1.3" diff --git a/src/Resources/views/Audit/helpers/helper.html.twig b/src/Resources/views/Audit/helpers/helper.html.twig index 4990361d..57a972f5 100644 --- a/src/Resources/views/Audit/helpers/helper.html.twig +++ b/src/Resources/views/Audit/helpers/helper.html.twig @@ -55,8 +55,9 @@ {% set target_subject = '' %} {% set target_label = '' %} {% if target is defined and target is not null %} - {% set target_subject = target.class ~ '#' ~ target.id %} - {% set target_link = path('dh_auditor_show_entity_history', { 'entity': helper.namespaceToParam(target.class), 'id': target.id }) %} + {% set target_id = target.pkName is defined ? target[target.pkName] : target.id %} + {% set target_subject = target.class ~ '#' ~ target_id %} + {% set target_link = path('dh_auditor_show_entity_history', { 'entity': helper.namespaceToParam(target.class), 'id': target_id }) %} {% if target_subject != target.label %} {% set target_label = '(' ~ helper.dump(target)|trim ~ ')' %} {% endif %} diff --git a/tests/Twig/Views/MacrosTest.php b/tests/Twig/Views/MacrosTest.php new file mode 100644 index 00000000..115f1868 --- /dev/null +++ b/tests/Twig/Views/MacrosTest.php @@ -0,0 +1,76 @@ +get('twig'); + if (!$twig instanceof Environment) { + self::markTestIncomplete('Twig missing'); + } + $twig->addExtension(new StringLoaderExtension()); + $em = $this->createEntityManager([ + __DIR__.'/../../../vendor/damienharper/auditor/tests/Provider/Doctrine/Fixtures/Issue112', + ]); + $entity = new DummyEntity(); + $entity->setPrimaryKey(1); + $entry = Entry::fromArray([ + 'diffs' => json_encode([ + 'source' => [ + 'label' => 'Example1', + ], + 'target' => $this->summarize($em, $entity), + ]), + 'type' => 'associate', + 'object_id' => '2', + ]); + + $template = twig_template_from_string($twig, $this->getTemplateAsString()); + $response = $template->render([ + 'entry' => $entry, + 'entity' => \get_class($entity), + ]); + self::assertSame($this->getExpected(), trim($response)); + } + + private function getTemplateAsString(): string + { + return <<<'TWIG' + {% import '@DHAuditor/Audit/helpers/helper.html.twig' as helper %} + {{ helper.summarize(entity, entry) }} + TWIG; + } + + private function getExpected(): string + { + return <<<'EXPECTED' + + DH\Auditor\Tests\Provider\Doctrine\Fixtures\Issue112\DummyEntity#2 + (Example1) has been associated to + DH\Auditor\Tests\Provider\Doctrine\Fixtures\Issue112\DummyEntity#1 + by an anonymous user + EXPECTED; + } +}