Skip to content

Commit

Permalink
Merge pull request #8 from ARCANEDEV/patch-collection
Browse files Browse the repository at this point in the history
Updating Collection & Adding Laravel 5.3 Support
  • Loading branch information
arcanedev-maroc authored Sep 4, 2016
2 parents 197ad60 + c174ef5 commit daa8c5a
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 28 deletions.
10 changes: 4 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
.idea/
_arcanedev/
build/
vendor/
composer.phar
composer.lock
/build/
/vendor/
/composer.phar
/composer.lock
4 changes: 2 additions & 2 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ checks:

tools:
external_code_coverage:
timeout: 600
runs: 8
timeout: 900
runs: 9
php_code_sniffer:
enabled: true
config:
Expand Down
9 changes: 4 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ language: php
sudo: false

php:
- 5.5.9
- 5.5
- 5.6
- 7.0
- 7.1
- nightly
- hhvm

matrix:
allow_failures:
Expand All @@ -17,6 +15,7 @@ matrix:
env:
- TESTBENCH_VERSION=3.1.*
- TESTBENCH_VERSION=3.2.*
- TESTBENCH_VERSION=3.3.*

before_script:
- travis_retry composer self-update
Expand All @@ -28,5 +27,5 @@ script:
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- if [ "$TRAVIS_PHP_VERSION" != "nightly" ] && [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [ "$TRAVIS_PHP_VERSION" != "nightly" ] && [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
- if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Support [![Packagist License][badge_license]](LICENSE.md) [![For Laravel 5][badge_laravel]][link-github-repo]

[![Travis Status][badge_build]][link-travis]
[![HHVM Status][badge_hhvm]][link-hhvm]
[![Coverage Status][badge_coverage]][link-scrutinizer]
[![Scrutinizer Code Quality][badge_quality]][link-scrutinizer]
[![SensioLabs Insight][badge_insight]][link-insight]
Expand Down Expand Up @@ -31,9 +30,8 @@ If you discover any security related issues, please email arcanedev.maroc@gmail.
- [All Contributors][link-contributors]

[badge_license]: http://img.shields.io/packagist/l/arcanedev/support.svg?style=flat-square
[badge_laravel]: https://img.shields.io/badge/For-Laravel%205.1|5.2-orange.svg?style=flat-square
[badge_laravel]: https://img.shields.io/badge/For-Laravel%205.1|5.2|5.3-orange.svg?style=flat-square
[badge_build]: http://img.shields.io/travis/ARCANEDEV/Support.svg?style=flat-square
[badge_hhvm]: https://img.shields.io/hhvm/arcanedev/support.svg?style=flat-square
[badge_coverage]: https://img.shields.io/scrutinizer/coverage/g/ARCANEDEV/Support.svg?style=flat-square
[badge_quality]: https://img.shields.io/scrutinizer/g/ARCANEDEV/Support.svg?style=flat-square
[badge_insight]: https://img.shields.io/sensiolabs/i/de0353dd-df17-4656-b9c0-1eea95aa30a2.svg?style=flat-square
Expand All @@ -48,6 +46,5 @@ If you discover any security related issues, please email arcanedev.maroc@gmail.
[link-contributors]: https://github.com/ARCANEDEV/Support/graphs/contributors
[link-packagist]: https://packagist.org/packages/arcanedev/support
[link-travis]: https://travis-ci.org/ARCANEDEV/Support
[link-hhvm]: http://hhvm.h4cc.de/package/arcanedev/support
[link-scrutinizer]: https://scrutinizer-ci.com/g/ARCANEDEV/Support/?branch=master
[link-insight]: https://insight.sensiolabs.com/projects/de0353dd-df17-4656-b9c0-1eea95aa30a2
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"type": "library",
"license": "MIT",
"require": {
"php": ">=5.5.9",
"php": ">=5.6",
"illuminate/filesystem": "~5.0",
"illuminate/support": "~5.0"
},
Expand Down
36 changes: 36 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,42 @@ class Collection extends IlluminateCollection
| Custom Functions
| ------------------------------------------------------------------------------------------------
*/
/**
* Return only unique items from the collection array.
*
* @param string|callable|null $key
* @param bool $strict
*
* @return static
*/
public function unique($key = null, $strict = false)
{
if (is_null($key))
return new static(array_unique($this->items, SORT_REGULAR));

$key = $this->valueRetriever($key);
$exists = [];

return $this->reject(function ($item) use ($key, $strict, &$exists) {
if (in_array($id = $key($item), $exists, $strict)) {
return true;
}
$exists[] = $id;
});
}

/**
* Return only unique items from the collection array using strict comparison.
*
* @param string|callable|null $key
*
* @return static
*/
public function uniqueStrict($key = null)
{
return $this->unique($key, true);
}

/**
* Reset the collection.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/Abortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait Abortable
*/
protected static function pageNotFound($message = 'Page not Found', array $headers = [])
{
abort(404, $message, $headers);
return abort(404, $message, $headers);
}

/**
Expand All @@ -31,6 +31,6 @@ protected static function pageNotFound($message = 'Page not Found', array $heade
*/
protected static function accessNotAllowed($message = 'Access denied !', array $headers = [])
{
abort(403, $message, $headers);
return abort(403, $message, $headers);
}
}
26 changes: 19 additions & 7 deletions tests/Bases/ControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Arcanedev\Support\Tests\Bases;

use Arcanedev\Support\Tests\TestCase;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* Class ControllerTest
Expand Down Expand Up @@ -28,14 +29,25 @@ public function it_can_do_dummy_stuff()
$this->assertEquals('Super dummy', $response->getContent());
}

