You are here

class ArrayCollectionTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/doctrine/collections/tests/Doctrine/Tests/Common/Collections/ArrayCollectionTest.php \Doctrine\Tests\Common\Collections\ArrayCollectionTest

Tests for { @covers \Doctrine\Common\Collections\ArrayCollection

Hierarchy

  • class \Doctrine\Tests\Common\Collections\ArrayCollectionTest extends \Doctrine\Tests\Common\Collections\PHPUnit_Framework_TestCase

Expanded class hierarchy of ArrayCollectionTest

See also

\Doctrine\Common\Collections\ArrayCollection}

File

vendor/doctrine/collections/tests/Doctrine/Tests/Common/Collections/ArrayCollectionTest.php, line 30

Namespace

Doctrine\Tests\Common\Collections
View source
class ArrayCollectionTest extends \PHPUnit_Framework_TestCase {

  /**
   * @dataProvider provideDifferentElements
   */
  public function testToArray($elements) {
    $collection = new ArrayCollection($elements);
    $this
      ->assertSame($elements, $collection
      ->toArray());
  }

  /**
   * @dataProvider provideDifferentElements
   */
  public function testFirst($elements) {
    $collection = new ArrayCollection($elements);
    $this
      ->assertSame(reset($elements), $collection
      ->first());
  }

  /**
   * @dataProvider provideDifferentElements
   */
  public function testLast($elements) {
    $collection = new ArrayCollection($elements);
    $this
      ->assertSame(end($elements), $collection
      ->last());
  }

  /**
   * @dataProvider provideDifferentElements
   */
  public function testKey($elements) {
    $collection = new ArrayCollection($elements);
    $this
      ->assertSame(key($elements), $collection
      ->key());
    next($elements);
    $collection
      ->next();
    $this
      ->assertSame(key($elements), $collection
      ->key());
  }

  /**
   * @dataProvider provideDifferentElements
   */
  public function testNext($elements) {
    $collection = new ArrayCollection($elements);
    while (true) {
      $collectionNext = $collection
        ->next();
      $arrayNext = next($elements);
      if (!$collectionNext || !$arrayNext) {
        break;
      }
      $this
        ->assertSame($arrayNext, $collectionNext, "Returned value of ArrayCollection::next() and next() not match");
      $this
        ->assertSame(key($elements), $collection
        ->key(), "Keys not match");
      $this
        ->assertSame(current($elements), $collection
        ->current(), "Current values not match");
    }
  }

  /**
   * @dataProvider provideDifferentElements
   */
  public function testCurrent($elements) {
    $collection = new ArrayCollection($elements);
    $this
      ->assertSame(current($elements), $collection
      ->current());
    next($elements);
    $collection
      ->next();
    $this
      ->assertSame(current($elements), $collection
      ->current());
  }

  /**
   * @dataProvider provideDifferentElements
   */
  public function testGetKeys($elements) {
    $collection = new ArrayCollection($elements);
    $this
      ->assertSame(array_keys($elements), $collection
      ->getKeys());
  }

  /**
   * @dataProvider provideDifferentElements
   */
  public function testGetValues($elements) {
    $collection = new ArrayCollection($elements);
    $this
      ->assertSame(array_values($elements), $collection
      ->getValues());
  }

  /**
   * @dataProvider provideDifferentElements
   */
  public function testCount($elements) {
    $collection = new ArrayCollection($elements);
    $this
      ->assertSame(count($elements), $collection
      ->count());
  }

  /**
   * @dataProvider provideDifferentElements
   */
  public function testIterator($elements) {
    $collection = new ArrayCollection($elements);
    $iterations = 0;
    foreach ($collection
      ->getIterator() as $key => $item) {
      $this
        ->assertSame($elements[$key], $item, "Item {$key} not match");
      $iterations++;
    }
    $this
      ->assertEquals(count($elements), $iterations, "Number of iterations not match");
  }

