ViewComponentAttributeBagTest.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Illuminate\Tests\View;
  3. use Illuminate\View\ComponentAttributeBag;
  4. use PHPUnit\Framework\TestCase;
  5. class ViewComponentAttributeBagTest extends TestCase
  6. {
  7. public function testAttributeRetrieval()
  8. {
  9. $bag = new ComponentAttributeBag(['class' => 'font-bold', 'name' => 'test']);
  10. $this->assertSame('class="font-bold"', (string) $bag->whereStartsWith('class'));
  11. $this->assertSame('font-bold', (string) $bag->whereStartsWith('class')->first());
  12. $this->assertSame('name="test"', (string) $bag->whereDoesntStartWith('class'));
  13. $this->assertSame('test', (string) $bag->whereDoesntStartWith('class')->first());
  14. $this->assertSame('class="mt-4 font-bold" name="test"', (string) $bag->merge(['class' => 'mt-4']));
  15. $this->assertSame('class="mt-4 font-bold" name="test"', (string) $bag->merge(['class' => 'mt-4', 'name' => 'foo']));
  16. $this->assertSame('class="mt-4 font-bold" id="bar" name="test"', (string) $bag->merge(['class' => 'mt-4', 'id' => 'bar']));
  17. $this->assertSame('class="mt-4 font-bold" name="test"', (string) $bag(['class' => 'mt-4']));
  18. $this->assertSame('class="mt-4 font-bold"', (string) $bag->only('class')->merge(['class' => 'mt-4']));
  19. $this->assertSame('name="test" class="font-bold"', (string) $bag->merge(['name' => 'default']));
  20. $this->assertSame('class="font-bold" name="test"', (string) $bag->merge([]));
  21. $this->assertSame('class="mt-4 font-bold"', (string) $bag->merge(['class' => 'mt-4'])->only('class'));
  22. $this->assertSame('class="mt-4 font-bold"', (string) $bag->only('class')(['class' => 'mt-4']));
  23. $this->assertSame('font-bold', $bag->get('class'));
  24. $this->assertSame('bar', $bag->get('foo', 'bar'));
  25. $this->assertSame('font-bold', $bag['class']);
  26. $this->assertSame('class="mt-4 font-bold" name="test"', (string) $bag->class('mt-4'));
  27. $this->assertSame('class="mt-4 font-bold" name="test"', (string) $bag->class(['mt-4']));
  28. $this->assertSame('class="mt-4 ml-2 font-bold" name="test"', (string) $bag->class(['mt-4', 'ml-2' => true, 'mr-2' => false]));
  29. $bag = new ComponentAttributeBag([]);
  30. $this->assertSame('class="mt-4"', (string) $bag->merge(['class' => 'mt-4']));
  31. $bag = new ComponentAttributeBag([
  32. 'test-string' => 'ok',
  33. 'test-null' => null,
  34. 'test-false' => false,
  35. 'test-true' => true,
  36. 'test-0' => 0,
  37. 'test-0-string' => '0',
  38. 'test-empty-string' => '',
  39. ]);
  40. $this->assertSame('test-string="ok" test-true="test-true" test-0="0" test-0-string="0" test-empty-string=""', (string) $bag);
  41. $this->assertSame('test-string="ok" test-true="test-true" test-0="0" test-0-string="0" test-empty-string=""', (string) $bag->merge());
  42. $bag = (new ComponentAttributeBag)
  43. ->merge([
  44. 'test-escaped' => '<tag attr="attr">',
  45. ]);
  46. $this->assertSame('test-escaped="&lt;tag attr=&quot;attr&quot;&gt;"', (string) $bag);
  47. $bag = (new ComponentAttributeBag)
  48. ->merge([
  49. 'test-string' => 'ok',
  50. 'test-null' => null,
  51. 'test-false' => false,
  52. 'test-true' => true,
  53. 'test-0' => 0,
  54. 'test-0-string' => '0',
  55. 'test-empty-string' => '',
  56. ]);
  57. $this->assertSame('test-string="ok" test-true="test-true" test-0="0" test-0-string="0" test-empty-string=""', (string) $bag);
  58. }
  59. }