/**
* @test
*
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
* @expectedExceptionMessage Super dummy not found !
*/
/** @test */
public function it_can_throw_four_o_four_exception()
{
$this->route('GET', 'dummy::get', ['not-super']);
try {
$response = $this->route('GET', 'dummy::get', ['not-super']);
$statusCode = $response->getStatusCode();
$message = $response->exception->getMessage();

$this->assertInstanceOf(
NotFoundHttpException::class,
$response->exception
);
}
catch(NotFoundHttpException $e) {
$statusCode = $e->getStatusCode();
$message = $e->getMessage();
}

$this->assertSame(404, $statusCode);
$this->assertSame('Super dummy not found !', $message);
}
}
64 changes: 64 additions & 0 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,70 @@ class CollectionTest extends TestCase
| Test Functions
| ------------------------------------------------------------------------------------------------
*/
/** @test */
public function it_can_filter_unique()
{
$c = new Collection(['Hello', 'World', 'World']);

$this->assertEquals(['Hello', 'World'], $c->unique()->all());

$c = new Collection([[1, 2], [1, 2], [2, 3], [3, 4], [2, 3]]);

$this->assertEquals([[1, 2], [2, 3], [3, 4]], $c->unique()->values()->all());
}

/** @test */
public function it_can_filter_unique_with_callback()
{
$c = new Collection([
1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'], 2 => ['id' => 2, 'first' => 'Taylor', 'last' => 'Otwell'],
3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell'], 4 => ['id' => 4, 'first' => 'Abigail', 'last' => 'Otwell'],
5 => ['id' => 5, 'first' => 'Taylor', 'last' => 'Swift'], 6 => ['id' => 6, 'first' => 'Taylor', 'last' => 'Swift'],
]);

$this->assertEquals([
1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'],
3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell'],
], $c->unique('first')->all());

$this->assertEquals([
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());
}

/** @test */
public function it_can_filter_unique_with_strict_mode()
{
$c = new Collection([
[
'id' => '0',
'name' => 'zero',
],
[
'id' => '00',
'name' => 'double zero',
],
[
'id' => '0',
'name' => 'again zero',
],
]);
$this->assertEquals([
[
'id' => '0',
'name' => 'zero',
],
[
'id' => '00',
'name' => 'double zero',
],
], $c->uniqueStrict('id')->all());
}

/** @test */
public function it_can_reset()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/DummyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function index()
public function getOne($slug)
{
if ($slug !== 'super') {
$this->pageNotFound('Super dummy not found !');
return $this->pageNotFound('Super dummy not found !');
}

return 'Super dummy';
Expand Down

0 comments on commit daa8c5a

Please sign in to comment.