Skip to content

Commit

Permalink
Merge pull request #4 from wp-graphql/feature/not-modifier
Browse files Browse the repository at this point in the history
"WPGraphQLTestCommon::not()" implemented and tested.
  • Loading branch information
kidunot89 committed Mar 31, 2021
2 parents 20d4fa8 + 8159d71 commit 79e48d5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 21 deletions.
86 changes: 65 additions & 21 deletions src/TestCase/WPGraphQLTestCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function toRelayId() {
* @return array
*/
public function expectedObject( string $path, $expected_value ) {
$type = 'OBJECT';
$type = $this->get_not() . 'OBJECT';
return compact( 'type', 'path', 'expected_value' );
}

Expand All @@ -64,7 +64,7 @@ public function expectedObject( string $path, $expected_value ) {
* @return array
*/
public function expectedNode( string $path, $expected_value = null, $expected_index = null ) {
$type = 'NODE';
$type = $this->get_not() . 'NODE';
return compact( 'type', 'path', 'expected_value', 'expected_index' );
}

Expand All @@ -77,10 +77,31 @@ public function expectedNode( string $path, $expected_value = null, $expected_in
* @return array
*/
public function expectedEdge( string $path, $expected_value = null, $expected_index = null ) {
$type = 'EDGE';
$type = $this->get_not() . 'EDGE';
return compact( 'type', 'path', 'expected_value', 'expected_index' );
}

/**
* Triggers the "not" flag for the next expect*() call.
*
* @return WPGraphQLTestCommon
*/
public function not() {
$this->not = '!';
return $this;
}

/**
* Clears the "not" flag and return the proper prefix.
*
* @return string
*/
private function get_not() {
$prefix = $this->not ? '!' : '';
unset( $this->not );
return $prefix;
}

/**
* Returns an expected "location" error data object.
*
Expand Down Expand Up @@ -201,8 +222,8 @@ public function assertExpectedDataFound( array $response, array $expected_data,
}

// Evaluate expected data.
switch( $type ) {
case 'OBJECT':
switch( true ) {
case $this->endsWith( 'OBJECT', $type ):
// Log assertion.
$actual_log_type = is_array( $actual_data ) ? 'ACTUAL_DATA_OBJECT' : 'ACTUAL_DATA';
$assertion_log = array(
Expand All @@ -211,18 +232,21 @@ public function assertExpectedDataFound( array $response, array $expected_data,
);
$this->logData( $assertion_log );

$assert_same = ! $this->startsWith( '!', $type );
$assertion = $assert_same ? 'assertSame' : 'assertNotSame';
$assertion_message = $assert_same ? 'doesn\'t match' : 'shouldn\'t match';
// Execute assertion.
$this->assertSame(
$this->$assertion(
$expected_value,
$actual_data,
$this->maybe_print_message(
$message,
sprintf( 'Data found at path "%s" doesn\'t match the provided value', $actual_path )
sprintf( 'Data found at path "%1$s" %2$s the provided value', $actual_path, $assertion_message )
)
);
break;
case 'NODE':
case 'EDGE':
case $this->endsWith( 'NODE', $type ):
case $this->endsWith( 'EDGE', $type ):
if ( $check_order ) {
// Log assertion.
$actual_log_type = is_array( $actual_data ) ? 'ACTUAL_DATA_OBJECT' : 'ACTUAL_DATA';
Expand All @@ -232,13 +256,16 @@ public function assertExpectedDataFound( array $response, array $expected_data,
);
$this->logData( $assertion_log );

$assert_same = ! $this->startsWith( '!', $type );
$assertion = $assert_same ? 'assertSame' : 'assertNotSame';
$assertion_message = $assert_same ? 'doesn\'t match' : 'shouldn\'t match';
// Execute assertion
$this->assertSame(
$this->$assertion(
$expected_value,
$actual_data,
$this->maybe_print_message(
$message,
sprintf( 'Data found at path "%s" doesn\'t match the provided value', $actual_path )
sprintf( 'Data found at path "%1$s" %2$s the provided value', $actual_path, $assertion_message )
)
);
break;
Expand All @@ -250,20 +277,37 @@ public function assertExpectedDataFound( array $response, array $expected_data,
);
$this->logData( $assertion_log );

foreach ( $actual_data as $actual_node ) {
$assert_same = ! $this->startsWith( '!', $type );
foreach ( $actual_data as $index => $actual_node ) {
// If match found, Assert true.
if ( $expected_value === $actual_node ) {
$this->assertTrue( true );
break 2;
if ( $assert_same ) {
$this->assertTrue( true );
break 2;
} else {
$this->assertTrue(
false,
$this->maybe_print_message(
$message,
sprintf( 'Undesired data found in %1$s list at path "%2$s.%3$d"', strtolower( $type ), $actual_path, $index )
)
);
}
}
}
$this->assertTrue(
false,
$this->maybe_print_message(
$message,
sprintf( 'Expected data not found in the %1$s list at path "%2$s"', strtolower( $type ), $actual_path )
)
);

if ( $assert_same ) {
$this->assertTrue(
false,
$this->maybe_print_message(
$message,
sprintf( 'Expected data not found in the %1$s list at path "%2$s"', strtolower( $type ), $actual_path )
)
);
}

$this->assertTrue( true );

break;
default:
throw new \Exception( 'Invalid data object provided for evaluation.' );
Expand Down
4 changes: 4 additions & 0 deletions tests/codeception/wpunit/WPGraphQLTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function testAssertQuerySuccessful() {
$this->expectedNode(
'posts.nodes',
array( 'id' => $this->toRelayId( 'post', $post_id ) )
),
$this->not()->expectedNode(
'posts.nodes',
array( 'id' => $this->toRelayId( 'post', 10001 ) )
)
);

Expand Down
4 changes: 4 additions & 0 deletions tests/phpunit/unit/test-wpgraphqlunittestcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function test_AssertQuerySuccessful() {
$this->expectedNode(
'posts.nodes',
array( 'id' => $this->toRelayId( 'post', $post_id ) )
),
$this->not()->expectedNode(
'posts.nodes',
array( 'id' => $this->toRelayId( 'post', 10001 ) )
)
);

Expand Down

0 comments on commit 79e48d5

Please sign in to comment.