ViewTest.php 2.4 KB

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