jwest/slim-testing

此包的最新版本(1.0.0)没有提供许可信息。

用于slim框架集成测试的简单工具

1.0.0 2013-07-16 21:17 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:18:33 UTC


README

#Slim框架的集成测试

如果你的应用程序是用slim框架编写的,并且正在进行集成测试,你必须使用此包 ;)

##它是如何工作的?

  1. 为你的测试命名,后缀为*ItTest.php(以提高可读性),

  2. 测试类继承自Slim\Test\Testing,

  3. 类中的字段$app显示了你的应用程序启动的位置(public/index.php),

  4. 使用五个魔术方法来创建和向你的应用程序发送请求

    • get($route) - 通过GET发送请求
    • delete($route) - 通过DELETE发送请求
    • post($route, $params) - 通过POST发送请求,参数以http params格式编码
    • postJson($route, $params) - 通过POST发送请求,请求体为json格式
    • put($route, $params) - 通过PUT发送请求,参数以http params格式编码
    • putJson($route, $params) - 通过PUT发送请求,请求体为json格式
  5. 在路由中使用查询字符串。

示例:tests/Slim/Test/TestingItTest.php

<?php

use Slim\Test\Testing;

class TestingTest extends Testing {

    public $app = 'tests/testApp.php';

    public function testIndex() {
        $this->assertEquals('test', $this->get('/')->getBody());
    }

    public function testNotExistsPage() {
        $response = $this->get('/notExistsPage');
        $this->assertContains('404 Page Not Found', $response->getBody());
        $this->assertEquals(404, $response->getStatus());
    }

    public function testDeleteProduct() {
        $this->assertEquals('ok', $this->delete('/product')->getBody());
    }

    public function testDrawApi() {
        $response = $this->post('/api/draw', array('key' => 'value'));
        $this->assertEquals('value', json_decode($response->getBody())->code);
    }

    public function testDrawApiWithSendJSON() {
        $response = $this->postJson('/api/draw.json', array('key' => 'value'));
        $this->assertEquals('value', json_decode($response->getBody())->code);
    }

    public function testPutNewOrder() {
        $response = $this->put('/api/order?force=true', 'orderValue');

        $this->assertEquals(
            (object) array(
                'force' => 'true',
                'order' => (object) array('orderKey' => 'orderValue'),
            ),
            json_decode($response->getBody())
        );
    }

    public function testPutNewOrderWithSendJSON() {
        $response = $this->putJson('/api/order.json?force=true', array('orderKey' => 'orderValue'));

        $this->assertEquals(
            (object) array(
                'force' => 'true',
                'order' => (object) array('orderKey' => 'orderValue'),
            ),
            json_decode($response->getBody())
        );
    }
}