if (!isset($context['target_instance'])) {
      throw new LogicException('$context[\'target_instance\'] must be set to denormalize with the FieldItemNormalizer');
    }
    if ($context['target_instance']->getParent() == NULL) {
      throw new LogicException('The field item passed in via $context[\'target_instance\'] must have a parent set.');
    }

Is incorrect LogicException should be \LogicException

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jessebeach’s picture

We need to do two things for this issue.

One is super simple: Roll a patch changing LogicException to \LogicException -- not the preceding slash.

Two is a little more tricky. We need to create a test that will trigger the two exceptions noted in the issue summary. These two exceptions are currently untested.

Boobaa’s picture

Attached is a patch with tests only and another patch with tests and fix.

Status: Needs review » Needs work

The last submitted patch, 2: 2225333-fielditemnormalizer-logicexception-2-tests-only.patch, failed testing.

dawehner’s picture

  1. +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/FieldItemNormalizer.php
    @@ -45,10 +45,10 @@ public function normalize($field_item, $format = NULL, array $context = array())
    -      throw new LogicException('$context[\'target_instance\'] must be set to denormalize with the FieldItemNormalizer');
    +      throw new \LogicException('$context[\'target_instance\'] must be set to denormalize with the FieldItemNormalizer');
    ...
    -      throw new LogicException('The field item passed in via $context[\'target_instance\'] must have a parent set.');
    +      throw new \LogicException('The field item passed in via $context[\'target_instance\'] must have a parent set.');
    

    Note: There is a logicexception as part of the serializer namespace, see Symfony\Component\Serializer\Exception\LogicException. The original code intended to use that, as it was probably copied from another place. On top of that I really wonder why we don't use an InvalidArgumentException given that seems to have a better semantic

  2. +++ b/core/modules/hal/lib/Drupal/hal/Tests/DenormalizeTest.php
    @@ -23,6 +25,33 @@ public static function getInfo() {
    +    catch (\LogicException $e) {
    ...
    +    catch (\LogicException $e) {
    

    These bits probably have to be adapted based on the previous point.

Boobaa’s picture

Firstly, @alexpott asked at #drupaldevdays to convert this mocking mess to proper getMock() stuff, so I implemented that one instead.

Secondly, I have changed from LogicException to InvalidArgumentException as @dawehner asked.

Attached is a patch with tests only and another patch with tests and the fix.

Status: Needs review » Needs work

The last submitted patch, 5: 2225333-fielditemnormalizer-logicexception-5-tests-only.patch, failed testing.

Boobaa’s picture

Status: Needs work » Needs review

The bot threw this issue back to NW because of the patch which contains only the tests, so it must have failed. The other patch contains tests AND the fix and passed, so setting the issue to NR, as @ianthomas_uk suggested on IRC.

jessebeach’s picture

Status: Needs review » Reviewed & tested by the community

Read through the code; it looks succinct and well written. The fix changes the incorrect LogicException to an \InvalidArgumentException.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/hal/tests/Drupal/hal/Tests/FieldItemNormalizerDenormalizeExceptionsUnitTest.php
    @@ -0,0 +1,62 @@
    +    try {
    +      $field_item_normalizer->denormalize($data, $class, NULL, $context);
    +      $this->fail('No exception has been thrown when denormalizing with a target_instance without proper getParent() return value');
    +    }
    +    catch (\InvalidArgumentException $e) {
    +      $this->assertInstanceOf('InvalidArgumentException', $e, 'InvalidArgumentException thrown: ' . $e->getMessage());
    +    }
    

    No need for this - can use the phpunit annotation @expectedException and this should be two tests or use a data provider

  2. +++ b/core/modules/hal/tests/Drupal/hal/Tests/FieldItemNormalizerDenormalizeExceptionsUnitTest.php
    @@ -0,0 +1,62 @@
    \ No newline at end of file
    

    No new line at end of file

Plus lets fix and test Drupal\hal\Normalizer\FieldNormalizer too as this has the same bug.

Boobaa’s picture

Here we go. Changes:

  1. Using the phpunit annotation @expectedException.
  2. Fixing and testing Drupal\hal\Normalizer\FieldNormalizer too.
  3. Using a common data provider for the two.
dawehner’s picture

Do we also want to unit test the other class?

+++ b/core/modules/hal/lib/Drupal/hal/Normalizer/FieldItemNormalizer.php
@@ -45,10 +45,10 @@ public function normalize($field_item, $format = NULL, array $context = array())
+      throw new \InvalidArgumentException('$context[\'target_instance\'] must be set to denormalize with the FieldItemNormalizer');
...
+      throw new \InvalidArgumentException('The field item passed in via $context[\'target_instance\'] must have a parent set.');

+++ b/core/modules/hal/lib/Drupal/hal/Normalizer/FieldNormalizer.php
@@ -62,10 +62,10 @@ public function normalize($field, $format = NULL, array $context = array()) {
+      throw new \InvalidArgumentException('$context[\'target_instance\'] must be set to denormalize with the FieldNormalizer');
...
+      throw new \InvalidArgumentException('The field passed in via $context[\'target_instance\'] must have a parent set.');

You could also use Symfony\Component\Serializer\Exception\InvalidArgumentException.

Status: Needs review » Needs work
Boobaa’s picture

Boobaa’s picture

Boobaa’s picture

Rerolled the pair of patches as @dawehner asked at #drupaldevdays: testing FieldItemNormalizer::denormalize() and FieldNormalizer::denormalize() in two different classes, but they have a common abstract ancestor as the dataProvider for the two is the same.

Additionally, changed from \InvalidArgumentException to \Symfony\Component\Serializer\Exception\InvalidArgumentException which is eventually the same at code level, but makes more sense semantically.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Awesome!

Status: Reviewed & tested by the community » Needs work
jessebeach’s picture

Status: Needs work » Reviewed & tested by the community

The "fail" patch failed in #17. The green patch passed.

dawehner’s picture

Awesome!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Nice test!

Committed 5a0edc3 and pushed to 8.x. Thanks!

  • Commit 5a0edc3 on 8.x by alexpott:
    Issue #2225333 by Boobaa: LogicException in Drupal\hal\Normalizer\...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.