ViewTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace think\tests;
  3. use Mockery\MockInterface;
  4. use PHPUnit\Framework\TestCase;
  5. use think\App;
  6. use think\Config;
  7. use think\Container;
  8. use think\contract\TemplateHandlerInterface;
  9. use think\View;
  10. use Mockery as m;
  11. class ViewTest extends TestCase
  12. {
  13. /** @var App|MockInterface */
  14. protected $app;
  15. /** @var View|MockInterface */
  16. protected $view;
  17. /** @var Config|MockInterface */
  18. protected $config;
  19. protected function tearDown(): void
  20. {
  21. m::close();
  22. }
  23. protected function setUp()
  24. {
  25. $this->app = m::mock(App::class)->makePartial();
  26. Container::setInstance($this->app);
  27. $this->app->shouldReceive('make')->with(App::class)->andReturn($this->app);
  28. $this->config = m::mock(Config::class)->makePartial();
  29. $this->app->shouldReceive('get')->with('config')->andReturn($this->config);
  30. $this->view = new View($this->app);
  31. }
  32. public function testAssignData()
  33. {
  34. $this->view->assign('foo', 'bar');
  35. $this->view->assign(['baz' => 'boom']);
  36. $this->view->qux = "corge";
  37. $this->assertEquals('bar', $this->view->foo);
  38. $this->assertEquals('boom', $this->view->baz);
  39. $this->assertEquals('corge', $this->view->qux);
  40. $this->assertTrue(isset($this->view->qux));
  41. }
  42. public function testRender()
  43. {
  44. $this->config->shouldReceive("get")->with("view.type", 'php')->andReturn(TestTemplate::class);
  45. $this->view->filter(function ($content) {
  46. return $content;
  47. });
  48. $this->assertEquals("fetch", $this->view->fetch('foo'));
  49. $this->assertEquals("display", $this->view->display('foo'));
  50. }
  51. }
  52. class TestTemplate implements TemplateHandlerInterface
  53. {
  54. /**
  55. * 检测是否存在模板文件
  56. * @access public
  57. * @param string $template 模板文件或者模板规则
  58. * @return bool
  59. */
  60. public function exists(string $template): bool
  61. {
  62. return true;
  63. }
  64. /**
  65. * 渲染模板文件
  66. * @access public
  67. * @param string $template 模板文件
  68. * @param array $data 模板变量
  69. * @return void
  70. */
  71. public function fetch(string $template, array $data = []): void
  72. {
  73. echo "fetch";
  74. }
  75. /**
  76. * 渲染模板内容
  77. * @access public
  78. * @param string $content 模板内容
  79. * @param array $data 模板变量
  80. * @return void
  81. */
  82. public function display(string $content, array $data = []): void
  83. {
  84. echo "display";
  85. }
  86. /**
  87. * 配置模板引擎
  88. * @access private
  89. * @param array $config 参数
  90. * @return void
  91. */
  92. public function config(array $config): void
  93. {
  94. // TODO: Implement config() method.
  95. }
  96. /**
  97. * 获取模板引擎配置
  98. * @access public
  99. * @param string $name 参数名
  100. * @return void
  101. */
  102. public function getConfig(string $name)
  103. {
  104. // TODO: Implement getConfig() method.
  105. }
  106. }