dazz/oauth2-server-httpfoundation-webtest

A bridge for webtesting oauth2-server-php with bshaffer/oauth2-server-httpfoundation-bridge and silex

dev-master 2013-05-06 12:42 UTC

This package is not auto-updated.

Last update: 2024-04-08 09:17:57 UTC


README

A webtesting bridge for oauth2-server-php with bshaffer/oauth2-server-httpfoundation-bridge.

Silex, Symfony and other frameworks depending on symfony/http-kernel offer a WebTestCase class for testing controller/actions. In order to write webtests for actions that are secured by the bshaffer/oauth2-server-php the request instance needs to implement \OAuth2_RequestInterface.

To achieve a valid request object the WebTestCase classes need to extend the WebTestCase class provided here.

Install

add to composer.json

{
  "minimum-stability": "dev",
  "require-dev": {
    "dazz/oauth2-server-httpfoundation-webtest":"dev-master"
  }
}

Run composer.phar update --dev to install the development dependencies.

Example for Silex

namespace Company\Test\SomeBundle\Controller

use OAuth2\HttpFoundationWebTest\Silex\WebTestCase;

class UserControllerTest extends WebTestCase
{
    public function testGetUser()
    {
        // create user, oauth2-client and access_token in the test-storage
        $accessToken = 'abc';
        // this creates the browsing client, not to mix up with the oauth2-client
        $client = $this->createClient();

        $client->request('GET', '/user/', array('access_token' => $accessToken));

        $this->assertTrue($client->getResponse()->isSuccessful());
    }

    public function testProjects()
    {
        $accessToken = 'abc';
        $client = $this->createClient();

        $content = json_encode(array(
            'name' => 'My Project',
        ));

        // the content of $server['HTTP_AUTHORIZATION'] will be copied to the header
        $server = array(
            'HTTP_AUTHORIZATION'=> sprintf('Bearer %s', $accessToken),
        );

        $client->request('PUT', '/projects/theId', array(), array(), $server, $content);

        // all sorts of assertions on response string
        $this->assertTrue($client->getResponse()->isSuccessful());
    }
}

Links