  /**
   * @return array
   */
  public function provideDifferentElements() {
    return array(
      'indexed' => array(
        array(
          1,
          2,
          3,
          4,
          5,
        ),
      ),
      'associative' => array(
        array(
          'A' => 'a',
          'B' => 'b',
          'C' => 'c',
        ),
      ),
      'mixed' => array(
        array(
          'A' => 'a',
          1,
          'B' => 'b',
          2,
          3,
        ),
      ),
    );
  }
  public function testRemove() {
    $elements = array(
      1,
      'A' => 'a',
      2,
      'B' => 'b',
      3,
    );
    $collection = new ArrayCollection($elements);
    $this
      ->assertEquals(1, $collection
      ->remove(0));
    unset($elements[0]);
    $this
      ->assertEquals(null, $collection
      ->remove('non-existent'));
    unset($elements['non-existent']);
    $this
      ->assertEquals(2, $collection
      ->remove(1));
    unset($elements[1]);
    $this
      ->assertEquals('a', $collection
      ->remove('A'));
    unset($elements['A']);
    $this
      ->assertEquals($elements, $collection
      ->toArray());
  }
  public function testRemoveElement() {
    $elements = array(
      1,
      'A' => 'a',
      2,
      'B' => 'b',
      3,
      'A2' => 'a',
      'B2' => 'b',
    );
    $collection = new ArrayCollection($elements);
    $this
      ->assertTrue($collection
      ->removeElement(1));
    unset($elements[0]);
    $this
      ->assertFalse($collection
      ->removeElement('non-existent'));
    $this
      ->assertTrue($collection
      ->removeElement('a'));
    unset($elements['A']);
    $this
      ->assertTrue($collection
      ->removeElement('a'));
    unset($elements['A2']);
    $this
      ->assertEquals($elements, $collection
      ->toArray());
  }
  public function testContainsKey() {
    $elements = array(
      1,
      'A' => 'a',
      2,
      'null' => null,
      3,
      'A2' => 'a',
      'B2' => 'b',
    );
    $collection = new ArrayCollection($elements);
    $this
      ->assertTrue($collection
      ->containsKey(0), "Contains index 0");
    $this
      ->assertTrue($collection
      ->containsKey('A'), "Contains key \"A\"");
    $this
      ->assertTrue($collection
      ->containsKey('null'), "Contains key \"null\", with value null");
    $this
      ->assertFalse($collection
      ->containsKey('non-existent'), "Doesn't contain key");
  }
  public function testEmpty() {
    $collection = new ArrayCollection();
    $this
      ->assertTrue($collection
      ->isEmpty(), "Empty collection");
    $collection
      ->add(1);
    $this
      ->assertFalse($collection
      ->isEmpty(), "Not empty collection");
  }
  public function testContains() {
    $elements = array(
      1,
      'A' => 'a',
      2,
      'null' => null,
      3,
      'A2' => 'a',
      'zero' => 0,
    );
    $collection = new ArrayCollection($elements);
    $this
      ->assertTrue($collection
      ->contains(0), "Contains Zero");
    $this
      ->assertTrue($collection
      ->contains('a'), "Contains \"a\"");
    $this
      ->assertTrue($collection
      ->contains(null), "Contains Null");
    $this
      ->assertFalse($collection
      ->contains('non-existent'), "Doesn't contain an element");
  }
  public function testExists() {
    $elements = array(
      1,
      'A' => 'a',
      2,
      'null' => null,
      3,
      'A2' => 'a',
      'zero' => 0,
    );
    $collection = new ArrayCollection($elements);
    $this
      ->assertTrue($collection
      ->exists(function ($key, $element) {
      return $key == 'A' && $element == 'a';
    }), "Element exists");
    $this
      ->assertFalse($collection
      ->exists(function ($key, $element) {
      return $key == 'non-existent' && $element == 'non-existent';
    }), "Element not exists");
  }
  public function testIndexOf() {
    $elements = array(
      1,
      'A' => 'a',
      2,
      'null' => null,
      3,
      'A2' => 'a',
      'zero' => 0,
    );
    $collection = new ArrayCollection($elements);
    $this
      ->assertSame(array_search(2, $elements, true), $collection
      ->indexOf(2), 'Index of 2');
    $this
      ->assertSame(array_search(null, $elements, true), $collection
      ->indexOf(null), 'Index of null');
    $this
      ->assertSame(array_search('non-existent', $elements, true), $collection
      ->indexOf('non-existent'), 'Index of non existent');
  }
  public function testGet() {
    $elements = array(
      1,
      'A' => 'a',
      2,
      'null' => null,
      3,
      'A2' => 'a',
      'zero' => 0,
    );
    $collection = new ArrayCollection($elements);
    $this
      ->assertSame(2, $collection
      ->get(1), 'Get element by index');
    $this
      ->assertSame('a', $collection
      ->get('A'), 'Get element by name');
    $this
      ->assertSame(null, $collection
      ->get('non-existent'), 'Get non existent element');
  }
  public function testMatchingWithSortingPreservesyKeys() {
    $object1 = new \stdClass();
    $object2 = new \stdClass();
    $object1->sortField = 2;
    $object2->sortField = 1;
    $collection = new ArrayCollection(array(
      'object1' => $object1,
      'object2' => $object2,
    ));
    $this
      ->assertSame(array(
      'object2' => $object2,
      'object1' => $object1,
    ), $collection
      ->matching(new Criteria(null, array(
      'sortField' => Criteria::ASC,
    )))
      ->toArray());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ArrayCollectionTest::provideDifferentElements public function
ArrayCollectionTest::testContains public function
ArrayCollectionTest::testContainsKey public function
ArrayCollectionTest::testCount public function @dataProvider provideDifferentElements
ArrayCollectionTest::testCurrent public function @dataProvider provideDifferentElements
ArrayCollectionTest::testEmpty public function
ArrayCollectionTest::testExists public function
ArrayCollectionTest::testFirst public function @dataProvider provideDifferentElements
ArrayCollectionTest::testGet public function
ArrayCollectionTest::testGetKeys public function @dataProvider provideDifferentElements
ArrayCollectionTest::testGetValues public function @dataProvider provideDifferentElements
ArrayCollectionTest::testIndexOf public function
ArrayCollectionTest::testIterator public function @dataProvider provideDifferentElements
ArrayCollectionTest::testKey public function @dataProvider provideDifferentElements
ArrayCollectionTest::testLast public function @dataProvider provideDifferentElements
ArrayCollectionTest::testMatchingWithSortingPreservesyKeys public function
ArrayCollectionTest::testNext public function @dataProvider provideDifferentElements
ArrayCollectionTest::testRemove public function
ArrayCollectionTest::testRemoveElement public function
ArrayCollectionTest::testToArray public function @dataProvider provideDifferentElements