SupportFluentTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use ArrayIterator;
  4. use Illuminate\Support\Fluent;
  5. use IteratorAggregate;
  6. use PHPUnit\Framework\TestCase;
  7. use ReflectionObject;
  8. class SupportFluentTest extends TestCase
  9. {
  10. public function testAttributesAreSetByConstructor()
  11. {
  12. $array = ['name' => 'Taylor', 'age' => 25];
  13. $fluent = new Fluent($array);
  14. $refl = new ReflectionObject($fluent);
  15. $attributes = $refl->getProperty('attributes');
  16. $attributes->setAccessible(true);
  17. $this->assertEquals($array, $attributes->getValue($fluent));
  18. $this->assertEquals($array, $fluent->getAttributes());
  19. }
  20. public function testAttributesAreSetByConstructorGivenstdClass()
  21. {
  22. $array = ['name' => 'Taylor', 'age' => 25];
  23. $fluent = new Fluent((object) $array);
  24. $refl = new ReflectionObject($fluent);
  25. $attributes = $refl->getProperty('attributes');
  26. $attributes->setAccessible(true);
  27. $this->assertEquals($array, $attributes->getValue($fluent));
  28. $this->assertEquals($array, $fluent->getAttributes());
  29. }
  30. public function testAttributesAreSetByConstructorGivenArrayIterator()
  31. {
  32. $array = ['name' => 'Taylor', 'age' => 25];
  33. $fluent = new Fluent(new FluentArrayIteratorStub($array));
  34. $refl = new ReflectionObject($fluent);
  35. $attributes = $refl->getProperty('attributes');
  36. $attributes->setAccessible(true);
  37. $this->assertEquals($array, $attributes->getValue($fluent));
  38. $this->assertEquals($array, $fluent->getAttributes());
  39. }
  40. public function testGetMethodReturnsAttribute()
  41. {
  42. $fluent = new Fluent(['name' => 'Taylor']);
  43. $this->assertSame('Taylor', $fluent->get('name'));
  44. $this->assertSame('Default', $fluent->get('foo', 'Default'));
  45. $this->assertSame('Taylor', $fluent->name);
  46. $this->assertNull($fluent->foo);
  47. }
  48. public function testArrayAccessToAttributes()
  49. {
  50. $fluent = new Fluent(['attributes' => '1']);
  51. $this->assertTrue(isset($fluent['attributes']));
  52. $this->assertEquals(1, $fluent['attributes']);
  53. $fluent->attributes();
  54. $this->assertTrue($fluent['attributes']);
  55. }
  56. public function testMagicMethodsCanBeUsedToSetAttributes()
  57. {
  58. $fluent = new Fluent;
  59. $fluent->name = 'Taylor';
  60. $fluent->developer();
  61. $fluent->age(25);
  62. $this->assertSame('Taylor', $fluent->name);
  63. $this->assertTrue($fluent->developer);
  64. $this->assertEquals(25, $fluent->age);
  65. $this->assertInstanceOf(Fluent::class, $fluent->programmer());
  66. }
  67. public function testIssetMagicMethod()
  68. {
  69. $array = ['name' => 'Taylor', 'age' => 25];
  70. $fluent = new Fluent($array);
  71. $this->assertTrue(isset($fluent->name));
  72. unset($fluent->name);
  73. $this->assertFalse(isset($fluent->name));
  74. }
  75. public function testToArrayReturnsAttribute()
  76. {
  77. $array = ['name' => 'Taylor', 'age' => 25];
  78. $fluent = new Fluent($array);
  79. $this->assertEquals($array, $fluent->toArray());
  80. }
  81. public function testToJsonEncodesTheToArrayResult()
  82. {
  83. $fluent = $this->getMockBuilder(Fluent::class)->onlyMethods(['toArray'])->getMock();
  84. $fluent->expects($this->once())->method('toArray')->willReturn('foo');
  85. $results = $fluent->toJson();
  86. $this->assertJsonStringEqualsJsonString(json_encode('foo'), $results);
  87. }
  88. }
  89. class FluentArrayIteratorStub implements IteratorAggregate
  90. {
  91. protected $items = [];
  92. public function __construct(array $items = [])
  93. {
  94. $this->items = $items;
  95. }
  96. #[\ReturnTypeWillChange]
  97. public function getIterator()
  98. {
  99. return new ArrayIterator($this->items);
  100. }
  101. }