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.

138 lines
4.0 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. 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', array('append', 'prepend'), 'LayoutMock');
  78. $action = $this->getMock('Action', array('fetch'));
  79. $action->expects($this->exactly(3))
  80. ->method('fetch')
  81. ->will($this->returnValue(true));
  82. $class = new ReflectionClass('LayoutMock');
  83. $method = $class->getMethod('append');
  84. $method->setAccessible(true);
  85. $method->invoke($layout, 'var', $action);
  86. $method = $class->getMethod('prepend');
  87. $method->setAccessible(true);
  88. $method->invoke($layout, 'var', $action);
  89. $method = $class->getMethod('assign');
  90. $method->setAccessible(true);
  91. $method->invoke($layout, 'var', $action);
  92. }
  93. protected function newCallback($className)
  94. {
  95. switch ($className) {
  96. case 'Router':
  97. return 'RouterMock';
  98. case 'PHPView':
  99. return 'PHPViewMock';
  100. default:
  101. return $className;
  102. }
  103. }
  104. public function tearDown()
  105. {
  106. // if (defined('DEBUG')) {
  107. // $debug = DEBUG ? 'TRUE' : 'FALSE';
  108. // echo PHP_EOL . __CLASS__ . ' DEBUG = ' . $debug . PHP_EOL;
  109. // } else {
  110. // echo PHP_EOL . __CLASS__ . ' ' . 'DEBUG NOT DEFINED' . PHP_EOL;
  111. // }
  112. unset_new_overload();
  113. }
  114. }