ViewTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Tests the View class
  4. *
  5. * @group kohana
  6. * @group kohana.core
  7. * @group kohana.core.view
  8. *
  9. * @package Kohana
  10. * @category Tests
  11. * @author Kohana Team
  12. * @copyright (c) Kohana Team
  13. * @license https://koseven.ga/LICENSE.md
  14. */
  15. class Kohana_ViewTest extends Unittest_TestCase
  16. {
  17. protected static $old_modules = [];
  18. /**
  19. * Setups the filesystem for test view files
  20. *
  21. * @return null
  22. */
  23. // @codingStandardsIgnoreStart
  24. public static function setupBeforeClass(): void
  25. // @codingStandardsIgnoreEnd
  26. {
  27. self::$old_modules = Kohana::modules();
  28. $new_modules = self::$old_modules+[
  29. 'test_views' => realpath(dirname(__FILE__).'/../test_data/')
  30. ];
  31. Kohana::modules($new_modules);
  32. }
  33. /**
  34. * Restores the module list
  35. *
  36. * @return null
  37. */
  38. // @codingStandardsIgnoreStart
  39. public static function teardownAfterClass(): void
  40. // @codingStandardsIgnoreEnd
  41. {
  42. Kohana::modules(self::$old_modules);
  43. }
  44. /**
  45. * Provider for test_instaniate
  46. *
  47. * @return array
  48. */
  49. public function provider_instantiate()
  50. {
  51. return [
  52. ['kohana/error', FALSE],
  53. ['test.css', FALSE],
  54. ['doesnt_exist', TRUE],
  55. ];
  56. }
  57. /**
  58. * Provider to test_set
  59. *
  60. * @return array
  61. */
  62. public function provider_set()
  63. {
  64. return [
  65. ['foo', 'bar', 'foo', 'bar'],
  66. [['foo' => 'bar'], NULL, 'foo', 'bar'],
  67. [new ArrayIterator(['foo' => 'bar']), NULL, 'foo', 'bar'],
  68. ];
  69. }
  70. /**
  71. * Tests that we can instantiate a view file
  72. *
  73. * @test
  74. * @dataProvider provider_instantiate
  75. *
  76. * @return null
  77. */
  78. public function test_instantiate($path, $expects_exception)
  79. {
  80. try
  81. {
  82. $view = new View($path);
  83. $this->assertSame(FALSE, $expects_exception);
  84. }
  85. catch(View_Exception $e)
  86. {
  87. $this->assertSame(TRUE, $expects_exception);
  88. }
  89. }
  90. /**
  91. * Tests that we can set using string, array or Traversable object
  92. *
  93. * @test
  94. * @dataProvider provider_set
  95. *
  96. * @return null
  97. */
  98. public function test_set($data_key, $value, $test_key, $expected)
  99. {
  100. $view = View::factory()->set($data_key, $value);
  101. $this->assertSame($expected, $view->$test_key);
  102. }
  103. /**
  104. * Tests that we can set global using string, array or Traversable object
  105. *
  106. * @test
  107. * @dataProvider provider_set
  108. *
  109. * @return null
  110. */
  111. public function test_set_global($data_key, $value, $test_key, $expected)
  112. {
  113. $view = View::factory();
  114. $view::set_global($data_key, $value);
  115. $this->assertSame($expected, $view->$test_key);
  116. }
  117. }