ContextTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. $context = new \Tools\Context($server, false);
  42. \Entity\ModelBase::setup();
  43. $route = new \Entity\Cms();
  44. $route->shurl = "/";
  45. $route->controller = "\TestController";
  46. $route->order = 0;
  47. $route->save();
  48. $this->assertEquals(1, $route->id);
  49. $route = new \Entity\Cms();
  50. $route->shurl = "/test/:testId";
  51. $route->controller = "\TestController2";
  52. $route->order = 0;
  53. $route->save();
  54. $this->assertEquals(2, $route->id);
  55. $this->assertEquals("127.0.0.10", $context->ip);
  56. $this->assertEquals($context, \Tools\Context::getContext());
  57. try {
  58. $i = $context->undefinedAttribute;
  59. $this->fail("Expected exception");
  60. }
  61. catch (\Exception $e)
  62. { }
  63. $context->serve();
  64. $myController = $context->controller;
  65. $this->assertNotNull($myController);
  66. $this->assertInstanceOf("\TestController", $myController);
  67. $this->assertFalse($myController->started);
  68. $server["REQUEST_URI"] = "/test";
  69. $context = new \Tools\Context($server, false);
  70. $context->serve();
  71. $myController = $context->controller;
  72. $this->assertNotNull($myController);
  73. $this->assertInstanceOf("\Controller\Error404", $myController);
  74. $server["REQUEST_URI"] = "/test/42";
  75. $context = new \Tools\Context($server, false);
  76. $context->serve();
  77. $myController = $context->controller;
  78. $this->assertNotNull($myController);
  79. $this->assertInstanceOf("\TestController2", $myController);
  80. $this->assertEquals(42, (int) $myController->getId());
  81. $server["REQUEST_URI"] = "/test/notFound";
  82. $context = new \Tools\Context($server, false);
  83. $context->serve();
  84. $myController = $context->controller;
  85. $this->assertNotNull($myController);
  86. $this->assertInstanceOf("\Controller\Error404", $myController);
  87. }
  88. }