10000 Allow single #[Examples] attribute by W0rma · Pull Request #6812 · Codeception/Codeception · GitHub
[go: up one dir, main page]

Skip to content

Allow single #[Examples] attribute #6812

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Codeception/Util/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ public function fetchAll(string $annotation): array
{
if (($attr = $this->attribute($annotation)) instanceof ReflectionAttribute) {
if (!$attr->isRepeated()) {
if ($annotation === 'example') {
return [$attr->getArguments()];
}
return $attr->getArguments();
}

Expand Down
7 changes: 7 additions & 0 deletions tests/data/SimpleWithExamplesCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ public function helloWorld(\CodeGuy $I, \Codeception\Example $example)
return count($example);
})->seeResultEquals(2);
}

#[Examples(first: true, second: false)]
public function namedArguments(\CodeGuy $I, \Codeception\Example $example)
{
$I->assertSame(true, $example['first']);
$I->assertSame(false, $example['second']);
}
}
3 changes: 2 additions & 1 deletion tests/data/claypit/tests/Attrs/BasicScenarioCest.php
Original file line number Diff line nu 10000 mber Diff line change
Expand Up @@ -39,7 +39,8 @@ private function close1(AttrsTester $I)
}

#[Group('e1')]
#[Examples([1, 1], [2, 2])]
#[Examples(1, 1)]
#[Examples(2, 2)]
Comment on lines +42 to +43
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As already mentioned in #6752 (comment) this is a slight BC break introduced by this PR.

However, the behavior doesn't seem to be documented and from DX perspective I would prefer the new behavior.

Any thoughts about this are welcome.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The first syntax allows for dereferencing from arrays using .... also what about generators? They can't use the 2nd Syntax

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The first syntax allows for dereferencing from arrays using ....

Shouldn't this still be possible by just passing an array as example, f.e. #[Examples(['foo' => 'bar'])]? Please correct me if I misunderstood the problem.

what about generators? They can't use the 2nd Syntax

Do you mean that the new syntax breaks generators? Can you provide an example?

Copy link
Member

Choose a reason for hiding this comment

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

@SamMousa Any news about this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I dont remember exactly.
But I'll say this: to me having a different behaviour depending on the type of argument given to the attribute is bad design, both from a code quality as well as a DX perspective.

I don't remember this PR (it's been a while, so feel free to ignore this comment and merge if you like it.

public function exampleTest(AttrsTester $I, Example $e)
{
$I->assertEquals($e[1], $e[0]);
Expand Down
9 changes: 8 additions & 1 deletion tests/data/data_provider/ExampleAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ class ExampleAnnotationTest extends TestCase
* @example ["foo", 5]
* @example ["bar", 6]
*/
public function testExample($arg1, $arg2): void
public function testMultipleExamples($arg1, $arg2): void
{
}

/**
* @example ["bar", 4]
*/
public function testSingleExample($arg1, $arg2): void
{
}
}
7 changes: 6 additions & 1 deletion tests/data/data_provider/ExamplesAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ class ExamplesAttributeTest extends TestCase
{
#[Examples("foo", 7)]
#[Examples("bar", 8)]
public function testExample($arg1, $arg2): void
public function testMultipleExamples($arg1, $arg2): void
{
}

#[Examples("foo", 9)]
public function testSingleExample($arg1, $arg2): void
{
}
}
34 changes: 30 additions & 4 deletions tests/unit/Codeception/Test/DataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,23 @@ public function testExecutesMultipleDataProviders()
$this->assertSame($expectedResult, $result);
}

public function testSupportsExampleAnnotations()
public function testSupportsSingleExampleAnnotation()
{
require_once codecept_data_dir('data_provider/ExampleAnnotationTest.php');
$method = new ReflectionMethod(ExampleAnnotationTest::class, 'testExample');
$method = new ReflectionMethod(ExampleAnnotationTest::class, 'testSingleExample');
$result = DataProvider::getDataForMethod($method);

$expectedResult = [
['bar', 4],
];

$this->assertSame($expectedResult, $result);
}

public function testSupportsMultipleExampleAnnotations()
{
require_once codecept_data_dir('data_provider/ExampleAnnotationTest.php');
$method = new ReflectionMethod(ExampleAnnotationTest::class, 'testMultipleExamples');
$result = DataProvider::getDataForMethod($method);

$expectedResult = [
Expand All @@ -150,10 +163,23 @@ public function testSupportsExampleAnnotations()
$this->assertSame($expectedResult, $result);
}

public function testSupportsExamplesAttribute()
public function testSupportsSingleExamplesAttribute()
{
require_once codecept_data_dir('data_provider/ExamplesAttributeTest.php');
$method = new ReflectionMethod(ExamplesAttributeTest::class, 'testSingleExample');
$result = DataProvider::getDataForMethod($method);

$expectedResult = [
['foo', 9],
];

$this->assertSame($expectedResult, $result);
}

public function testSupportsMultipleExamplesAttributes()
{
require_once codecept_data_dir('data_provider/ExamplesAttributeTest.php');
$method = new ReflectionMethod(ExamplesAttributeTest::class, 'testExample');
$method = new ReflectionMethod(ExamplesAttributeTest::class, 'testMultipleExamples');
$result = DataProvider::getDataForMethod($method);

$expectedResult = [
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Codeception/TestLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,6 @@ public function testLoadTestWithExamples()
{
$this->testLoader->loadTest('SimpleWithExamplesCest.php');
$tests = $this->testLoader->getTests();
$this->assertCount(3, $tests);
$this->assertCount(4, $tests);
}
}
0