PageFactoryTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Navigation
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once 'Zend/Navigation/Page.php';
  23. /**
  24. * Tests Zend_Navigation_Page::factory()
  25. *
  26. /**
  27. * @category Zend
  28. * @package Zend_Navigation
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Navigation
  33. */
  34. class Zend_Navigation_PageFactoryTest extends PHPUnit_Framework_TestCase
  35. {
  36. protected $_oldIncludePath;
  37. protected function setUp()
  38. {
  39. // store old include path
  40. $this->_oldIncludePath = get_include_path();
  41. // add _files dir to include path
  42. $addToPath = dirname(__FILE__) . '/_files';
  43. set_include_path($addToPath . PATH_SEPARATOR . $this->_oldIncludePath);
  44. }
  45. protected function tearDown()
  46. {
  47. // reset include path
  48. set_include_path($this->_oldIncludePath);
  49. }
  50. public function testDetectMvcPage()
  51. {
  52. $pages = array(
  53. Zend_Navigation_Page::factory(array(
  54. 'label' => 'MVC Page',
  55. 'action' => 'index'
  56. )),
  57. Zend_Navigation_Page::factory(array(
  58. 'label' => 'MVC Page',
  59. 'controller' => 'index'
  60. )),
  61. Zend_Navigation_Page::factory(array(
  62. 'label' => 'MVC Page',
  63. 'module' => 'index'
  64. )),
  65. Zend_Navigation_Page::factory(array(
  66. 'label' => 'MVC Page',
  67. 'route' => 'home'
  68. )),
  69. Zend_Navigation_Page::factory(array(
  70. 'label' => 'MVC Page',
  71. 'params' => array(
  72. 'foo' => 'bar',
  73. ),
  74. )),
  75. );
  76. $this->assertContainsOnly('Zend_Navigation_Page_Mvc', $pages);
  77. }
  78. public function testDetectUriPage()
  79. {
  80. $page = Zend_Navigation_Page::factory(array(
  81. 'label' => 'URI Page',
  82. 'uri' => '#'
  83. ));
  84. $this->assertTrue($page instanceof Zend_Navigation_Page_Uri);
  85. }
  86. public function testSupportsMvcShorthand()
  87. {
  88. $mvcPage = Zend_Navigation_Page::factory(array(
  89. 'type' => 'mvc',
  90. 'label' => 'MVC Page',
  91. 'action' => 'index',
  92. 'controller' => 'index'
  93. ));
  94. $this->assertTrue($mvcPage instanceof Zend_Navigation_Page_Mvc);
  95. }
  96. public function testSupportsUriShorthand()
  97. {
  98. $uriPage = Zend_Navigation_Page::factory(array(
  99. 'type' => 'uri',
  100. 'label' => 'URI Page',
  101. 'uri' => 'http://www.example.com/'
  102. ));
  103. $this->assertTrue($uriPage instanceof Zend_Navigation_Page_Uri);
  104. }
  105. public function testSupportsCustomPageTypes()
  106. {
  107. $page = Zend_Navigation_Page::factory(array(
  108. 'type' => 'My_Page',
  109. 'label' => 'My Custom Page'
  110. ));
  111. return $this->assertTrue($page instanceof My_Page);
  112. }
  113. public function testShouldFailForInvalidType()
  114. {
  115. try {
  116. $page = Zend_Navigation_Page::factory(array(
  117. 'type' => 'My_InvalidPage',
  118. 'label' => 'My Invalid Page'
  119. ));
  120. } catch(Zend_Navigation_Exception $e) {
  121. return;
  122. }
  123. $this->fail('An exception has not been thrown for invalid page type');
  124. }
  125. public function testShouldFailForNonExistantType()
  126. {
  127. $pageConfig = array(
  128. 'type' => 'My_NonExistant_Page',
  129. 'label' => 'My non-existant Page'
  130. );
  131. try {
  132. $page = Zend_Navigation_Page::factory($pageConfig);
  133. $this->fail(
  134. 'A Zend_Exception has not been thrown for non-existant class'
  135. );
  136. } catch(Zend_Exception $e) {
  137. $this->assertEquals(
  138. 'File "My' . DIRECTORY_SEPARATOR . 'NonExistant' . DIRECTORY_SEPARATOR . 'Page.php" does not exist or class '
  139. . '"My_NonExistant_Page" was not found in the file',
  140. $e->getMessage()
  141. );
  142. }
  143. }
  144. public function testShouldFailIfUnableToDetermineType()
  145. {
  146. try {
  147. $page = Zend_Navigation_Page::factory(array(
  148. 'label' => 'My Invalid Page'
  149. ));
  150. $this->fail(
  151. 'An exception has not been thrown for invalid page type'
  152. );
  153. } catch(Zend_Navigation_Exception $e) {
  154. $this->assertEquals(
  155. 'Invalid argument: Unable to determine class to instantiate '
  156. . '(Page label: My Invalid Page)',
  157. $e->getMessage()
  158. );
  159. }
  160. }
  161. }