如何单元测试Slim框架应用程序

时间:2014-10-26 07:00:43

标签: php unit-testing phpunit slim

我一直试图对其他人代码的示例进行单元测试,并且每次我达到我的测试运行没有错误时 - 我只是在我期望它们通过时得到相同的失败。网上没有大量的文档,我也不知道还有什么可以用的。任何人都可以看到我的代码中哪些地方出错:

bootstrap.php(phpunit bootstrap文件)

这基本上只是$ app对象的容器。我使用相同的文件启动$ app对象,我使用(routes,config)启动我的实际应用程序。

<?php
/**
 * This makes our life easier when dealing with paths. Everything is relative
 * to the application root now.
 */
chdir(dirname('../'));

// require composer autoloader for loading classes
require 'vendor/autoload.php';

// app container class - singleton pattern
class App
{
     // Hold an instance of the class
    private static $instance;

    public static function getInstance()
    {
        if (!isset(self::$instance)) {
            // Instantiate a Slim application:
            $app = new \Slim\Slim(array(
                'mode' => getenv('APPLICATION_ENV') ?: 'production',
            ));

            // set configuration
            require 'app/config.php';

            // include the routes (always after we've instantiated our app instance)
            require 'app/routes.php';

            self::$instance = $app;
        }
        return self::$instance;
    }
}

接下来,我的测试文件是一个测试:

AccountsControllerTest.php

<?php

use Slim\Environment;

class AccountsControllerTest extends PHPUnit_Framework_TestCase {

    public static function get($path)
    {
        Environment::mock(array(
            'REQUEST_METHOD' => 'GET',
            'PATH_INFO' => $path,
        ));

        $app = App::getInstance();

        //$app->middleware[0]->call();
        $app->response()->finalize();
        return $app->response();
    }

    public function testIndex() {

        $response = $this->get('/accounts');

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

希望我有点清楚我要做的事情。基本上只是检查&#34;账户&#34;是否存在(当我在浏览器中加载时,它存在)

以下是我回来的结果:

$ vendor/bin/phpunit
PHPUnit 4.3.4 by Sebastian Bergmann.

Configuration read from /var/www/shared-views-slim/phpunit.xml

F

Time: 33 ms, Memory: 4.75Mb

There was 1 failure:

1) AccountsControllerTest::testIndex
Failed asserting that '' contains "Accounts".

/var/www/shared-views-slim/tests/app/controllers/AccountsControllerTest.php:30

FAILURES!                            
Tests: 1, Assertions: 1, Failures: 1.

更新

routes.php文件

<?php

// Define a HTTP GET route:
$app->group('/accounts', function () use ($app) {

    $controller = new App\Controllers\AccountsController($app);

    // index

    $app->get('/', function () use ($controller) {
        $controller->index();
    });

    // show

    $app->get('/:id', function ($id) use ($controller) {
        $controller->show($id);
    })->conditions(array('id' => '[1-9]([0-9]*)'));

    // create

    // form
    $app->get('/create', function () use ($controller) {
        $controller->create();
    });    

    // action
    $app->post('/', function () use ($controller) {
        $controller->create();
    });

    // update

    // form
    $app->get('/:id/edit', function ($id) use ($controller) {
        $controller->update($id);
    })->conditions(array('id' => '[1-9]([0-9]*)')); 

    // action
    $app->put('/:id', function ($id) use ($controller) {
        $controller->update($id);
    });

    // delete

    // form
    $app->get('/:id/delete', function ($id) use ($controller) {
        $controller->delete($id);
    })->conditions(array('id' => '[1-9]([0-9]*)')); 

    //action
    $app->delete('/:id', function ($id) use ($controller) {
        $controller->delete($id);
    });

});

0 个答案:

没有答案