Skip to content

Commit

Permalink
Improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mobileka committed Jul 3, 2015
1 parent 1df1a04 commit 60dbd23
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 17 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ php:
- 5.6

before_script:
- composer self-update
- composer install --prefer-source --no-interaction --dev
- curl -s http://getcomposer.org/installer | php
- composer install --prefer-source --no-interaction

script:
- mkdir -p build/logs
- phpunit
- php vendor/bin/phpunit -c phpunit.xml

after_script:
- php vendor/bin/coveralls
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Build Status](https://travis-ci.org/mobileka/scope-applicator.svg)](https://travis-ci.org/mobileka/scope-applicator)
[![Code Climate](https://codeclimate.com/github/mobileka/scope-applicator.png)](https://codeclimate.com/github/mobileka/scope-applicator)
[![Coverage Status](https://coveralls.io/repos/mobileka/scope-applicator/badge.png?branch=master)](https://coveralls.io/r/mobileka/scope-applicator?branch=master)
[![Code Climate](https://codeclimate.com/github/mobileka/scope-applicator.svg)](https://codeclimate.com/github/mobileka/scope-applicator)
[![Coverage Status](https://coveralls.io/repos/mobileka/scope-applicator/badge.svg?branch=master)](https://coveralls.io/r/mobileka/scope-applicator?branch=master)

ScopeApplicator brings an elegant way to sort and filter data to your Laravel projects.

Expand Down
16 changes: 8 additions & 8 deletions src/Mobileka/ScopeApplicator/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public function prepareScopes()
$result = [];

foreach ($this->scopes as $key => $scope) {
// If no scope configuration has been provided, just make the scope name to be it's alias
// If no scope configuration has been provided, just make scope's name to be its alias
if (!is_array($scope)) {
$result[$scope] = ['alias' => $scope];
continue;
}

$result[$key] = $scope;

// If no alias provided, make it to be the scope's name
// If no alias has been provided, make it to be scope's name
if (!isset($scope['alias'])) {
$result[$key]['alias'] = $key;
}
Expand All @@ -71,7 +71,7 @@ public function parseScopeArguments(array $scope)
$scope = new MosaicArray($scope);
$result = [];

// If "type" key is provided, we should typecast the result
// If "type" key has been provided, we have to typecast the result
$type = $scope->getItem('type');

// Get default scope argument value
Expand All @@ -87,10 +87,10 @@ public function parseScopeArguments(array $scope)
return $default ? [$default] : null;
}

// If "keys" configuration key is provided, we are dealing with an array parameter (e.g. <input name="somename[somekey]">)
// If "keys" configuration key has been provided, we are dealing with an array parameter (e.g. <input name="somename[somekey]">)
$keys = $scope->getItem('keys');

// If "keys" are empty, we need to perform some DRY actions
// If "keys" are empty, we have to perform some DRY actions
if (is_null($keys)) {
$keys = ['default'];
$value = ['default' => $value];
Expand All @@ -99,8 +99,8 @@ public function parseScopeArguments(array $scope)
foreach ((array) $keys as $key) {
$arg = $this->setType($value[$key], $type);

// Empty arguments are not allowed by default to allow default scope argument values
// Set allowEmpty option to true if you want to change this behavior
// Empty arguments are not allowed by default in order to allow default scope argument values
// Set allowEmpty option to `true` if you want to change this behavior
if ($arg !== '' or $scope->getItem('allowEmpty')) {
$result[] = $arg;
}
Expand All @@ -110,7 +110,7 @@ public function parseScopeArguments(array $scope)
}

/**
* Convert a provided variable to a provided type
* Typecast a variable
*
* @param mixed $variable
* @param string|null $type
Expand Down
1 change: 1 addition & 0 deletions src/Mobileka/ScopeApplicator/Laravel/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Logger implements LoggerInterface
{
/**
* @param string $message
* @codeCoverageIgnore
*/
public function log($message)
{
Expand Down
6 changes: 4 additions & 2 deletions src/Mobileka/ScopeApplicator/ScopeApplicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public function getConfigurator($scopes)
*
* @param mixed $dataProvider
* @param array $allowedScopes
* @throws BadInputManagerException
* @throws \ErrorException
* @throws \Mobileka\ScopeApplicator\BadInputManagerException
* @return mixed
*/
public function applyScopes($dataProvider, array $allowedScopes = [])
Expand All @@ -53,12 +54,13 @@ public function applyScopes($dataProvider, array $allowedScopes = [])
foreach ($scopes as $scope => $config) {
$scopeArguments = $configurator->parseScopeArguments($config);

// If null, we should ignore this scope
// If null, we have to ignore this scope
if (!is_null($scopeArguments)) {
try {
$dataProvider = call_user_func_array([$dataProvider, $scope], $scopeArguments);
} catch (ErrorException $e) {
$this->getLogger()->log($e);
throw $e;
}
}
}
Expand Down
28 changes: 27 additions & 1 deletion tests/ConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function setUp()
*/
public function getInstance($manager = null, $scopes = [])
{
$manager = $manager ? : Mockery::mock('Mobileka\ScopeApplicator\Contracts\InputManagerInterface');
$manager = $manager ?: Mockery::mock('Mobileka\ScopeApplicator\Contracts\InputManagerInterface');

return $configurator = new Configurator(
$manager,
Expand Down Expand Up @@ -148,6 +148,32 @@ public function parses_boolean_scope_arguments()
assertSame([false], $result);
}

/**
* @covers Mobileka\ScopeApplicator\Configurator::parseScopeArguments
* @test
*/
public function ignores_scope_names_which_return_null_from_input_manager()
{
$configurator = $this->getInstance($this->getInputManagerMock(['scope'], null));

$result = $configurator->parseScopeArguments(['alias' => 'scope']);

assertSame(null, $result);
}

/**
* @covers Mobileka\ScopeApplicator\Configurator::parseScopeArguments
* @test
*/
public function returns_default_value_if_scope_name_returns_null_from_input_manager()
{
$configurator = $this->getInstance($this->getInputManagerMock(['scope'], null));

$result = $configurator->parseScopeArguments(['alias' => 'scope', 'default' => 5]);

assertSame([5], $result);
}

/**
* @param array $args
* @param mixed $return
Expand Down
11 changes: 11 additions & 0 deletions tests/ScopeApplicatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ public function applies_a_scope_with_a_default_argument()
assertSame($expect, $result);
}

/**
* @covers Mobileka\ScopeApplicator\ScopeApplicator::applyScopes
* @test
* @expectedException \ErrorException
*/
public function throws_error_exception()
{
$repository = new Stubs\Fake\Repository;
$repository->getFakeData(['error']);
}

/**
* @covers Mobileka\ScopeApplicator\ScopeApplicator::applyScopes
* @expectedException Mobileka\ScopeApplicator\BadInputManagerException
Expand Down
9 changes: 9 additions & 0 deletions tests/Stubs/Fake/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ public function between($min, $max)
return $this;
}

/**
* @throws \ErrorException
* @return float
*/
public function error()
{
throw new \ErrorException;
}

/**
* @return array
*/
Expand Down
1 change: 1 addition & 0 deletions tests/Stubs/Fake/InputManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function get($key = null, $default = null)

case 'five':
case 'six:default':
case 'error':
return '';

default:
Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/GoodRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GoodRepository
/**
* @var InputManagerInterface
*/
public $inputManager;
protected $inputManager;

/**
* @param $manager
Expand Down

0 comments on commit 60dbd23

Please sign in to comment.