Fix incorrect Annotation::fetchAll() return structure for single Examples attribute by xEdelweiss · Pull Request #6752 · Codeception/Codeception · GitHub
Skip to content

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

Closed
xEdelweiss wants to merge 3 commits into
Codeception:5.1from
xEdelweiss:5.1-fix-single-example-attr
Closed

Fix incorrect Annotation::fetchAll() return structure for single Examples attribute#6752
xEdelweiss wants to merge 3 commits into
Codeception:5.1from
xEdelweiss:5.1-fix-single-example-attr

Conversation

@xEdelweiss

Copy link
Copy Markdown

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.

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@SamMousa SamMousa deleted the branch Codeception:5.1 September 13, 2024 09:27
@SamMousa SamMousa closed this Sep 13, 2024
@SamMousa

Copy link
Copy Markdown
Collaborator

This PR was auto closed because I deleted the 5.1 branch.
Please redirect it to the main branch if you're still interested in getting this merged.
Regardless of your decision, thanks for your work!

@W0rma

W0rma commented Dec 5, 2024

Copy link
Copy Markdown
Contributor

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.

4 participants