User Tools

Site Tools


programmieren:testing:unittest:table_tests

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
programmieren:testing:unittest:table_tests [2026/08/26 15:54] – Explanation to Data Validation Tests adminprogrammieren:testing:unittest:table_tests [2026/08/27 10:07] (current) – [Problem with DataValidationTestTrait] Explaination admin
Line 61: 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->testDataValidationIsUnique( 
 +    $subscriptions, 
 +    'uuid', 
 +    '12345681-4ad6-43e7-842c-aeb392a0f862', 
 +    [ 
 +        'account_id' => '1', 
 +        'resource' => Resource::MEM->value, 
 +        'amount' => 17179869184, 
 +        'price' => 150.50, 
 +        'status' => SubscriptionStatuses::ACTIVE->value, 
 +        'auto_renew' => 1, 
 +        'free_tier' => 0, 
 +        'start_time' => '2025-11-07 15:46:40', 
 +        'end_time' => '2026-11-07 15:46:40', 
 +        'period' => '365 days, 1:28:20.246798', 
 +    ] 
 +); 
 +</code> 
 + 
 +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->newEmptyEntity(); 
 +    $table->patchEntity( 
 +        $prevEntity, 
 +        array_merge($additionalProperties, [$fieldName => $fieldValue]), 
 +        ['validate' => false], // <--- We noticed this before 
 +    ); 
 +    $table->saveOrFail($prevEntity); 
 + 
 +    $entity = $table->newEmptyEntity(); 
 +    $table->patchEntity( 
 +        $entity, 
 +        array_merge($additionalProperties, [$fieldName => $fieldValue]), 
 +        ['validate' => false], // <--- BUT WE MISSED THIS 
 +    ); 
 + 
 +    $result = $table->checkRules($entity); 
 +    static::assertFalse($result); 
 + 
 +    $expected ??= ['_isUnique' => 'This value is already in use']; 
 + 
 +    $this->assertDataValidationErrorsContain($fieldName, $entity->getError($fieldName), $expected); 
 +} 
 +</code>
programmieren/testing/unittest/table_tests.1787752443.txt.gz · Last modified: by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki