• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql-testcase / #310

08 Aug 2024 04:18PM UTC coverage: 78.59% (-0.5%) from 79.076%
#310

push

web-flow
Merge c1d5c4192 into 81cab02b0

10 of 15 new or added lines in 2 files covered. (66.67%)

301 of 383 relevant lines covered (78.59%)

3.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Constraint/QueryErrorConstraint.php
1
<?php
2
/**
3
 * QueryErrorConstraint 
4
 *
5
 * Assertion validating successful WPGraphQL query response.
6
 * @since v3.0.0
7
 * @package Tests\WPGraphQL\Constraint
8
 */
9

10
namespace Tests\WPGraphQL\Constraint;
11

12
class QueryErrorConstraint extends QueryConstraint {
13

14
    /**
15
     * {@inheritDoc}
16
     */
17
    public function matches($response): bool {
18
        // Ensure response is valid.
19
        if ( ! $this->responseIsValid( $response ) ) {
6✔
20
            return false;
1✔
21
        }
22

23
        // Throw if response has errors.
24
        if ( ! array_key_exists( 'errors', $response ) ) {
5✔
25
            $this->error_message = "No errors was thrown during the previous GraphQL requested. \n Use \"--debug\" flag to see contents of previous request.";
1✔
26
            return false;
1✔
27
        }
28

29
        // Return true if no validation rules provided.
30
        if ( empty( $this->validationRules ) ) {
4✔
31
            return true;
2✔
32
        }
33

34
        // Check validation rules.
35
        $data_passed  = true;
3✔
36
        $error_passed = true;
3✔
37
        foreach( $this->validationRules as $expected_data ) {
3✔
38
            if ( empty( $expected_data['type'] ) ) {
3✔
39
                $this->logger->logData( array( 'INVALID_DATA_OBJECT' => $expected_data ) );
1✔
40
                $this->error_details[] = 'Invalid data object provided for evaluation.';
1✔
41
                $data_passed = false;
1✔
42
                $error_passed = false;
1✔
43
                continue;
1✔
44
            }
45

46
            if ( str_starts_with( $expected_data['type'], 'ERROR_' ) ) {
3✔
47
                if ( ! $this->expectedErrorFound( $response, $expected_data ) ) {
3✔
48
                    $error_passed = false;
1✔
49
                }
50
                continue;
3✔
51
            }
52

53
            if ( ! $this->expectedDataFound( $response, $expected_data ) ) {
3✔
54
                $data_passed = false;
1✔
55
            }
56
        }
57

58
        if ( ! $data_passed || ! $error_passed) {
3✔
59
            $this->error_message = 'The GraphQL response failed the following steps in validation';
1✔
60
            return false;
1✔
61
        }
62

63
        return true;
2✔
64
    }
65

66
    /**
67
     * {@inheritDoc}
68
     */
69
    public function toString(): string {
70
        return 'is a WPGraphQL query response with errors';
1✔
71
    }
72
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc