Skip to content
This repository has been archived by the owner on Jan 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #3 from wp-graphql/feature/unit-test-setup
Browse files Browse the repository at this point in the history
Adding Unit Tests
  • Loading branch information
jasonbahl committed Sep 26, 2017
2 parents b309d60 + 0d31aba commit 6dab15c
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 49 deletions.
42 changes: 17 additions & 25 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,26 @@ matrix:
fast_finish: true

install:
# Install coveralls.phar
- wget -c -nc --retry-connrefused --tries=0 https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar
- chmod +x coveralls.phar
- php coveralls.phar --version

before_script:
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- |
if [[ ! -z "$WP_VERSION" ]] ; then
bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
if [[ ${TRAVIS_PHP_VERSION:0:2} == "5." ]]; then
composer global require "phpunit/phpunit=4.8.*"
else
composer global require "phpunit/phpunit=5.7.*"
fi
fi
- |
if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then
composer global require wp-coding-standards/wpcs
phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs
fi
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
- composer self-update
- composer require phpunit/phpunit:$PHP_UNIT_VERSION
- composer install --no-interaction
- mkdir -p build/logs
- ls -al

script:
- |
if [[ ! -z "$WP_VERSION" ]] ; then
phpunit --coverage-clover build/logs/clover.xml
WP_MULTISITE=1 phpunit
fi
- |
if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then
phpcs --standard=phpcs.ruleset.xml $(find . -name '*.php')
fi
- phpunit --coverage-clover build/logs/clover.xml

after_success:
- travis_retry php coveralls.phar -v

cache:
directories:
- vendor
- $HOME/.composer/cac
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# WPGraphQL Insights

