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.

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