Group events fire twice #3112

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

Comments

Projects
None yet
2 participants
@jstaudenmaier
Contributor

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 from Group events firing twice to Group events fire twice May 19, 2016

@jstaudenmaier

This comment has been minimized.

Show comment
Hide comment
@jstaudenmaier

jstaudenmaier Jun 9, 2016

Contributor

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.

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@Naktibalda

Naktibalda Jun 11, 2016

Member

Could you make a pull request?

Member

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

This comment has been minimized.

Show comment
Hide comment
@jstaudenmaier

jstaudenmaier Jun 14, 2016

Contributor

I could! :-)

#3221

Contributor

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. (#3221)
* 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