在 [slim] 中伪造 Request 来进行你的 HTTP 测试吧

2019-12-04 17:26:19 +08:00
 DavidNineRoc

构建好测试文件

    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    }
```	
* 根目录新建配置文件`phpunit.xml`内容如下

<phpunit bootstrap="tests/bootstrap.php"> <testsuites> <testsuite name="Feature"> <directory suffix="Test.php">tests</directory> </testsuite> </testsuites> </phpunit> ``` * `tests/bootstrap.php`文件内容如下
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

$app = AppFactory::create();

$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");
    return $response;
});

$app->get('/api/v1/users', function (Request $request, Response $response) {
    
    $data = [['name' => 'Bob', 'age' => 40]];
    $payload = json_encode($data);
    
    $response->getBody()->write($payload);
    return $response
        ->withHeader('Content-Type', 'application/json');
});

// 这里不要运行 app
// $app->run();
// 并且声明一个函数得到 App 对象
function getApplication()
{
    global $app;
    
    return $app;
}
<?php

namespace Tests;
use Nyholm\Psr7\Uri;
use PHPUnit\Framework\TestCase;
use Slim\Factory\ServerRequestCreatorFactory;

class HomeTest extends TestCase
{
    public function testHello()
    {
        $name = 'Bob';
        
        $serverRequestCreator = ServerRequestCreatorFactory::create();
        $request = $serverRequestCreator->createServerRequestFromGlobals();
    
        $uri = new Uri();
        $request = $request->withUri($uri->withPath("/hello/{$name}"));

        $response = getApplication()->handle($request);
        $responseContent = (string)$response->getBody();
    
        $this->assertEquals($responseContent, "Hello, {$name}");
    }
    
    public function testJsonApi()
    {
        $serverRequestCreator = ServerRequestCreatorFactory::create();
        $request = $serverRequestCreator->createServerRequestFromGlobals();
        
        // 因为 Uri 和 Request 对象都是不可以修改的,所以都需要新建
        $uri = new Uri();
        $request = $request->withUri($uri->withPath('api/v1/users'));
        // 如果需要伪造查询参数可以这样子做
        // $request = $request->withQueryParams([]);
        // 使用全局函数拿到 App, 传入伪造的 Request,得到处理之后的 Response
        $response = getApplication()->handle($request);
        
        // 需要用 (string) 强转,不要直接 $response->getBody()->getContents()
        // 区别就是强转,在实现类把读取指针重置到了第一位,防止得不到完整的内容
        $responseContent = (string)$response->getBody();
        
        $this->assertJson($responseContent);
    }
}
$ phpunit
PHPUnit 7.5.17 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 45 ms, Memory: 4.00 MB

OK (2 tests, 2 assertions)

原文链接 http://www.shiguopeng.cn/archives/431

1872 次点击
所在节点    分享发现
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/625955

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX