Slim Framework 端点单元测试

2022-01-25 00:00:00 php slim phpunit

我正在尝试为我的小型超薄框架应用程序编写一些 PHPUnit 测试,但在文档中看不到任何指向执行完整请求和断言响应的方法(包含文本或 200状态,或任何东西,真的).

I'm trying to write some PHPUnit tests for my small slim framework app, but don't see anywhere in the docs that point to a way to do a full request and assert on the response (either containing text or a 200 status, or anything, really).

有没有人发现/使用过的方法?

Is there any way to do this that anyone has found/used?

推荐答案

下面是如何测试 Slim 应用程序的示例:

Here is example how you may test your Slim application:

https://github.com/mac2000/SlimTestable

假设我们有一个简单的应用程序:

Suppose we have simple application:

<?php
use SlimSlim;

require_once 'vendor/autoload.php';

$app = new Slim();

$app->get('/', function(){
    echo 'home';
})->name('home');

$app->get('/hello/:name', function($name){
    echo "hello $name";
})->name('hello');

$app->map('/login', function() use($app) {
    if($app->request()->params('login')) {
        $app->flash('success', 'Successfully logged in');
        $app->redirect($app->urlFor('hello', array('name' => $app->request()->params('login'))));
    } else {
        $app->flash('error', 'Wrong login');
        $app->redirect($app->urlFor('home'));
    }
})->via('GET', 'POST');

$app->run();

我们如何测试它?

创建App类:

<?php // src/App.php
use SlimSlim;

class App extends Slim {
    function __construct(array $userSettings = array())
    {
        parent::__construct($userSettings);

        $this->get('/', function(){
            echo 'home';
        })->name('home');

        $this->get('/hello/:name', function($name){
            echo "hello $name";
        })->name('hello');

        $this->map('/login', function() {
            if($this->request()->params('login')) {
                $this->flash('success', 'Successfully logged in');
                $this->redirect($this->urlFor('hello', array('name' => $this->request()->params('login'))));
            } else {
                $this->flash('error', 'Wrong login');
                $this->redirect($this->urlFor('home'));
            }
        })->via('GET', 'POST');
    }

    /**
     * @return SlimHttpResponse
     */
    public function invoke() {
        $this->middleware[0]->call();
        $this->response()->finalize();
        return $this->response();
    }
}

请注意,我们将所有路由移动到新的类构造函数,还请注意新的 invoke 方法,它与 run 方法相同,只是它返回响应而不是回显它出去.

Notice that we move all our routes to new class constructor, also notice new invoke method, which do the same as run method except it returns response rather than echoing it out.

现在你的 index.php 文件可能是这样的:

Now your index.php file might be like this one:

<?php
require_once 'vendor/autoload.php';

$app = new App();
$app->run();

现在是测试的时候了:

<?php // tests/ExampleTest.php
use SlimEnvironment;

class ExampleTest extends PHPUnit_Framework_TestCase {
    private $app;

    public function setUp()
    {
        $_SESSION = array();
        $this->app = new App();
    }

    public function testHome() {
        Environment::mock(array(
            'PATH_INFO' => '/'
        ));
        $response = $this->app->invoke();

        $this->assertContains('home', $response->getBody());
    }

    public function testHello() {
        Environment::mock(array(
            'PATH_INFO' => '/hello/world'
        ));
        $response = $this->app->invoke();

        $this->assertTrue($response->isOk());
        $this->assertContains('hello world', $response->getBody());
    }

    public function testNotFound() {
        Environment::mock(array(
            'PATH_INFO' => '/not-exists'
        ));
        $response = $this->app->invoke();

        $this->assertTrue($response->isNotFound());
    }

    public function testLogin() {
        Environment::mock(array(
            'PATH_INFO' => '/login'
        ));
        $response = $this->app->invoke();

        $this->assertTrue($response->isRedirect());
        $this->assertEquals('Wrong login', $_SESSION['slim.flash']['error']);
        $this->assertEquals('/', $response->headers()->get('Location'));
    }

    public function testPostLogin() {
        Environment::mock(array(
            'REQUEST_METHOD' => 'POST',
            'PATH_INFO' => '/login',
            'slim.input' => 'login=world'
        ));
        $response = $this->app->invoke();

        $this->assertTrue($response->isRedirect());
        $this->assertEquals('Successfully logged in', $_SESSION['slim.flash']['success']);
        $this->assertEquals('/hello/world', $response->headers()->get('Location'));
    }

    public function testGetLogin() {
        Environment::mock(array(
            'PATH_INFO' => '/login',
            'QUERY_STRING' => 'login=world'
        ));
        $response = $this->app->invoke();

        $this->assertTrue($response->isRedirect());
        $this->assertEquals('Successfully logged in', $_SESSION['slim.flash']['success']);
        $this->assertEquals('/hello/world', $response->headers()->get('Location'));
    }
}

你应该注意几件事:

在设置测试时,我们正在创建 $_SESSION 数组用于测试目的并实例化我们的 App 类对象.

While setting up test we are creating $_SESSION array for test purposes and instantiate our App class object.

在测试中而不是 run 我们调用 invoke 执行相同的操作,但返回响应对象.

In tests rather than run we are calling invoke which do the same, but returns response object.

Environment::mock 用于模拟我们的应用程序处理的请求.

Environment::mock used to mock requests which are processed with our application.

相关文章