Reproducible Tests


Reproducible Tests - 图1

If you have found a bug, you can open an issue in GitHub. Along with your description of the bug, you will need to provide as much information as possible so that the core team can reproduce the behavior you are experiencing. The best way to do this is to create a test that fails, showcasing the behavior. If the bug you found is in an application that is publicly available in a repository, please provide also the link for this repository. You can also use a Gist to post any code you want to share with us.

Creating a small script

A small PHP file can be used to showcase how to reproduce the issue:

  1. <?php
  2. use Phalcon\Di\FactoryDefault;
  3. use Phalcon\Di\Injectable;
  4. use Phalcon\Session\Manager;
  5. use Phalcon\Session\Adapter\Files;
  6. use Phalcon\Http\Response\Cookies;
  7. $container = new FactoryDefault();
  8. // Register your custom services
  9. $container['session'] = function() {
  10. $session = new Manager();
  11. $adapter = new Files(
  12. [
  13. 'save_path' => '/tmp',
  14. ]
  15. );
  16. $session->setHandler($adapter);
  17. $session->start();
  18. return $session;
  19. };
  20. $container['cookies'] = function() {
  21. $cookies = new Cookies();
  22. $cookies->useEncryption(false);
  23. return $cookies;
  24. };
  25. class SomeClass extends Injectable
  26. {
  27. public function someMethod()
  28. {
  29. $cookies = $this->getDI()->getCookies();
  30. $cookies->set(
  31. 'mycookie',
  32. 'test',
  33. time() + 3600,
  34. '/'
  35. );
  36. }
  37. }
  38. $class = new MyClass();
  39. $class->setDI($container);
  40. $class->someMethod();
  41. $container['cookies']->send();
  42. var_dump($_SESSION);
  43. var_dump($_COOKIE);

Database

Remember to include the register information for your db service, i.e. adapter, connection parameters etc.

  1. <?php
  2. use Phalcon\Di\FactoryDefault;
  3. use Phalcon\Db\Adapter\Pdo\Mysql;
  4. $container = new FactoryDefault();
  5. $container->setShared(
  6. 'db',
  7. function () {
  8. return new Mysql(
  9. [
  10. 'host' => '127.0.0.1',
  11. 'username' => 'root',
  12. 'password' => '',
  13. 'dbname' => 'test',
  14. 'charset' => 'utf8',
  15. ]
  16. );
  17. }
  18. );
  19. $result = $container['db']->query('SELECT * FROM customers');

Single/Multi-Module applications

Remember to add to the script how you are creating the Phalcon\Mvc\Application instance and how you register your modules

  1. <?php
  2. use Phalcon\Di\FactoryDefault;
  3. use Phalcon\Mvc\Application;
  4. $container = new FactoryDefault();
  5. // other services
  6. $application = new Application();
  7. $application->setDI($container);
  8. // register modules if any
  9. $response = $application->handle(
  10. $_SERVER["REQUEST_URI"]
  11. );
  12. echo $response->getContent();

Include models and controllers as part of the test:

  1. <?php
  2. use Phalcon\Di\FactoryDefault;
  3. use Phalcon\Mvc\Application;
  4. use Phalcon\Mvc\Controller;
  5. use Phalcon\Mvc\Model;
  6. $container = new FactoryDefault();
  7. // other services
  8. $application = new Application();
  9. $application->setDI($container);
  10. class IndexController extends Controller
  11. {
  12. public function indexAction() {
  13. /* your content here */
  14. }
  15. }
  16. class Users extends Model
  17. {
  18. }
  19. $response = $application->handle(
  20. $_SERVER["REQUEST_URI"]
  21. );
  22. echo $response->getContent();

Micro application

For micro applications, you can use the skeleton script below:

  1. <?php
  2. use Phalcon\Di\FactoryDefault;
  3. use Phalcon\Mvc\Micro;
  4. $container = new FactoryDefault();
  5. // other services
  6. $application = new Micro($container);
  7. // define your routes here
  8. $application->handle(
  9. $_SERVER["REQUEST_URI"]
  10. );

ORM

You can provide your own database schema or even better, use any of the existing schemas in our testing suite (located in tests/_data/assets/db/schemas/ in the repository).

  1. <?php
  2. use Phalcon\Di;
  3. use Phalcon\Db\Adapter\Pdo\Mysql as Connection;
  4. use Phalcon\Events\Manager as EventsManager;
  5. use Phalcon\Mvc\Model;
  6. use Phalcon\Mvc\Model\Manager as ModelsManager;
  7. use Phalcon\Mvc\Model\Metadata\Memory as ModelsMetaData;
  8. $eventsManager = new EventsManager();
  9. $container = new Di();
  10. $connection = new Connection(
  11. [
  12. 'host' => 'localhost',
  13. 'username' => 'root',
  14. 'password' => '',
  15. 'dbname' => 'test',
  16. ]
  17. );
  18. $connection->setEventsManager($eventsManager);
  19. $eventsManager->attach(
  20. 'db:beforeQuery',
  21. function ($event, $connection) {
  22. echo $connection->getSqlStatement(), '<br>' . PHP_EOL;
  23. }
  24. );
  25. $container['db'] = $connection;
  26. $container['modelsManager'] = new ModelsManager();
  27. $container['modelsMetadata'] = new ModelsMetadata();
  28. if (true !== $connection->tableExists('user', 'test')) {
  29. $connection->execute(
  30. 'CREATE TABLE user (id integer primary key auto_increment, email varchar(120) not null)'
  31. );
  32. }
  33. class User extends Model
  34. {
  35. public $id;
  36. public $email;
  37. public static function createNewUserReturnId()
  38. {
  39. $newUser = new User();
  40. $newUser->email = 'test';
  41. if (false === $newUser->save()) {
  42. return false;
  43. }
  44. return $newUser->id;
  45. }
  46. }
  47. echo User::createNewUserReturnId();