dazz/oauth2-server-httpfoundation-webtest

为 bshaffer/oauth2-server-httpfoundation-bridge 和 silex 提供的 oauth2-server-php 的 web 测试桥梁

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

This package is not auto-updated.

Last update: 2024-09-23 11:32:42 UTC


README

使用 bshaffer/oauth2-server-httpfoundation-bridge 为 oauth2-server-php 提供的 web 测试桥梁。

Silex, Symfony 及其他依赖于 symfony/http-kernel 的框架提供 WebTestCase 类来测试控制器/动作。为了编写受 bshaffer/oauth2-server-php 保护的动作的 web 测试,请求实例需要实现 \OAuth2_RequestInterface。

为了获得有效的请求对象,WebTestCase 类需要扩展这里提供的 WebTestCase 类。

安装

添加到 composer.json

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

运行 composer.phar update --dev 以安装开发依赖。

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());
    }
}

链接