Exemple #1
0
 public function testFilterBy()
 {
     $a = [['id' => 123, 'name' => 'foo', 'group' => 'primary', 'value' => 123456, 'when' => '2014-01-01'], ['id' => 456, 'name' => 'bar', 'group' => 'primary', 'value' => 1468, 'when' => '2014-07-15'], ['id' => 499, 'name' => 'baz', 'group' => 'secondary', 'value' => 2365, 'when' => '2014-08-23'], ['id' => 789, 'name' => 'ter', 'group' => 'primary', 'value' => 2468, 'when' => '2010-03-01'], ['id' => 888, 'name' => 'qux', 'value' => 6868, 'when' => '2015-01-01'], ['id' => 999, 'name' => 'flux', 'group' => null, 'value' => 6868, 'when' => '2015-01-01']];
     $b = Arr::filterBy($a, 'name', 'baz');
     $this->assertCount(1, $b);
     $this->assertEquals(2365, $b[0]['value']);
     $b = Arr::filterBy($a, 'name', ['baz']);
     $this->assertCount(1, $b);
     $this->assertEquals(2365, $b[0]['value']);
     $c = Arr::filterBy($a, 'value', 2468);
     $this->assertCount(1, $c);
     $this->assertEquals('primary', $c[0]['group']);
     $d = Arr::filterBy($a, 'group', 'primary');
     $this->assertCount(3, $d);
     $e = Arr::filterBy($a, 'value', 2000, 'lt');
     $this->assertCount(1, $e);
     $this->assertEquals(1468, $e[0]['value']);
     $e = Arr::filterBy($a, 'value', [2468, 2365], 'contains');
     $this->assertCount(2, $e);
     $this->assertContains(2468, Arr::pluck($e, 'value'));
     $this->assertNotContains(1468, Arr::pluck($e, 'value'));
     $e = Arr::filterBy($a, 'when', '2014-02-01', 'older');
     $this->assertCount(2, $e);
     $this->assertContains('2014-01-01', Arr::pluck($e, 'when'));
     $this->assertContains('2010-03-01', Arr::pluck($e, 'when'));
     $this->assertNotContains('2014-08-23', Arr::pluck($e, 'when'));
     $f = Arr::filterBy($a, 'group', 'primary', 'ne');
     $this->assertCount(3, $f, 'Count should pick up groups which are explicitly set as null AND those which don\'t have the property at all');
     $this->assertContains('qux', Arr::pluck($f, 'name'));
     $this->assertContains('flux', Arr::pluck($f, 'name'));
 }