ArrayCollectionTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace Elgg\Structs;
  3. use PHPUnit_Framework_TestCase as TestCase;
  4. class ArrayCollectionTest extends TestCase {
  5. public function testCountIsAccurate() {
  6. $zeroItems = new ArrayCollection();
  7. $this->assertEquals(0, count($zeroItems));
  8. $oneItem = new ArrayCollection(array('one'));
  9. $this->assertEquals(1, count($oneItem));
  10. $twoItems = new ArrayCollection(array('one', 'two'));
  11. $this->assertEquals(2, count($twoItems));
  12. }
  13. public function testContainsDoesNotImplicitlyCastSimilarValues() {
  14. $collection = new ArrayCollection(array('1', false));
  15. $this->assertTrue($collection->contains('1'));
  16. $this->assertTrue($collection->contains(false));
  17. $this->assertFalse($collection->contains(1));
  18. $this->assertFalse($collection->contains(0));
  19. $this->assertFalse($collection->contains(''));
  20. }
  21. public function testIsTraversable() {
  22. $collection = new ArrayCollection(array('one', 'two', 'three'));
  23. $items = array();
  24. foreach ($collection as $item) {
  25. $items[] = $item;
  26. }
  27. $this->assertEquals(array('one', 'two', 'three'), $items);
  28. }
  29. public function testIsFilterable() {
  30. $collection = new ArrayCollection(array(0, 1, 2, 3, 4));
  31. $filtered = $collection->filter(function($number) {
  32. return $number > 2;
  33. });
  34. $this->assertFalse($filtered->contains(0));
  35. $this->assertFalse($filtered->contains(1));
  36. $this->assertFalse($filtered->contains(2));
  37. $this->assertTrue($filtered->contains(3));
  38. $this->assertTrue($filtered->contains(4));
  39. $this->assertEquals(2, count($filtered));
  40. $this->assertNotSame($filtered, $collection);
  41. }
  42. public function testIsMappable() {
  43. $collection = new ArrayCollection(array(0, 1, 2, 3, 4));
  44. $mapped = $collection->map(function($number) {
  45. return $number * 2;
  46. });
  47. $this->assertTrue($mapped->contains(0));
  48. $this->assertTrue($mapped->contains(2));
  49. $this->assertTrue($mapped->contains(4));
  50. $this->assertTrue($mapped->contains(6));
  51. $this->assertTrue($mapped->contains(8));
  52. $this->assertEquals(5, count($mapped));
  53. $this->assertNotSame($mapped, $collection);
  54. }
  55. }