CollectionTest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Tests;
  3. use think\Collection;
  4. class CollectionTest extends TestCase
  5. {
  6. public function testMerge()
  7. {
  8. $c = new Collection(['name' => 'Hello']);
  9. $this->assertSame(['name' => 'Hello', 'id' => 1], $c->merge(['id' => 1])->all());
  10. }
  11. public function testFirst()
  12. {
  13. $c = new Collection(['name' => 'Hello', 'age' => 25]);
  14. $this->assertSame('Hello', $c->first());
  15. }
  16. public function testLast()
  17. {
  18. $c = new Collection(['name' => 'Hello', 'age' => 25]);
  19. $this->assertSame(25, $c->last());
  20. }
  21. public function testToArray()
  22. {
  23. $c = new Collection(['name' => 'Hello', 'age' => 25]);
  24. $this->assertSame(['name' => 'Hello', 'age' => 25], $c->toArray());
  25. }
  26. public function testToJson()
  27. {
  28. $c = new Collection(['name' => 'Hello', 'age' => 25]);
  29. $this->assertSame(json_encode(['name' => 'Hello', 'age' => 25]), $c->toJson());
  30. $this->assertSame(json_encode(['name' => 'Hello', 'age' => 25]), (string) $c);
  31. $this->assertSame(json_encode(['name' => 'Hello', 'age' => 25]), json_encode($c));
  32. }
  33. public function testSerialize()
  34. {
  35. $c = new Collection(['name' => 'Hello', 'age' => 25]);
  36. $sc = serialize($c);
  37. $c = unserialize($sc);
  38. $this->assertSame(['name' => 'Hello', 'age' => 25], $c->all());
  39. }
  40. public function testGetIterator()
  41. {
  42. $c = new Collection(['name' => 'Hello', 'age' => 25]);
  43. $this->assertInstanceOf(\ArrayIterator::class, $c->getIterator());
  44. $this->assertSame(['name' => 'Hello', 'age' => 25], $c->getIterator()->getArrayCopy());
  45. }
  46. public function testCount()
  47. {
  48. $c = new Collection(['name' => 'Hello', 'age' => 25]);
  49. $this->assertCount(2, $c);
  50. }
  51. }