예제 #1
0
 public function testRead()
 {
     $data = [['id' => 1, 'name' => 'foo', 'group' => 'admin'], ['id' => 2, 'name' => 'bar', 'group' => 'user'], ['id' => 3, 'name' => 'foobar', 'group' => 'user'], ['id' => 4, 'name' => 'foo', 'group' => 'admin'], ['id' => 5, 'name' => 'baz', 'group' => 'user'], ['id' => 6, 'name' => 'foobaz', 'group' => 'user']];
     // Select all
     $source = new ArrayDataSource($data);
     $selection = new ReadSelectionBuilder($source);
     $this->assertEquals(6, count($selection->toArray()));
     $this->assertEquals($data, $selection->toArray());
     // Select with a predicate
     $source = new ArrayDataSource($data);
     $selection = new ReadSelectionBuilder($source);
     $selection = $selection->where('group = %s', 'user');
     $this->assertEquals(4, count($selection->toArray()));
     $this->assertNotContains($data[0], $selection->toArray());
     $this->assertContains($data[1], $selection->toArray());
     // Select with groups
     $source = new ArrayDataSource($data);
     $selection = new ReadSelectionBuilder($source);
     $selection = $selection->groupBy(['group']);
     $this->assertEquals(2, count($selection->toArray()));
     $source = new ArrayDataSource($data);
     $selection = new ReadSelectionBuilder($source);
     $selection = $selection->groupBy(['group', 'name'], E::e('name in %s()', ['foo', 'bar', 'foobar']));
     $this->assertEquals(3, count($selection->toArray()));
     // Select with limit
     $source = new ArrayDataSource($data);
     $selection = new ReadSelectionBuilder($source);
     $selection = $selection->limit(1);
     $this->assertEquals(1, count($selection->toArray()));
     $this->assertContains($data[0], $selection->toArray());
     // Select with projection
     $source = new ArrayDataSource($data);
     $selection = new ReadSelectionBuilder($source);
     $projection = $selection->select(['n' => 'name']);
     $this->assertEquals(6, iterator_count($projection));
     foreach ($projection as $record) {
         $this->assertEquals(['n'], array_keys($record));
     }
 }
예제 #2
0
파일: SqlTableTest.php 프로젝트: jivoo/data
 public function testRead()
 {
     $db = $this->getDb();
     $table = new SqlTable($db, 'Foo');
     // Select all
     $selection = new ReadSelectionBuilder($table);
     $db->expects($this->exactly(7))->method('query')->withConsecutive([$this->equalTo('SELECT {Foo}.* FROM {Foo}')], [$this->equalTo('SELECT {Foo}.* FROM {Foo} WHERE group = "user" ORDER BY name DESC')], [$this->equalTo('SELECT {Foo}.* FROM {Foo} GROUP BY group, name HAVING name IN ("foo", "bar", "foobar")')], [$this->equalTo('SELECT {Foo}.* FROM {Foo} LIMIT 10 OFFSET 5')], [$this->equalTo('SELECT f.* FROM {Foo} AS f LEFT JOIN {Foo} AS o ON f.id = o.id')], [$this->equalTo('SELECT DISTINCT a FROM {Foo}')], [$this->equalTo('SELECT {Foo}.*, 2 + 2 AS ans FROM {Foo}')])->willReturn($this->getMockBuilder('Jivoo\\Data\\Database\\ResultSet')->getMock());
     $selection->toArray();
     // Select with a predicate and ordering
     $selection->where('group = "user"')->orderByDescending('name')->toArray();
     // Select with groups
     $selection->groupBy(['group', 'name'], E::e('name IN %s()', ['foo', 'bar', 'foobar']))->toArray();
     // Select with limit and offset
     $selection->limit(10)->offset(5)->toArray();
     // Select with join and alias
     $selection->alias('f')->leftJoin($table, 'f.id = o.id', 'o')->toArray();
     // Select with projection and distinct
     $selection->distinct()->select('a');
     // Select with additional fields
     $selection->with('ans', '2 + 2')->toArray();
 }
예제 #3
0
 /**
  * Convert selection to an array.
  *
  * @return Record[] Array of records.
  */
 public function toArray()
 {
     $selection = new ReadSelectionBuilder($this->getSource());
     return $selection->toArray();
 }