programmieren:testing:unittest:table_tests
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| programmieren:testing:unittest:table_tests [2026/08/26 15:40] – created admin | programmieren:testing:unittest:table_tests [2026/08/27 10:07] (current) – [Problem with DataValidationTestTrait] Explaination admin | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| ===== Data Validation Tests ===== | ===== Data Validation Tests ===== | ||
| + | |||
| + | Data Validation Tests in our application are done with the DataValidationTestTrait. | ||
| + | |||
| + | This trait in most cases first checks the given fields validation with a positive case, where it should run throught without problem and then with a negative case where it should cause an expected validation error. | ||
| + | |||
| + | This usually leads to quite a few lines of code like this: | ||
| + | <code php> | ||
| + | public function testValidationDefault(): | ||
| + | { | ||
| + | $accounts = $this-> | ||
| + | $field = ' | ||
| + | $this-> | ||
| + | $this-> | ||
| + | $this-> | ||
| + | $this-> | ||
| + | |||
| + | $field = ' | ||
| + | $this-> | ||
| + | $this-> | ||
| + | $this-> | ||
| + | $this-> | ||
| + | |||
| + | $field = ' | ||
| + | $this-> | ||
| + | $this-> | ||
| + | $this-> | ||
| + | |||
| + | $field = ' | ||
| + | $this-> | ||
| + | $this-> | ||
| + | $this-> | ||
| + | $this-> | ||
| + | |||
| + | $field = ' | ||
| + | $this-> | ||
| + | $this-> | ||
| + | $this-> | ||
| + | $this-> | ||
| + | } | ||
| + | </ | ||
| + | |||
| ==== Problem with DataValidationTestTrait ==== | ==== Problem with DataValidationTestTrait ==== | ||
| Line 20: | Line 61: | ||
| - Keep the logic but implement it so that if the error you're looking for is contained within the list of errors it still runs through. I'm not sure how well this will work since it causes a different kind of error than validation errors. | - Keep the logic but implement it so that if the error you're looking for is contained within the list of errors it still runs through. I'm not sure how well this will work since it causes a different kind of error than validation errors. | ||
| - | **REMEMBER TO LOOK AT THIS WITH Fabian** | + | So it ended up being a much simpler mistake. We forgot to disable validation on BOTH the times it was saved. The existing logic actually allows there being multiple different errors since it searches for a specific one in the list of errors it gets. We corrected it now in the trait itsself and it works properly now. The issue that you have to give it all the required fields still exists tho since it is not a data validation error. So an implementation would now look like this: |
| + | |||
| + | <code php> | ||
| + | $this-> | ||
| + | $subscriptions, | ||
| + | ' | ||
| + | ' | ||
| + | [ | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ] | ||
| + | ); | ||
| + | </ | ||
| + | |||
| + | And the trait now looks like this: | ||
| + | |||
| + | <code php> | ||
| + | protected function testDataValidationIsUnique( | ||
| + | Table $table, | ||
| + | string $fieldName, | ||
| + | mixed $fieldValue, | ||
| + | array $additionalProperties = [], | ||
| + | ?array $expected = null, | ||
| + | ): void { | ||
| + | $prevEntity = $table-> | ||
| + | $table-> | ||
| + | $prevEntity, | ||
| + | array_merge($additionalProperties, | ||
| + | [' | ||
| + | ); | ||
| + | $table-> | ||
| + | |||
| + | $entity = $table-> | ||
| + | $table-> | ||
| + | $entity, | ||
| + | array_merge($additionalProperties, | ||
| + | [' | ||
| + | ); | ||
| + | |||
| + | $result = $table-> | ||
| + | static:: | ||
| + | |||
| + | $expected ??= [' | ||
| + | |||
| + | $this-> | ||
| + | } | ||
| + | </ | ||
programmieren/testing/unittest/table_tests.1787751628.txt.gz · Last modified: by admin
