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

Commit

Permalink
CS fixes and usage of phpcs to ensure PSR2 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed Jun 25, 2013
1 parent 3551044 commit 5fc3b17
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 147 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -13,7 +13,7 @@ before_script:

script:
- ./vendor/bin/phpunit --coverage-clover ./build/logs/clover.xml
- output=$(php php-cs-fixer.phar fix -v --dry-run --level=psr2 ./src/); if [[ $output ]]; then while read -r line; do echo -e "\e[00;31m$line\e[00m"; done <<< "$output"; false; fi;
- ./vendor/bin/phpcs --standard=PSR2 ./src/ ./tests/

after_script:
- php vendor/bin/coveralls -v
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -39,6 +39,7 @@
"doctrine/common": ">=2.3,<2.5-dev",
"zendframework/zend-developer-tools": "0.*",
"zf-commons/zfc-user": "0.*",
"squizlabs/php_codesniffer": "1.4.*",
"satooshi/php-coveralls": "~0.6"
},
"suggests": {
Expand Down
3 changes: 1 addition & 2 deletions src/BjyAuthorize/View/RedirectionStrategy.php
Expand Up @@ -74,8 +74,7 @@ public function onDispatchError(MvcEvent $event)
$error = $event->getError();
$url = $this->redirectUri;

if (
$result instanceof Response
if ($result instanceof Response
|| ! $routeMatch
|| ($response && ! $response instanceof Response)
|| ! (
Expand Down
12 changes: 7 additions & 5 deletions tests/BjyAuthorizeTest/Collector/RoleCollectorTest.php
Expand Up @@ -105,11 +105,13 @@ public function testTraversableCollect()
->method('getIdentityRoles')
->will(
$this->returnValue(
new \ArrayIterator(array(
$role1,
'role2',
'key' => 'role3',
))
new \ArrayIterator(
array(
$role1,
'role2',
'key' => 'role3',
)
)
)
);

Expand Down
73 changes: 40 additions & 33 deletions tests/BjyAuthorizeTest/Guard/ControllerTest.php
Expand Up @@ -51,13 +51,8 @@ public function setUp()
->serviceLocator
->expects($this->any())
->method('get')
->will($this->returnCallback(function ($name) use ($authorize) {
if ($name === 'BjyAuthorize\Service\Authorize') {
return $authorize;
}

throw new \UnexpectedValueException();
}));
->with('BjyAuthorize\\Service\\Authorize')
->will($this->returnValue($authorize));
}

/**
Expand Down Expand Up @@ -129,9 +124,13 @@ public function testOnDispatchWithValidController()
->authorize
->expects($this->any())
->method('isAllowed')
->will($this->returnValue(function ($resource) {
return $resource === 'controller/test-controller';
}));
->will(
$this->returnValue(
function ($resource) {
return $resource === 'controller/test-controller';
}
)
);

$this->assertNull($this->controllerGuard->onDispatch($event), 'Does not stop event propagation');
}
Expand All @@ -147,9 +146,13 @@ public function testOnDispatchWithValidControllerAndAction()
->authorize
->expects($this->any())
->method('isAllowed')
->will($this->returnValue(function ($resource) {
return $resource === 'controller/test-controller:test-action';
}));
->will(
$this->returnValue(
function ($resource) {
return $resource === 'controller/test-controller:test-action';
}
)
);

$this->assertNull($this->controllerGuard->onDispatch($event), 'Does not stop event propagation');
}
Expand All @@ -165,9 +168,13 @@ public function testOnDispatchWithValidControllerAndMethod()
->authorize
->expects($this->any())
->method('isAllowed')
->will($this->returnValue(function ($resource) {
return $resource === 'controller/test-controller:put';
}));
->will(
$this->returnValue(
function ($resource) {
return $resource === 'controller/test-controller:put';
}
)
);

$this->assertNull($this->controllerGuard->onDispatch($event), 'Does not stop event propagation');
}
Expand Down Expand Up @@ -203,12 +210,8 @@ public function testOnDispatchWithInvalidResource()
->will($this->returnValue(false));
$event->expects($this->once())->method('setError')->with(Controller::ERROR);
$event->expects($this->exactly(3))->method('setParam')->with(
$this->callback(function ($key) {
return in_array($key, array('identity', 'controller', 'action'));
}),
$this->callback(function ($val) {
return in_array($val, array('admin', 'test-controller', 'test-action'));
})
$this->logicalOr('identity', 'controller', 'action'),
$this->logicalOr('admin', 'test-controller', 'test-action')
);
$event
->getTarget()
Expand Down Expand Up @@ -243,17 +246,21 @@ private function createMvcEvent($controller = null, $action = null, $method = nu
$routeMatch
->expects($this->any())
->method('getParam')
->will($this->returnCallback(function ($param) use ($controller, $action) {
if ($param === 'controller') {
return $controller;
}

if ($param === 'action') {
return $action;
}

return null;
}));
->will(
$this->returnCallback(
function ($param) use ($controller, $action) {
if ($param === 'controller') {
return $controller;
}

if ($param === 'action') {
return $action;
}

return null;
}
)
);

return $event;
}
Expand Down
27 changes: 11 additions & 16 deletions tests/BjyAuthorizeTest/Guard/RouteTest.php
Expand Up @@ -51,13 +51,8 @@ public function setUp()
->serviceLocator
->expects($this->any())
->method('get')
->will($this->returnCallback(function ($name) use ($authorize) {
if ($name === 'BjyAuthorize\Service\Authorize') {
return $authorize;
}

throw new \UnexpectedValueException();
}));
->with('BjyAuthorize\\Service\\Authorize')
->will($this->returnValue($authorize));
}

/**
Expand Down Expand Up @@ -148,9 +143,13 @@ public function testOnRouteWithValidRoute()
->authorize
->expects($this->any())
->method('isAllowed')
->will($this->returnValue(function ($resource) {
return $resource === 'route/test-route';
}));
->will(
$this->returnValue(
function ($resource) {
return $resource === 'route/test-route';
}
)
);

$this->assertNull($this->routeGuard->onRoute($event), 'Does not stop event propagation');
}
Expand All @@ -169,12 +168,8 @@ public function testOnRouteWithInvalidResource()
->will($this->returnValue(false));
$event->expects($this->once())->method('setError')->with(Route::ERROR);
$event->expects($this->exactly(2))->method('setParam')->with(
$this->callback(function ($key) {
return in_array($key, array('identity', 'route'));
}),
$this->callback(function ($val) {
return in_array($val, array('admin', 'test-route'));
})
$this->logicalOr('identity', 'route'),
$this->logicalOr('admin', 'test-route')
);
$event
->getTarget()
Expand Down
5 changes: 1 addition & 4 deletions tests/BjyAuthorizeTest/Provider/Resource/ConfigTest.php
Expand Up @@ -24,10 +24,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase
*/
public function testGetResources()
{
$config = new Config(array(
'resource1',
'resource2',
));
$config = new Config(array('resource1', 'resource2',));

$resources = $config->getResources();

Expand Down
30 changes: 16 additions & 14 deletions tests/BjyAuthorizeTest/Provider/Role/ConfigTest.php
Expand Up @@ -25,19 +25,21 @@ class ConfigTest extends PHPUnit_Framework_TestCase
*/
public function testConstructor()
{
$config = new Config(array(
'role1' => array(),
'role2',
'role3' => array(
'children' => array('role4'),
),
'role5' => array(
'children' => array(
'role6',
'role7' => array(),
$config = new Config(
array(
'role1' => array(),
'role2',
'role3' => array(
'children' => array('role4'),
),
),
));
'role5' => array(
'children' => array(
'role6',
'role7' => array(),
),
),
)
);

$roles = $config->getRoles();

Expand All @@ -46,10 +48,10 @@ public function testConstructor()
/* @var $role \BjyAuthorize\Acl\Role */
foreach ($roles as $role) {
$this->assertInstanceOf('BjyAuthorize\Acl\Role', $role);
$this->assertTrue(in_array(
$this->assertContains(
$role->getRoleId(),
array('role1', 'role2', 'role3', 'role4', 'role5', 'role6', 'role7')
));
);

if ('role4' === $role->getRoleId()) {
$this->assertNotNull($role->getParent());
Expand Down
Expand Up @@ -81,7 +81,7 @@ public function testGetRolesWithNoParents()
new Role('role2', null),
);

$this->assertEquals($expects, $this->provider->getRoles());
$this->assertEquals($expects, $this->provider->getRoles());
}

/**
Expand Down Expand Up @@ -109,6 +109,6 @@ public function testGetRolesWithParents()
new Role('role3', $expectedRole1),
);

$this->assertEquals($expects, $this->provider->getRoles());
$this->assertEquals($expects, $this->provider->getRoles());
}
}
Expand Up @@ -40,23 +40,23 @@ public function testCreateService()
$serviceLocator
->expects($this->any())
->method('get')
->will($this->returnCallback(function ($service) use ($user, $config) {
if ('zfcuser_user_service' === $service) {
return $user;
}

if ('BjyAuthorize\Config' === $service) {
return $config;
}

throw new \InvalidArgumentException();
}));
->with($this->logicalOr('zfcuser_user_service', 'BjyAuthorize\\Config'))
->will(
$this->returnCallback(
function ($service) use ($user, $config) {
if ('zfcuser_user_service' === $service) {
return $user;
}

return $config;
}
)
);

$authenticationFactory = new AuthenticationIdentityProviderServiceFactory();
$authentication = $authenticationFactory->createService($serviceLocator);

$this->assertEquals($authentication->getDefaultRole(), 'test-guest');
$this->assertEquals($authentication->getAuthenticatedRole(), 'test-user');
}

}
18 changes: 12 additions & 6 deletions tests/BjyAuthorizeTest/Service/AuthorizeTest.php
Expand Up @@ -30,12 +30,18 @@ public function testLoadLoadsAclFromCacheAndDoesNotBuildANewAclObject()
->disableOriginalConstructor()
->getMock();

$cache->expects($this->once())
->method('getItem')
->will($this->returnCallback(function($key, &$success) use ($acl) {
$success = true;
return $acl;
}));
$cache
->expects($this->once())
->method('getItem')
->will(
$this->returnCallback(
function ($key, & $success) use ($acl) {
$success = true;

return $acl;
}
)
);

$serviceManager = new ServiceManager();
$serviceManager->setService(
Expand Down
34 changes: 21 additions & 13 deletions tests/BjyAuthorizeTest/Service/BaseProvidersServiceFactoryTest.php
Expand Up @@ -11,7 +11,6 @@
namespace BjyAuthorizeTest\Service;

use PHPUnit_Framework_TestCase;
use BjyAuthorize\Service\BaseProvidersServiceFactory;

/**
* Test for {@see \BjyAuthorize\Service\ResourceProvidersServiceFactory}
Expand Down Expand Up @@ -40,24 +39,33 @@ public function testCreateService()
$serviceLocator
->expects($this->any())
->method('has')
->will($this->returnCallback(function ($serviceName) {
return in_array($serviceName, array('foo', 'bar'), true);
}));
->will(
$this->returnCallback(
function ($serviceName) {
return in_array($serviceName, array('foo', 'bar'), true);
}
)
);

$serviceLocator
->expects($this->any())
->method('get')
->will($this->returnCallback(function ($serviceName) use ($foo, $bar, $config) {
if ('BjyAuthorize\\Config' === $serviceName) {
return $config;
}
->with($this->logicalOr('BjyAuthorize\\Config', 'foo', 'bar'))
->will(
$this->returnCallback(
function ($serviceName) use ($foo, $bar, $config) {
if ('BjyAuthorize\\Config' === $serviceName) {
return $config;
}

if ('foo' === $serviceName) {
return $foo;
}
if ('foo' === $serviceName) {
return $foo;
}

return $bar;
}));
return $bar;
}
)
);

$providers = $factory->createService($serviceLocator);

Expand Down

0 comments on commit 5fc3b17

Please sign in to comment.