跳转到内容

可重复测试


注意

如果你发现了一个错误,你可以在GitHub. 除了对错误的描述外,还需要提供尽可能多的信息,以便核心团队能够重现你所遇到的行为。最好的方法是创建一个失败的测试,以展示该行为。如果你发现的错误存在于一个公共存储库中的应用程序,请同时提供该存储库的链接。你也可以使用Gist来分享你想与我们共享的任何代码。

创建一个小脚本

一个小型 PHP 文件可以用于展示如何重现该问题:

<?php

use Phalcon\Di\FactoryDefault;
use Phalcon\Di\Injectable;
use Phalcon\Session\Manager;
use Phalcon\Session\Adapter\Files;
use Phalcon\Http\Response\Cookies;

$container = new FactoryDefault();

// Register your custom services
$container['session'] = function() {
    $session = new Manager();
    $adapter = new Files(
        [
            'save_path' => '/tmp',
         ]
    );

    $session->setHandler($adapter);

    $session->start();

    return $session;
};

$container['cookies'] = function() {
    $cookies = new Cookies();

    $cookies->useEncryption(false);

    return $cookies;
};

class SomeClass extends Injectable
{
    public function someMethod()
    {
        $cookies = $this->getDI()->getCookies();

        $cookies->set(
            'mycookie',
            'test',
            time() + 3600,
            '/'
        );
    }
}

$class = new MyClass();

$class->setDI($container);

$class->someMethod();

$container['cookies']->send();

var_dump($_SESSION);
var_dump($_COOKIE);

数据库

注意

记得包含你的db服务的注册信息,即适配器、连接参数等。

<?php

use Phalcon\Di\FactoryDefault;
use Phalcon\Db\Adapter\Pdo\Mysql;

$container = new FactoryDefault();

$container->setShared(
    'db', 
    function () {
        return new Mysql(
            [
                'host'     => '127.0.0.1',
                'username' => 'root',
                'password' => '',
                'dbname'   => 'test',
                'charset'  => 'utf8',
            ]
        );
    }
);

$result = $container['db']->query('SELECT * FROM customers');

单模块/多模块应用程序

注意

记得将脚本中添加你创建Phalcon\Mvc\Application实例的方式以及注册模块的方式

<?php

use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Application;

$container = new FactoryDefault();

// other services

$application = new Application();

$application->setDI($container);

// register modules if any

$response = $application->handle(
    $_SERVER["REQUEST_URI"]
);

echo $response->getContent();

将模型和控制器作为测试的一部分包含进去:

<?php

use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Application;
use Phalcon\Mvc\Controller;
use Phalcon\Mvc\Model;

$container = new FactoryDefault();

// other services

$application = new Application();

$application->setDI($container);

class IndexController extends Controller
{
    public function indexAction() { 
          /* your content here */
    }
}

class Users extends Model
{
}

$response = $application->handle(
    $_SERVER["REQUEST_URI"]
);

echo $response->getContent();

微型应用

对于微型应用,你可以使用以下骨架脚本:

<?php

use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Micro;

$container = new FactoryDefault();

// other services

$application = new Micro($container);

// define your routes here

$application->handle(
    $_SERVER["REQUEST_URI"]
);

ORM

注意

你可以提供自己的数据库模式,甚至更好的是,使用我们测试套件中现有的任何模式(位于tests/_data/assets/db/schemas/在仓库中)。

<?php

use Phalcon\Di\Di;
use Phalcon\Db\Adapter\Pdo\Mysql as Connection;
use Phalcon\Events\Manager as EventsManager;
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Manager as ModelsManager;
use Phalcon\Mvc\Model\Metadata\Memory as ModelsMetaData;

$eventsManager = new EventsManager();
$container     = new Di();
$connection    = new Connection(
    [
        'host'     => 'localhost',
        'username' => 'root',
        'password' => '',
        'dbname'   => 'test',
    ]
);

$connection->setEventsManager($eventsManager);

$eventsManager->attach(
    'db:beforeQuery',
    function ($event, $connection) {
        echo $connection->getSqlStatement(), '<br>' . PHP_EOL;
    }
);

$container['db']             = $connection;
$container['modelsManager']  = new ModelsManager();
$container['modelsMetadata'] = new ModelsMetadata();

if (true !== $connection->tableExists('user', 'test')) {
    $connection->execute(
        'CREATE TABLE user (
            id integer primary key auto_increment, 
            email varchar(120) not null
        )'
    );
}

class User extends Model
{
    public $id;

    public $email;

    public static function createNewUserReturnId()
    {
        $newUser = new User();

        $newUser->email = 'test';

        if (false === $newUser->save()) {
            return false;
        }

        return $newUser->id;
    }
}

echo User::createNewUserReturnId();
无噪 Logo
无噪文档
25 年 6 月翻译
文档源↗