This adds tracing to WPGraphQL, per this spec: https://github.com/apollographql/apollo-tracing
[![Build Status](https://travis-ci.org/wp-graphql/wp-graphql-insights.svg?branch=master)](https://travis-ci.org/wp-graphql/wp-graphql-insights)
[![Coverage Status](https://coveralls.io/repos/github/wp-graphql/wp-graphql-insights/badge.svg?branch=master)](https://coveralls.io/github/wp-graphql/wp-graphql-insights?branch=master)

This adds tracing to WPGraphQL, per the proposed Apollo Tracing Spec: https://github.com/apollographql/apollo-tracing


6 changes: 2 additions & 4 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,8 @@ install_db() {
}

install_wpgraphql() {
if [ -d $WP_CORE_DIR ]; then
return;
fi
git clone git@github.com:wp-graphql/wp-graphql.git $WP_CORE_DIR/wp-content/plugins/wp-graphql
echo "Cloning WPGraphQL"
git clone https://github.com/wp-graphql/wp-graphql.git $WP_CORE_DIR/wp-content/plugins/wp-graphql
}

install_wp
Expand Down
8 changes: 8 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
</phpunit>
43 changes: 28 additions & 15 deletions src/Tracing.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/
class Tracing {

/**
* Stores whether tracing should be included in the response for GraphQL Requests
*/
public static $include_in_response = true;

/**
* Stores whether tracing is enabled or not
*
Expand Down Expand Up @@ -136,11 +141,6 @@ public static function init_trace( $request, $operation_name, $variables ) {
Data::$operation_name = $operation_name;
Data::$variables = $variables;

// Don't store trace data for default IntrospectionQuery
if ( strpos( $request, 'IntrospectionQuery' ) ) {
self::$store_data = false;
}

self::$request_start_microtime = microtime( true );
self::$request_start_timestamp = self::_format_timestamp( self::$request_start_microtime );
}
Expand Down Expand Up @@ -186,10 +186,7 @@ public static function get_resolver_duration( $resolver_start ) {
* @param $trace
*/
public static function trace_resolver( $trace ) {
if ( empty( $trace ) || ! is_array( $trace ) ) {
return;
}
self::$sanitized_resolver_traces[] = self::_sanitize_resolver_trace( $trace );
self::$sanitized_resolver_traces[] = ( ! empty( $trace ) || is_array( $trace ) ) ? self::_sanitize_resolver_trace( $trace ) : [];
}

/**
Expand Down Expand Up @@ -257,23 +254,39 @@ public static function get_trace() {
/**
* This adds the "tracing" to the GraphQL response extensions.
*
* @param $results
* @param $response
*
* @return mixed
* @return \GraphQL\Executor\ExecutionResult
*/
public static function add_tracing_to_response_extensions( $results, $schema, $operation_name, $request, $variables ) {
public static function add_tracing_to_response_extensions( $response, $schema, $operation_name, $request, $variables ) {

/**
* Filter whether the tracing should be included in the response or not.
*
* @param bool $include_in_response
* @param object $response
* @param object $schema
* @param string $operation_name
* @param string $request
* @param array $variables
*/
$include_in_response = apply_filters( 'graphql_tracing_include_in_response', self::$include_in_response, $response, $schema, $operation_name, $request, $variables );

if ( true !== $include_in_response ) {
return $response;
}

/**
* If tracing should be included in the response
*/
if ( true === self::include_tracing_in_response( $results, $schema, $operation_name, $request, $variables ) ) {
$results->extensions['tracing'] = self::get_trace();
if ( true === self::include_tracing_in_response( $response, $schema, $operation_name, $request, $variables ) ) {
$response->extensions['tracing'] = self::get_trace();
}

/**
* Return the GraphQL Results, with or without the tracing extension added
*/
return $results;
return $response;
}

}
9 changes: 7 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
$_tests_dir = '/tmp/wordpress-tests-lib';
$_tests_dir = '/tmp/wp-graphql-insights/wordpress-tests-lib';
}

// Give access to tests_add_filter() function.
Expand All @@ -17,8 +17,13 @@
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
$_wp_content_dir = getenv( 'WP_CORE_DIR' );
if ( ! $_wp_content_dir ) {
$_wp_content_dir = '/tmp/wp-graphql-insights/wordpress/wp-content';
}

// Require WPGraphQL and WPGraphQL Insights
require_once( dirname( dirname( dirname( __FILE__ ) ) ) . '/wp-graphql/wp-graphql.php' );
require_once $_wp_content_dir . '/plugins/wp-graphql/wp-graphql.php';
require_once( dirname( dirname( __FILE__ ) ) . '/wp-graphql-insights.php' );
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
Expand Down
4 changes: 2 additions & 2 deletions tests/test-Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class TestData extends WP_UnitTestCase {

function testGetDocument() {
\WPGraphQL\Extensions\Insights\Data::$document = 'Test';
$actual = \WPGraphQL\Extensions\Insights\Data::$document;
$actual = \WPGraphQL\Extensions\Insights\Data::get_document();
$this->assertEquals( 'Test', $actual );
}

function testGetOperationName() {
\WPGraphQL\Extensions\Insights\Data::$operation_name = 'Test';
$actual = \WPGraphQL\Extensions\Insights\Data::$operation_name;
$actual = \WPGraphQL\Extensions\Insights\Data::get_operation_name();
$this->assertEquals( 'Test', $actual );
}

Expand Down
35 changes: 35 additions & 0 deletions tests/test-Tracing.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public static function _set_request_end_microtime( $time ) {
self::$request_end_microtime = $time;
self::$request_end_timestamp = self::_format_timestamp( self::$request_end_microtime );
}
public static function _get_sanitized_resolver_traces() {
return self::$sanitized_resolver_traces;
}
}

class TestTracing extends WP_UnitTestCase {
Expand Down Expand Up @@ -87,4 +90,36 @@ public function testGetRequestEndTimestamp() {
$this->assertEquals( $expected, $actual );
}

public function testAddTracingToResponseExtensions() {

\WPGraphQL\Extensions\Insights\Tracing::$include_in_response = true;

$response = new \GraphQL\Executor\ExecutionResult();
$this->assertArrayNotHasKey( 'extensions', $response->toArray() );

$traced_response = \WPGraphQL\Extensions\Insights\Tracing::add_tracing_to_response_extensions( $response, '', '', '', '' )->toArray();
$this->assertArrayHasKey( 'tracing', $traced_response['extensions'] );

}

public function testAddTracingToResponseExtensionsDisabled() {

\WPGraphQL\Extensions\Insights\Tracing::$include_in_response = false;

$response = new \GraphQL\Executor\ExecutionResult();
$this->assertArrayNotHasKey( 'extensions', $response->toArray() );

$traced_response = \WPGraphQL\Extensions\Insights\Tracing::add_tracing_to_response_extensions( $response, '', '', '', '' )->toArray();
$this->assertArrayNotHasKey( 'extensions', $traced_response );

}

public function testTraceResolverWithInvalidTrace() {
$trace = '';
\WPGraphQL\Extensions\Insights\Tracing::trace_resolver( $trace );
$sanitized_trace = _Tracing::_get_sanitized_resolver_traces();
$this->assertEmpty( $sanitized_trace[0] );

}

}
39 changes: 39 additions & 0 deletions tests/test-WPGraphQL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

class Test_WPGraphQL_Integration extends WP_UnitTestCase {

/**
* Let's ensure that Tracing is output as expected in the extensions of the GraphQL Response
*/
function testGraphQLQueryTracingInResponse() {
\WPGraphQL\Extensions\Insights\Tracing::$include_in_response = true;
$query = '{posts{edges{node{id}}}}';
$results = do_graphql_request( $query );
$this->assertArrayNotHasKey( 'errors', $results );
$this->assertArrayHasKey( 'tracing', $results['extensions'] );
}

function testGraphQLQueryTracingNotInResponseWhenDisabled() {

// Disable including tracing in the response
\WPGraphQL\Extensions\Insights\Tracing::$include_in_response = false;

// Run a query
$query = '{posts{edges{node{id}}}}';
$results = do_graphql_request( $query );

// Make sure the query didn't respond with any errors
$this->assertArrayNotHasKey( 'errors', $results );

// There's a chance there might be other extensions at some point, so let's not be
// too strict on the assertion here. We want to make sure that if tracing is disabled that
// either no extensions are present in the results OR tracing _at least_ is not present
if ( ! empty( $results['extensions'] ) ) {
$this->assertArrayNotHasKey( 'tracing', $results['extensions'] );
} else {
$this->assertArrayNotHasKey( 'extensions', $results );
}

}

}
13 changes: 13 additions & 0 deletions tests/test-wp-graphql-insights.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

class Test_WPGraphQLInsights extends WP_UnitTestCase {

/**
* Ensure that the function that instantiates the plugin properly returns the class instance
*/
function testGraphQLInsightsInit() {
$actual = \WPGraphQL\Extensions\graphql_insights_init();
$this->assertEquals( \WPGraphQL\Extensions\Insights::instance(), $actual );
}

}

0 comments on commit 6dab15c

Please sign in to comment.