测试重现
最后更新于:2022-04-02 05:12:54
[TOC]
如果您发现了错误,请务必在您的问题中添加相关的可重现性信息,以便我们重现错误并更快地修复错误。如果您在Github上公开申请,请提交存储库地址以及问题说明。您还可以使用 [Gist](https://gist.github.com/) 发布您想要与我们分享的任何代码。
## 创建一个小脚本
一个小的单文件脚本通常是重现问题的最佳方法:
```php
start();
return $session;
};
$di['cookies'] = function() {
$cookies = new Phalcon\Http\Response\Cookies();
$cookies->useEncryption(false);
return $cookies;
};
class SomeClass extends \Phalcon\DI\Injectable
{
public function someMethod()
{
$cookies = $this->getDI()->getCookies();
$cookies->set("mycookie", "test", time() + 3600, "/");
}
}
$c = new MyClass;
$c->setDI($di);
$c->someMethod();
$di['cookies']->send();
var_dump($_SESSION);
var_dump($_COOKIE);
```
根据您的应用程序,您可以使用这些框架来创建自己的脚本并重现该错误:
### 数据库
请记住向脚本添加如何注册数据库服务:
```php
setShared('db', function () {
return new \Phalcon\Db\Adapter\PDO\Mysql(array(
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'dbname' => 'test',
'charset' => 'utf8',
));
});
$result = $di['db']->query('SELECT * FROM customers');
```
### 单/多模块应用
请记住向脚本添加如何创建 `Phalcon\Mvc\Application` 实例:
```php
setDi($di);
//register modules if any
echo $app->handle->getContent()
```
包括模型和控制器作为测试的一部分:
```php
setDi($di);
class IndexController extends Phalcon\Mvc\Controller
{
public function indexAction() {
/* your content here */
}
}
class Users extends Phalcon\Mvc\Model
{
}
echo $app->handle->getContent()
```
### 微应用
按照此结构创建脚本:
```php
handle();
```
### Dispatcher
### ORM
您可以提供自己的数据库模式,甚至可以更好地使用任何phalcon测试[数据库](https://github.com/phalcon/cphalcon/tree/master/unit-tests/schemas)。按照此结构创建脚本:
```php
"localhost",
"username" => "root",
"password" => "",
"dbname" => "test"
));
$connection->setEventsManager($eventsManager);
$eventsManager->attach('db',
function ($event, $connection) {
switch ($event->getType()) {
case 'beforeQuery':
echo $connection->getSqlStatement(), "
\n"; break; } } ); $di['db'] = $connection; $di['modelsManager'] = new ModelsManager(); $di['modelsMetadata'] = new ModelsMetadata(); if (!$connection->tableExists('user', 'test')) { $connection->execute('CREATE TABLE user (id integer primary key auto_increment, email varchar(120) not null)'); } class User extends \Phalcon\Mvc\Model { public $id; public $email; public static function myCustomUserCreator() { $newUser = new User(); $newUser->email = 'test'; if ($newUser->save() == false) { return false; } return $newUser->id; } } echo User::myCustomUserCreator(); ```
';
\n"; break; } } ); $di['db'] = $connection; $di['modelsManager'] = new ModelsManager(); $di['modelsMetadata'] = new ModelsMetadata(); if (!$connection->tableExists('user', 'test')) { $connection->execute('CREATE TABLE user (id integer primary key auto_increment, email varchar(120) not null)'); } class User extends \Phalcon\Mvc\Model { public $id; public $email; public static function myCustomUserCreator() { $newUser = new User(); $newUser->email = 'test'; if ($newUser->save() == false) { return false; } return $newUser->id; } } echo User::myCustomUserCreator(); ```