CollectionTest.php 4.61 KB
Newer Older
1 2
<?php

3
namespace Doctrine\Tests\Common\Collections;
4

5 6
use Doctrine\Tests;

7
require_once __DIR__ . '/../../TestInit.php';
8

9 10
class CollectionTest extends \Doctrine\Tests\DoctrineTestCase
{
11 12
    private $_coll;

13 14
    protected function setUp()
    {
15
        $this->_coll = new \Doctrine\Common\Collections\ArrayCollection;
16 17
    }

jwage's avatar
jwage committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    public function testIssetAndUnset()
    {
        $this->assertFalse(isset($this->_coll[0]));
        $this->_coll->add('testing');
        $this->assertTrue(isset($this->_coll[0]));
        unset($this->_coll[0]);
        $this->assertFalse(isset($this->_coll[0]));
    }

    public function testToString()
    {
        $this->_coll->add('testing');
        $this->assertTrue(is_string((string) $this->_coll));
    }

    public function testRemovingNonExistentEntryReturnsNull()
    {
        $this->assertEquals(null, $this->_coll->remove('testing_does_not_exist'));
    }

38 39
    public function testExists()
    {
40 41
        $this->_coll->add("one");
        $this->_coll->add("two");
romanb's avatar
romanb committed
42
        $exists = $this->_coll->exists(function($k, $e) { return $e == "one"; });
43
        $this->assertTrue($exists);
romanb's avatar
romanb committed
44
        $exists = $this->_coll->exists(function($k, $e) { return $e == "other"; });
45 46 47
        $this->assertFalse($exists);
    }

48 49
    public function testMap()
    {
50 51
        $this->_coll->add(1);
        $this->_coll->add(2);
romanb's avatar
romanb committed
52
        $res = $this->_coll->map(function($e) { return $e * 2; });
53
        $this->assertEquals(array(2, 4), $res->toArray());
54 55
    }

56 57
    public function testFilter()
    {
58 59 60
        $this->_coll->add(1);
        $this->_coll->add("foo");
        $this->_coll->add(3);
romanb's avatar
romanb committed
61
        $res = $this->_coll->filter(function($e) { return is_numeric($e); });
62
        $this->assertEquals(array(0 => 1, 2 => 3), $res->toArray());
63
    }
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    public function testFirstAndLast()
    {
        $this->_coll->add('one');
        $this->_coll->add('two');

        $this->assertEquals($this->_coll->first(), 'one');
        $this->assertEquals($this->_coll->last(), 'two');
    }

    public function testArrayAccess()
    {
        $this->_coll[] = 'one';
        $this->_coll[] = 'two';

        $this->assertEquals($this->_coll[0], 'one');
        $this->assertEquals($this->_coll[1], 'two');

        unset($this->_coll[0]);
        $this->assertEquals($this->_coll->count(), 1);
    }

    public function testContainsKey()
    {
        $this->_coll[5] = 'five';
romanb's avatar
romanb committed
89
        $this->assertTrue($this->_coll->containsKey(5));
90 91 92 93 94
    }

    public function testContains()
    {
        $this->_coll[0] = 'test';
romanb's avatar
romanb committed
95
        $this->assertTrue($this->_coll->contains('test'));
96 97 98 99 100
    }

    public function testSearch()
    {
        $this->_coll[0] = 'test';
romanb's avatar
romanb committed
101
        $this->assertEquals(0, $this->_coll->search('test'));
102 103 104 105 106
    }

    public function testGet()
    {
        $this->_coll[0] = 'test';
romanb's avatar
romanb committed
107
        $this->assertEquals('test', $this->_coll->get(0));
108 109 110 111 112 113
    }

    public function testGetKeys()
    {
        $this->_coll[] = 'one';
        $this->_coll[] = 'two';
romanb's avatar
romanb committed
114
        $this->assertEquals(array(0, 1), $this->_coll->getKeys());
115 116
    }

117
    public function testGetValues()
118 119 120
    {
        $this->_coll[] = 'one';
        $this->_coll[] = 'two';
121
        $this->assertEquals(array('one', 'two'), $this->_coll->getValues());
122 123 124 125 126 127 128 129 130 131 132 133 134 135
    }

    public function testCount()
    {
        $this->_coll[] = 'one';
        $this->_coll[] = 'two';
        $this->assertEquals($this->_coll->count(), 2);
        $this->assertEquals(count($this->_coll), 2);
    }

    public function testForAll()
    {
        $this->_coll[] = 'one';
        $this->_coll[] = 'two';
romanb's avatar
romanb committed
136 137
        $this->assertEquals($this->_coll->forAll(function($k, $e) { return is_string($e); }), true);
        $this->assertEquals($this->_coll->forAll(function($k, $e) { return is_array($e); }), false);
138 139 140 141 142 143
    }

    public function testPartition()
    {
        $this->_coll[] = true;
        $this->_coll[] = false;
romanb's avatar
romanb committed
144
        $partition = $this->_coll->partition(function($k, $e) { return $e == true; });
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
        $this->assertEquals($partition[0][0], true);
        $this->assertEquals($partition[1][0], false);
    }

    public function testClear()
    {
        $this->_coll[] = 'one';
        $this->_coll[] = 'two';
        $this->_coll->clear();
        $this->assertEquals($this->_coll->isEmpty(), true);
    }

    public function testRemove()
    {
        $this->_coll[] = 'one';
        $this->_coll[] = 'two';
        $this->_coll->remove(0);
        $this->assertEquals($this->_coll->contains('one'), false);
    }

    public function testRemoveElement()
    {
        $this->_coll[] = 'one';
        $this->_coll[] = 'two';
        $this->_coll->removeElement('two');
        $this->assertEquals($this->_coll->contains('two'), false);
    }
}