ViewComponentTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace Illuminate\Tests\View;
  3. use Illuminate\View\Component;
  4. use Illuminate\View\ComponentAttributeBag;
  5. use PHPUnit\Framework\TestCase;
  6. class ViewComponentTest extends TestCase
  7. {
  8. public function testDataExposure()
  9. {
  10. $component = new TestViewComponent;
  11. $variables = $component->data();
  12. $this->assertEquals(10, $variables['votes']);
  13. $this->assertSame('world', $variables['hello']());
  14. $this->assertSame('taylor', $variables['hello']('taylor'));
  15. }
  16. public function testAttributeParentInheritance()
  17. {
  18. $component = new TestViewComponent;
  19. $component->withAttributes(['class' => 'foo', 'attributes' => new ComponentAttributeBag(['class' => 'bar', 'type' => 'button'])]);
  20. $this->assertSame('class="foo bar" type="button"', (string) $component->attributes);
  21. }
  22. public function testPublicMethodsWithNoArgsAreConvertedToStringableCallablesInvokedAndNotCached()
  23. {
  24. $component = new TestSampleViewComponent;
  25. $this->assertEquals(0, $component->counter);
  26. $this->assertEquals(0, TestSampleViewComponent::$publicStaticCounter);
  27. $variables = $component->data();
  28. $this->assertEquals(0, $component->counter);
  29. $this->assertEquals(0, TestSampleViewComponent::$publicStaticCounter);
  30. $this->assertSame('noArgs val', $variables['noArgs']());
  31. $this->assertSame('noArgs val', (string) $variables['noArgs']);
  32. $this->assertEquals(0, $variables['counter']);
  33. // make sure non-public members are not invoked nor counted.
  34. $this->assertEquals(2, $component->counter);
  35. $this->assertArrayHasKey('publicHello', $variables);
  36. $this->assertArrayNotHasKey('protectedHello', $variables);
  37. $this->assertArrayNotHasKey('privateHello', $variables);
  38. $this->assertArrayNotHasKey('publicStaticCounter', $variables);
  39. $this->assertArrayNotHasKey('protectedCounter', $variables);
  40. $this->assertArrayNotHasKey('privateCounter', $variables);
  41. // test each time we invoke data(), the non-argument methods aren't invoked
  42. $this->assertEquals(2, $component->counter);
  43. $component->data();
  44. $this->assertEquals(2, $component->counter);
  45. $component->data();
  46. $this->assertEquals(2, $component->counter);
  47. }
  48. public function testItIgnoresExceptedMethodsAndProperties()
  49. {
  50. $component = new TestExceptedViewComponent;
  51. $variables = $component->data();
  52. // Ignored methods (with no args) are not invoked behind the scenes.
  53. $this->assertSame('Otwell', $component->taylor);
  54. $this->assertArrayNotHasKey('hello', $variables);
  55. $this->assertArrayNotHasKey('hello2', $variables);
  56. $this->assertArrayNotHasKey('taylor', $variables);
  57. }
  58. public function testMethodsOverridePropertyValues()
  59. {
  60. $component = new TestHelloPropertyHelloMethodComponent;
  61. $variables = $component->data();
  62. $this->assertArrayHasKey('hello', $variables);
  63. $this->assertSame('world', $variables['hello']());
  64. // protected methods do not override public properties.
  65. $this->assertArrayHasKey('world', $variables);
  66. $this->assertSame('world property', $variables['world']);
  67. }
  68. }
  69. class TestViewComponent extends Component
  70. {
  71. public $votes = 10;
  72. public function render()
  73. {
  74. return 'test';
  75. }
  76. public function hello($string = 'world')
  77. {
  78. return $string;
  79. }
  80. }
  81. class TestSampleViewComponent extends Component
  82. {
  83. public $counter = 0;
  84. public static $publicStaticCounter = 0;
  85. protected $protectedCounter = 0;
  86. private $privateCounter = 0;
  87. public function render()
  88. {
  89. return 'test';
  90. }
  91. public function publicHello($string = 'world')
  92. {
  93. $this->counter = 100;
  94. return $string;
  95. }
  96. public function noArgs()
  97. {
  98. $this->counter++;
  99. return 'noArgs val';
  100. }
  101. protected function protectedHello()
  102. {
  103. $this->counter++;
  104. }
  105. private function privateHello()
  106. {
  107. $this->counter++;
  108. }
  109. }
  110. class TestExceptedViewComponent extends Component
  111. {
  112. protected $except = ['hello', 'hello2', 'taylor'];
  113. public $taylor = 'Otwell';
  114. public function hello($string = 'world')
  115. {
  116. return $string;
  117. }
  118. public function hello2()
  119. {
  120. return $this->taylor = '';
  121. }
  122. public function render()
  123. {
  124. return 'test';
  125. }
  126. }
  127. class TestHelloPropertyHelloMethodComponent extends Component
  128. {
  129. public function render()
  130. {
  131. return 'test';
  132. }
  133. public $hello = 'hello property';
  134. public $world = 'world property';
  135. public function hello($string = 'world')
  136. {
  137. return $string;
  138. }
  139. protected function world($string = 'world')
  140. {
  141. return $string;
  142. }
  143. }
  144. class TestDefaultAttributesComponent extends Component
  145. {
  146. public function __construct()
  147. {
  148. $this->withAttributes(['class' => 'text-red-500']);
  149. }
  150. public function render()
  151. {
  152. return $this->attributes->get('id');
  153. }
  154. }