You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.8 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-31
  8. *
  9. * Unit tests for Layout class
  10. */
  11. require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php';
  12. require_once dirname(__FILE__) . '/../../app/FrontController.php';
  13. require_once dirname(__FILE__) . '/../../layout/Layout.php';
  14. /**
  15. * @runTestsInSeparateProcesses
  16. */
  17. class LayoutTest extends PHPUnit_Framework_TestCase
  18. {
  19. public function run(PHPUnit_Framework_TestResult $result = NULL)
  20. {
  21. $this->setPreserveGlobalState(false);
  22. return parent::run($result);
  23. }
  24. public function setUp()
  25. {
  26. if (!class_exists('RouterMock')) {
  27. $this->getMock('Router', array(), array(), 'RouterMock', false);
  28. }
  29. if (!class_exists('PHPView')) {
  30. $this->getMock('PHPView', array('fetch', 'append', 'prepend', 'assign', 'getTemplate'), array(), 'PHPViewMock', false);
  31. }
  32. set_new_overload(array($this, 'newCallback'));
  33. }
  34. public function testConstruct()
  35. {
  36. if (!defined('DEBUG')) {
  37. define('DEBUG', false);
  38. }
  39. $layout = $this->getMockForAbstractClass('Layout');
  40. $this->assertAttributeInstanceOf('PHPView', 'view', $layout);
  41. }
  42. public function testFetch()
  43. {
  44. if (!defined('DEBUG')) {
  45. define('DEBUG', false);
  46. }
  47. $layout = $this->getMockForAbstractClass('Layout');
  48. $action = $this->getMock('Action', array('fetch'));
  49. $action->expects($this->once())
  50. ->method('fetch')
  51. ->will($this->returnValue('this is the result of Action::fetch()'));
  52. $this->assertNull($layout->fetch($action));
  53. }
  54. public function testFetchWithTemplate()
  55. {
  56. if (!defined('DEBUG')) {
  57. define('DEBUG', false);
  58. }
  59. $layout = $this->getMockForAbstractClass('Layout');
  60. $class = new ReflectionClass('Layout');
  61. $template = $class->getProperty('template');
  62. $template->setAccessible(TRUE);
  63. $this->assertAttributeEquals(null, 'template', $layout);
  64. $template->setValue($layout, 'template');
  65. $this->assertAttributeEquals('template', 'template', $layout);
  66. $action = $this->getMock('Action', array('fetch'));
  67. $action->expects($this->once())
  68. ->method('fetch')
  69. ->will($this->returnValue('this is the result of Action::fetch()'));
  70. $this->assertNull($layout->fetch($action));
  71. }
  72. public function testAppend()
  73. {
  74. if (!defined('DEBUG')) {
  75. define('DEBUG', false);
  76. }
  77. $layout = $this->getMockForAbstractClass('Layout');
  78. $action = $this->getMock('Action', array('fetch'));
  79. $class = new ReflectionClass('Layout');
  80. $method = $class->getMethod('append');
  81. $method->setAccessible(true);
  82. $method->invoke($layout, 'var', $action);
  83. $method = $class->getMethod('prepend');
  84. $method->setAccessible(true);
  85. $method->invoke($layout, 'var', $action);
  86. $method = $class->getMethod('assign');
  87. $method->setAccessible(true);
  88. $method->invoke($layout, 'var', $action);
  89. }
  90. protected function newCallback($className)
  91. {
  92. switch ($className) {
  93. case 'Router':
  94. return 'RouterMock';
  95. case 'PHPView':
  96. return 'PHPViewMock';
  97. default:
  98. return $className;
  99. }
  100. }
  101. public function tearDown()
  102. {
  103. // if (defined('DEBUG')) {
  104. // $debug = DEBUG ? 'TRUE' : 'FALSE';
  105. // echo PHP_EOL . __CLASS__ . ' DEBUG = ' . $debug . PHP_EOL;
  106. // } else {
  107. // echo PHP_EOL . __CLASS__ . ' ' . 'DEBUG NOT DEFINED' . PHP_EOL;
  108. // }
  109. unset_new_overload();
  110. }
  111. }