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

Group events fire twice #3112

Open
jstaudenmaier opened this issue May 18, 2016 · 3 comments
Open

Group events fire twice #3112

jstaudenmaier opened this issue May 18, 2016 · 3 comments

Comments

@jstaudenmaier
Copy link
Contributor

@jstaudenmaier jstaudenmaier commented May 18, 2016

My Use Case

Using groups to add data to the Db for certain tests (ex: Not all tests need to have two companies so only add the second company for the group of tests that need it)

Created the Group, added it to the codeception.yml file, added the appropriate @group annotation to the tests in question.

Problem:
The _before event of the group is firing twice, leading to SQL Integrity constraint errors because duplicate data is getting entered.

Root Cause Analysis

The test.before.groupname event is getting fired twice because the group is listed twice in the Scenario->groups[] array.

The group gets added once to the Scenario during the Cest preload() process in the Parser::attachMetadata() function.

Then it gets added again in the SuiteManager::addToSuite() function (which, incidentally is the same function that calls preload())

There are no guards on the $groups[] array in the scenario to prevent the same group from being added twice so the group ends up in the array twice and when Listener::fire() iterates through the Scenario's groups, it get two items for the group and therefore fires the group's events twice.

Suggested Solution

Rather than re-engineering the system to avoid double-adds, or adding complicated logic to avoid adding the same group to Scenario->$groups[] twice, a simple solution is to wrap the return of Scenario->getGroups() to only return unique values, like so:

public function getGroups() { return array_unique($this->groups); }

@jstaudenmaier jstaudenmaier changed the title Group events firing twice Group events fire twice May 19, 2016
@jstaudenmaier
Copy link
Contributor Author

@jstaudenmaier jstaudenmaier commented Jun 9, 2016

Just in case you were wondering, this still occurs in Codeception 2.2. It's just migrated from the Scenario object to the Test\Metadata object.

@Naktibalda
Copy link
Member

@Naktibalda Naktibalda commented Jun 11, 2016

Could you make a pull request?

jstaudenmaier added a commit to IDEXX/Codeception that referenced this issue Jun 14, 2016
@jstaudenmaier
Copy link
Contributor Author

@jstaudenmaier jstaudenmaier commented Jun 14, 2016

I could! :-)

#3221

DavertMik added a commit that referenced this issue Jun 14, 2016
* Fix for issue #3112 Group events fire twice.

* Updated Dummy Test count.

* Updated Dummy Test count.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.