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

[EasyTest] Created InvalidDataMaker #471

Merged
merged 9 commits into from
Jan 26, 2021

Conversation

skrut
Copy link
Contributor

@skrut skrut commented Jan 22, 2021

Introducing a new util class helpful in functional tests of ApiResource validation.

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets ...

InvalidDataMaker can be extended in an application to add new yield methods.

When custom validation messages are used in an application, a translation file path should be added either by overriding the InvalidDataMaker::$translations property or by calling the InvalidDataMaker::addTranslations() method from a setUp method.

All the yield methods generate an array with the following items: data (an array containing invalid data that can be passed to an API endpoint), message, and propertyPath (a translated message and a property path that can be used to assert the violation details in the error response).

Usage examples:

// The syntax used in most cases
yield from InvalidDataMaker::make('propertyName')->yieldBlankString();

// In case a path differs from a property name
yield from InvalidDataMaker::make('propertyName')
    ->propertyPath('some.specific.path.different.from.propertyName')
    ->yieldIntegerGreaterThanGiven(100);

// Useful when we apply a constraint with a custom validation message
yield from InvalidDataMaker::make('propertyName')
    ->message('Some custom message used instead of the one from the Symfony validator.')
    ->yieldBlankString();

// This is the only method for now, which generates a fixture for a custom constraint/validation
// (e.g. when a regex is used). So, we have to pass a custom message
yield from InvalidDataMaker::make('propertyName')
    ->message('Here we use a custom regex validator, so have to set a message.')
    ->yieldInvalidFloat();

// Generating an "object" with a nested property
yield from InvalidDataMaker::make('propertyName')
    ->wrapWith('nestedObjectName')
    ->message('Just to demonstrate chaining.')
    ->yieldBlankString();

// Generating an integer casted to a string. Can be useful for amounts stored as strings
yield from InvalidDataMaker::make('propertyName')
    ->asString()
    ->yieldIntegerGreaterThanGiven(1);

// Generating an array with a blank string inside
yield from InvalidDataMaker::make('propertyName')
    ->asArrayElement()
    ->yieldBlankString();

@skrut skrut requested a review from roman-eonx January 22, 2021 09:04
@natepage natepage merged commit ca7f369 into master Jan 26, 2021
@natepage natepage deleted the feature/PLU-2760-create-invalid-data-generator branch January 26, 2021 22:35
natepage pushed a commit that referenced this pull request Mar 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants