Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect Annotation::fetchAll() return structure for single Examples attribute #6752

Open
wants to merge 3 commits into
base: 5.1
Choose a base branch
from

Conversation

xEdelweiss
Copy link

I had an issue similar to #6726, caused by how Annotation::fetchAll() handles the single #[Examples] attribute.

Here are comparison of fetchAll() return structures:

# Multiple Example attributes:
array (
  0 => array (
    0 => 'example 1',
  ),
  1 => array (
    0 => 'example 2',
  ),
)

# ❌ Single Example attribute (actual):
array (
  0 => 'example 1', 
)

# ✔️ Single Example attribute (expected):
array (
  0 => array (
    0 => 'example 1',
  ),
)

This PR includes a fix and unit-tests update for this issue. It also has a test for the #[Given] attribute to show that the changes do not break it.

@@ -130,6 +130,9 @@ public function fetchAll(string $annotation): array
$attr = $this->attribute($annotation);
if ($attr) {
if (!$attr->isRepeated()) {
if ($annotation === 'example') {
return [$attr->getArguments()];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change broke tests\cli\RunCest.php:runWithExamples

Please fix it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've missed this test, but it is the one that highlights the issue and conflicts with the changes I propose in this PR.

It is confusing that a single #[Example] attribute is treated not in the same way as multiple ones.
This also forces you to update attribute arguments when you move from single #[Example] to multiple and vice versa.

Like in this flow:

// write simple test - works

#[Examples([1, 1])]
public function example1(AttrsTester $I, Example $e)
{
    $I->assertEquals($e[1], $e[0]);
}

// duplicate line, update data - fails

#[Examples([1, 1])]
#[Examples([2, 2])]
public function example2(AttrsTester $I, Example $e)
{
    $I->assertEquals($e[1], $e[0]);
}

// unwrap arrays to make it work - works

#[Examples(1, 1)]
#[Examples(2, 2)]
public function example3(AttrsTester $I, Example $e)
{
    $I->assertEquals($e[1], $e[0]);
}

// same in the opposite direction

I see how this change can introduce backward incompatibility for those who have used a single #[Example]. But since the documentation does not describe the specific behavior of a single #[Example], I don't think it's a big issue compared to the improved developer experience.

So I propose to update the tests\cli\RunCest.php:runWithExamples.

I'd appreciate your thoughts on this.

Annotation::forMethod($class, 'multipleAttributes')->fetchAll('example'));

$this->assertSame(
[['example 1/1']],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpcs doesn't like style of this file

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my bad. Missed PHPCS checks. Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants