Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from tightenco/5.2.36-release
Browse files Browse the repository at this point in the history
5.2.36 release changes
  • Loading branch information
mattstauffer authored Jun 9, 2016
2 parents d2a4e57 + 8e80609 commit fded841
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 30 deletions.
5 changes: 2 additions & 3 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ public function keyBy($keyBy)

$results = [];

foreach ($this->items as $item) {
$results[$keyBy($item)] = $item;
foreach ($this->items as $key => $item) {
$results[$keyBy($item, $key)] = $item;
}

return new static($results);
Expand Down Expand Up @@ -817,7 +817,6 @@ public function sort(callable $callback = null)
$items = $this->items;

$callback ? uasort($items, $callback) : uasort($items, function ($a, $b) {

if ($a == $b) {
return 0;
}
Expand Down
96 changes: 69 additions & 27 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ public function testLastReturnsLastItemInCollection()
public function testLastWithCallback()
{
$data = new Collection([2, 4, 3, 2]);
$result = $data->last(function ($key, $value) { return $value > 2; });
$result = $data->last(function ($key, $value) {
return $value > 2;
});
$this->assertEquals(3, $result);
}

public function testLastWithCallbackAndDefault()
{
$data = new Collection(['foo', 'bar']);
$result = $data->last(function ($key, $value) { return $value === 'baz'; }, 'default');
$result = $data->last(function ($key, $value) {
return $value === 'baz';
}, 'default');
$this->assertEquals('default', $result);
}

Expand Down Expand Up @@ -466,7 +470,9 @@ public function testUniqueWithCallback()
1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'],
3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell'],
5 => ['id' => 5, 'first' => 'Taylor', 'last' => 'Swift'],
], $c->unique(function ($item) { return $item['first'].$item['last']; })->all());
], $c->unique(function ($item) {
return $item['first'].$item['last'];
})->all());
}

public function testCollapse()
Expand Down Expand Up @@ -509,12 +515,16 @@ public function testSortWithCallback()
public function testSortBy()
{
$data = new Collection(['taylor', 'dayle']);
$data = $data->sortBy(function ($x) { return $x; });
$data = $data->sortBy(function ($x) {
return $x;
});

$this->assertEquals(['dayle', 'taylor'], array_values($data->all()));

$data = new Collection(['dayle', 'taylor']);
$data = $data->sortByDesc(function ($x) { return $x; });
$data = $data->sortByDesc(function ($x) {
return $x;
});

$this->assertEquals(['taylor', 'dayle'], array_values($data->all()));
}
Expand Down Expand Up @@ -655,8 +665,8 @@ public function testMacroable()
// Foo() macro : unique values starting with A
Collection::macro('foo', function () {
return $this->filter(function ($item) {
return strpos($item, 'a') === 0;
})
return strpos($item, 'a') === 0;
})
->unique()
->values();
});
Expand Down Expand Up @@ -770,7 +780,9 @@ public function testGetPluckValueWithAccessors()
public function testMap()
{
$data = new Collection(['first' => 'taylor', 'last' => 'otwell']);
$data = $data->map(function ($item, $key) { return $key.'-'.strrev($item); });
$data = $data->map(function ($item, $key) {
return $key.'-'.strrev($item);
});
$this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $data->all());
}

Expand All @@ -780,28 +792,36 @@ public function testFlatMap()
['name' => 'taylor', 'hobbies' => ['programming', 'basketball']],
['name' => 'adam', 'hobbies' => ['music', 'powerlifting']],
]);
$data = $data->flatMap(function ($person) { return $person['hobbies']; });
$data = $data->flatMap(function ($person) {
return $person['hobbies'];
});
$this->assertEquals(['programming', 'basketball', 'music', 'powerlifting'], $data->all());
}

public function testTransform()
{
$data = new Collection(['first' => 'taylor', 'last' => 'otwell']);
$data->transform(function ($item, $key) { return $key.'-'.strrev($item); });
$data->transform(function ($item, $key) {
return $key.'-'.strrev($item);
});
$this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $data->all());
}

public function testFirstWithCallback()
{
$data = new Collection(['foo', 'bar', 'baz']);
$result = $data->first(function ($key, $value) { return $value === 'bar'; });
$result = $data->first(function ($key, $value) {
return $value === 'bar';
});
$this->assertEquals('bar', $result);
}

public function testFirstWithCallbackAndDefault()
{
$data = new Collection(['foo', 'bar']);
$result = $data->first(function ($key, $value) { return $value === 'baz'; }, 'default');
$result = $data->first(function ($key, $value) {
return $value === 'baz';
}, 'default');
$this->assertEquals('default', $result);
}

Expand Down Expand Up @@ -929,7 +949,9 @@ public function testKeyByAttribute()
$result = $data->keyBy('rating');
$this->assertEquals([1 => ['rating' => 1, 'name' => '1'], 2 => ['rating' => 2, 'name' => '2'], 3 => ['rating' => 3, 'name' => '3']], $result->all());

$result = $data->keyBy(function ($item) { return $item['rating'] * 2; });
$result = $data->keyBy(function ($item) {
return $item['rating'] * 2;
});
$this->assertEquals([2 => ['rating' => 1, 'name' => '1'], 4 => ['rating' => 2, 'name' => '2'], 6 => ['rating' => 3, 'name' => '3']], $result->all());
}

Expand All @@ -939,12 +961,12 @@ public function testKeyByClosure()
['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'],
['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'],
]);
$result = $data->keyBy(function ($item) {
return strtolower($item['firstname'].$item['lastname']);
$result = $data->keyBy(function ($item, $key) {
return strtolower($key.'-'.$item['firstname'].$item['lastname']);
});
$this->assertEquals([
'taylorotwell' => ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'],
'lucasmichot' => ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'],
'0-taylorotwell' => ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'],
'1-lucasmichot' => ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'],
], $result->all());
}

Expand All @@ -954,8 +976,12 @@ public function testContains()

$this->assertTrue($c->contains(1));
$this->assertFalse($c->contains(2));
$this->assertTrue($c->contains(function ($value) { return $value < 5; }));
$this->assertFalse($c->contains(function ($value) { return $value > 5; }));
$this->assertTrue($c->contains(function ($value) {
return $value < 5;
}));
$this->assertFalse($c->contains(function ($value) {
return $value > 5;
}));

$c = new Collection([['v' => 1], ['v' => 3], ['v' => 5]]);

Expand All @@ -975,7 +1001,9 @@ public function testGettingSumFromCollection()
$this->assertEquals(100, $c->sum('foo'));

$c = new Collection([(object) ['foo' => 50], (object) ['foo' => 50]]);
$this->assertEquals(100, $c->sum(function ($i) { return $i->foo; }));
$this->assertEquals(100, $c->sum(function ($i) {
return $i->foo;
}));
}

public function testCanSumValuesWithoutACallback()
Expand Down Expand Up @@ -1027,7 +1055,9 @@ public function testRejectRemovesElementsPassingTruthTest()
$this->assertEquals(['foo'], $c->reject('bar')->values()->all());

$c = new Collection(['foo', 'bar']);
$this->assertEquals(['foo'], $c->reject(function ($v) { return $v == 'bar'; })->values()->all());
$this->assertEquals(['foo'], $c->reject(function ($v) {
return $v == 'bar';
})->values()->all());

$c = new Collection(['foo', null]);
$this->assertEquals(['foo'], $c->reject(null)->values()->all());
Expand All @@ -1036,7 +1066,9 @@ public function testRejectRemovesElementsPassingTruthTest()
$this->assertEquals(['foo', 'bar'], $c->reject('baz')->values()->all());

$c = new Collection(['foo', 'bar']);
$this->assertEquals(['foo', 'bar'], $c->reject(function ($v) { return $v == 'baz'; })->values()->all());
$this->assertEquals(['foo', 'bar'], $c->reject(function ($v) {
return $v == 'baz';
})->values()->all());

$c = new Collection(['id' => 1, 'primary' => 'foo', 'secondary' => 'bar']);
$this->assertEquals(['primary' => 'foo', 'secondary' => 'bar'], $c->reject(function ($item, $key) {
Expand All @@ -1050,8 +1082,12 @@ public function testSearchReturnsIndexOfFirstFoundItem()

$this->assertEquals(1, $c->search(2));
$this->assertEquals('foo', $c->search('bar'));
$this->assertEquals(4, $c->search(function ($value) { return $value > 4; }));
$this->assertEquals('foo', $c->search(function ($value) { return ! is_numeric($value); }));
$this->assertEquals(4, $c->search(function ($value) {
return $value > 4;
}));
$this->assertEquals('foo', $c->search(function ($value) {
return ! is_numeric($value);
}));
}

public function testSearchReturnsFalseWhenItemIsNotFound()
Expand All @@ -1060,8 +1096,12 @@ public function testSearchReturnsFalseWhenItemIsNotFound()

$this->assertFalse($c->search(6));
$this->assertFalse($c->search('foo'));
$this->assertFalse($c->search(function ($value) { return $value < 1 && is_numeric($value); }));
$this->assertFalse($c->search(function ($value) { return $value == 'nope'; }));
$this->assertFalse($c->search(function ($value) {
return $value < 1 && is_numeric($value);
}));
$this->assertFalse($c->search(function ($value) {
return $value == 'nope';
}));
}

public function testKeys()
Expand Down Expand Up @@ -1220,7 +1260,9 @@ public function testCombineWithCollection()
public function testReduce()
{
$data = new Collection([1, 2, 3]);
$this->assertEquals(6, $data->reduce(function ($carry, $element) { return $carry += $element; }));
$this->assertEquals(6, $data->reduce(function ($carry, $element) {
return $carry += $element;
}));
}

/**
Expand Down

0 comments on commit fded841

Please sign in to comment.