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

13 years ago
13 years ago
  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('PHPViewMock')) {
  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. Config::set('DEBUG', 0);
  37. $layout = $this->getMockForAbstractClass('Layout');
  38. $this->assertAttributeInstanceOf('PHPView', 'view', $layout);
  39. }
  40. public function testFetch()
  41. {
  42. Config::set('DEBUG', 0);
  43. $layout = $this->getMockForAbstractClass('Layout');
  44. $action = $this->getMock('Action', array('fetch'));
  45. $action->expects($this->once())
  46. ->method('fetch')
  47. ->will($this->returnValue('this is the result of Action::fetch()'));
  48. $this->assertNull($layout->fetch($action));
  49. }
  50. public function testFetchWithTemplate()
  51. {
  52. Config::set('DEBUG', 0);
  53. $layout = $this->getMockForAbstractClass('Layout');
  54. $class = new ReflectionClass('Layout');
  55. $template = $class->getProperty('template');
  56. $template->setAccessible(TRUE);
  57. $this->assertAttributeEquals(null, 'template', $layout);
  58. $template->setValue($layout, 'template');
  59. $this->assertAttributeEquals('template', 'template', $layout);
  60. $action = $this->getMock('Action', array('fetch'));
  61. $action->expects($this->once())
  62. ->method('fetch')
  63. ->will($this->returnValue('this is the result of Action::fetch()'));
  64. $this->assertNull($layout->fetch($action));
  65. }
  66. public function testAppend()
  67. {
  68. Config::set('DEBUG', 0);
  69. $layout = $this->getMockForAbstractClass('Layout', array('append', 'prepend'), 'LayoutMock');
  70. $action = $this->getMock('Action', array('fetch'));
  71. $action->expects($this->exactly(3))
  72. ->method('fetch')
  73. ->will($this->returnValue(true));
  74. $class = new ReflectionClass('LayoutMock');
  75. $method = $class->getMethod('append');
  76. $method->setAccessible(true);
  77. $method->invoke($layout, 'var', $action);
  78. $method = $class->getMethod('prepend');
  79. $method->setAccessible(true);
  80. $method->invoke($layout, 'var', $action);
  81. $method = $class->getMethod('assign');
  82. $method->setAccessible(true);
  83. $method->invoke($layout, 'var', $action);
  84. }
  85. protected function newCallback($className)
  86. {
  87. switch ($className) {
  88. case 'Router':
  89. return 'RouterMock';
  90. case 'PHPView':
  91. return 'PHPViewMock';
  92. default:
  93. return $className;
  94. }
  95. }
  96. public function tearDown()
  97. {
  98. // if (defined('DEBUG')) {
  99. // $debug = DEBUG ? 'TRUE' : 'FALSE';
  100. // echo PHP_EOL . __CLASS__ . ' DEBUG = ' . $debug . PHP_EOL;
  101. // } else {
  102. // echo PHP_EOL . __CLASS__ . ' ' . 'DEBUG NOT DEFINED' . PHP_EOL;
  103. // }
  104. unset_new_overload();
  105. }
  106. }