ContextTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. class TestController extends \Tools\AController
  3. {
  4. public $started = false;
  5. public function start()
  6. {
  7. $this->started = true;
  8. }
  9. }
  10. class TestController2 extends \Tools\AController
  11. {
  12. public $started = false;
  13. public $params;
  14. public function __construct($c, $p)
  15. {
  16. parent::__construct($c, $p);
  17. $this->params = $p;
  18. if (isset($p[":testId"]) && $p[":testId"] == "notFound")
  19. throw new \Exception\Error404();
  20. }
  21. public function start()
  22. {
  23. $this->started = true;
  24. }
  25. public function getId()
  26. {
  27. if (!isset($this->params[":testId"]))
  28. return false;
  29. return $this->params[":testId"];
  30. }
  31. }
  32. class ContextTest extends PHPUnit_Framework_TestCase
  33. {
  34. public function testContext()
  35. {
  36. list($server, $mysqlConfig) = require("test/config.php");
  37. $dbh = new PDO($mysqlConfig[4][0], $mysqlConfig[1], $mysqlConfig[2]);
  38. $dbh->exec("DROP DATABASE ".$mysqlConfig[4][1]);
  39. $dbh->exec("CREATE DATABASE ".$mysqlConfig[4][1]);
  40. \Entity\ModelBase::init($mysqlConfig);
  41. // Double init test
  42. \Entity\ModelBase::init();
  43. $context = new \Tools\Context($server, false);
  44. \Entity\ModelBase::setup();
  45. $route = new \Entity\Cms();
  46. $route->shurl = "/testController0";
  47. $route->controller = "\TestController";
  48. $route->order = 0;
  49. $route->save();
  50. $this->assertNotNull($route->id);
  51. $this->assertNotFalse($route->id);
  52. $route = new \Entity\Cms();
  53. $route->shurl = "/test/:testId";
  54. $route->controller = "\TestController2";
  55. $route->order = 0;
  56. $route->save();
  57. $this->assertNotNull($route->id);
  58. $this->assertNotFalse($route->id);
  59. $this->assertEquals("127.0.0.10", $context->ip);
  60. $this->assertEquals($context, \Tools\Context::getContext());
  61. try {
  62. $i = $context->undefinedAttribute;
  63. $this->fail("Expected exception");
  64. }
  65. catch (\Exception $e)
  66. { }
  67. $server["REQUEST_URI"] = "/testController0";
  68. $context = new \Tools\Context($server, false);
  69. $context->serve();
  70. $myController = $context->controller;
  71. $this->assertNotNull($myController);
  72. $this->assertInstanceOf("\TestController", $myController);
  73. $this->assertFalse($myController->started);
  74. $server["REQUEST_URI"] = "/";
  75. $context = new \Tools\Context($server, false);
  76. $context->serve();
  77. $myController = $context->controller;
  78. $this->assertNotNull($myController);
  79. $this->assertInstanceOf("\Controller\HomeController", $myController);
  80. $server["REQUEST_URI"] = "/test/notFound";
  81. $context = new \Tools\Context($server, false);
  82. $context->serve();
  83. $myController = $context->controller;
  84. $this->assertNotNull($myController);
  85. $this->assertInstanceOf("\Controller\Error404", $myController);
  86. }
  87. }