Skip to content

Conversation

@carlos-granados
Copy link
Contributor

@carlos-granados carlos-granados commented Jan 24, 2025

Adds a new type of transformer that is able to transform a column in any table. For example you might have this scenario:

scenario: Check user age
  Then the users ages should be:
    | user   |  age  |
    | john   |  21   |
    | peter  |  34   |

And this transformer:

#[Transforrm('column:user')]
public function transformUsernameToUser(string $username): User
{
    return $this->getUserByUsername($username);
}

Then this transformer will be called to transform any data in the column named user

The final result which is passed to the step that uses this transformer is an array of rows where the data for the column user will be the transformed user. So, for example the step could be implemented like this:

#[Then('the users ages should be:')]
public function checkUsersAges(array $table): void
{
    foreach ($table as $row) {
        Assert::assertSame($row['user']->getAge(), $row['age']);
    }
} 

This transformation can be applied to different tables as long as the column name matches.

The transformation can be applied to more than one column if you list more than one column name in the Transform definition, for example:

#[Transforrm('column:user,other user')]

More than one transformation can be applied to different columns of a table, so in the example above if we had another transformation defined for the age column it would also be applied. The end result is again an array where in each row any column that matched any of the transformations has been replaced with its transformed value

Before implementing this new feature I modified the existing tests for the transformations to adopt the new format where the tests are defined using real files in the fixtures folder. This has been done in a separate previous commit to make it easier to review

With this feature we can get the functionality described in #1229

Once this is merged, I will update the documentation for this transformer (and also other transformers like the row transformer which are not documented)

@carlos-granados carlos-granados force-pushed the column-transformer branch 2 times, most recently from 0e9aaee to 6b23cde Compare January 31, 2025 14:44
@carlos-granados
Copy link
Contributor Author

Rebased this branch to include the new tests for the transformations with unicode characters and modified the new column transformer to also accept unicode characters

Copy link
Contributor

@acoulton acoulton left a comment

Choose a reason for hiding this comment

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

Another super-helpful feature, thanks @carlos-granados! Just a few suggestions / comments from me :)

@carlos-granados
Copy link
Contributor Author

@acoulton I rebased this code and added two commits:

  • One with coding standard changes detected by our new linter
  • Another with changes following your suggestions

@acoulton
Copy link
Contributor

acoulton commented Feb 7, 2025

@carlos-granados I think maybe the commit with your followup to code review suggestions hasn't been pushed? As there are a few things you've 👍 but I don't see changed in the diff. Thanks :)

@carlos-granados
Copy link
Contributor Author

@carlos-granados I think maybe the commit with your followup to code review suggestions hasn't been pushed? As there are a few things you've 👍 but I don't see changed in the diff. Thanks :)

Argh, yes forgot to push, done!

Copy link
Contributor

@acoulton acoulton left a comment

Choose a reason for hiding this comment

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

Great work, thanks @carlos-granados :)

@acoulton acoulton changed the title Column transformer feature: Support transforming named column(s) in any table Feb 13, 2025
@acoulton acoulton merged commit 81781cb into Behat:master Feb 13, 2025
18 checks passed
@carlos-granados carlos-granados deleted the column-transformer branch February 13, 2025 14:33
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.

2 participants