Skip to content

Instantly share code, notes, and snippets.

@Rokt33r
Last active August 29, 2015 14:20
Show Gist options
  • Save Rokt33r/069c4842f0808920c08c to your computer and use it in GitHub Desktop.
Save Rokt33r/069c4842f0808920c08c to your computer and use it in GitHub Desktop.
REST Test helper for Laravel5

Test Helper

Simple test helper trait for testing Laravel5 REST API app.

Using packages

  • Laracasts/TestDummy for Intergration test with DB
  • Flow/JSONPath for validating JSON

How to use

Install the packages

	"laracasts/testdummy": "~2.0",
	"flow/jsonpath": "0.2.*"

Put TestHelpersTrait.php to APP_PATH/tests directory.

<?php

use Illuminate\Http\Request;
use Laracasts\TestDummy\Factory as TestDummy;

trait TestHelpersTrait{

    /**
     * Call with an access token
     * 
     * @param $accessToken
     * @param $method
     * @param $uri
     * @param array $parameters
     * @param array $cookies
     * @param array $files
     * @param array $server
     * @param null $content
     * @return \Illuminate\Http\Response
     */
    public function callWithAccessToken($accessToken, $uri, $method, $parameters = [], $cookies = [], $files = [], $server = [], $content = null){
        $server['HTTP_Authorization'] = 'Bearer ' . $accessToken;

        return $this->call($uri, $method, $parameters, $cookies, $files, $server, $content);
    }

    /**
     * Call with JWT user authentication token
     * 
     * @param \App\User $user
     * @param ...$params
     * @return \Illuminate\Http\Response
     */
    public function authCall(\App\User $user, $uri, $method, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
    {
        $accessToken = JWTAuth::fromUser($user);

        return $this->callWithAccessToken($accessToken, $uri, $method, $parameters, $cookies, $files, $server, $content);
    }

    /**
     * Grab JSON
     * @param $json
     * @param $jsonPath
     * @return \Flow\JSONPath\JSONPath
     */
    public function grabJson($json, $jsonPath)
    {
        if(is_string($json)) $json = json_decode($json, true);
        return (new \Flow\JSONPath\JSONPath($json))->find($jsonPath);
    }

    /**
     * Check JSON with JSONPath
     * @param $json
     * @param $jsonPath
     * @return bool
     */
    public function matchJson($json, $jsonPath)
    {
        if(is_string($json)) $json = json_decode($json, true);
        return $this->assertTrue(count((new \Flow\JSONPath\JSONPath($json))->find($jsonPath)->data()) > 0);
    }

    /**
     * @param $model
     * @param array $overrides
     * @return mixed
     */
    public function hasA($model, $overrides = [])
    {
        return TestDummy::create($model, $overrides);
    }

    /**
     * @param $times
     * @param $model
     * @param array $overrides
     * @return mixed
     */
    public function hasMany($times, $model, $overrides = [])
    {
        return TestDummy::times($times)->create($model, $overrides);
    }
}

Modify classmap("tests/TestCase.php""tests") in composer.json

	"autoload-dev": {
		"classmap": [
			"tests"
		]
	},

execute $ composer dumpautoload or $ php artisan optimize on terminal to load the trait

Apply TestHelpersTrait.php to TestCase.php

<?php

class TestCase extends Illuminate\Foundation\Testing\TestCase {

	use TestHelpersTrait;
	
.......
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment