Skip to content

Commit

Permalink
Adding AbstractMultiLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
doublecompile committed Nov 9, 2016
1 parent d6db6e9 commit 77f0f4c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
12 changes: 12 additions & 0 deletions hhi/AbstractMultiLoader.hhi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?hh // strict

namespace Caridea\Acl;

abstract class AbstractMultiLoader implements Loader, MultiStrategy
{
public function load(Target $target, array<Subject> $subjects, Service $service): Acl
{
$acls = $this->loadAll([$target], $subjects, $service);
return $acls[(string) $target];
}
}
42 changes: 42 additions & 0 deletions src/AbstractMultiLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/**
* Caridea
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* @copyright 2015-2016 LibreWorks contributors
* @license http://opensource.org/licenses/Apache-2.0 Apache 2.0 License
*/
namespace Caridea\Acl;

/**
* Abstract base for `Loaders` which implement `MultiStrategy`
*
* @copyright 2015-2016 LibreWorks contributors
* @license http://opensource.org/licenses/Apache-2.0 Apache 2.0 License
*/
abstract class AbstractMultiLoader implements Loader, MultiStrategy
{
/**
* {@inheritDoc}
*/
public function load(Target $target, array $subjects, Service $service): Acl
{
$acls = $this->loadAll([$target], $subjects, $service);
if (!array_key_exists((string) $target, $acls)) {
throw new Exception\Unloadable("Could not load ACL for $target");
}
return $acls[(string) $target];
}
}
62 changes: 62 additions & 0 deletions tests/AbstractMultiLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
/**
* Caridea
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* @copyright 2015-2016 LibreWorks contributors
* @license http://opensource.org/licenses/Apache-2.0 Apache 2.0 License
*/
namespace Caridea\Acl;

/**
* Generated by hand.
*/
class AbstractMultiLoaderTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Caridea\Acl\AbstractMultiLoader::load
*/
public function testLoad()
{
$target = new Target('foo', 'bar');
$subjects = [Subject::role('admin')];

$acl = $this->getMockForAbstractClass(Acl::class);
$service = $this->getMockBuilder(Service::class)
->disableOriginalConstructor()
->getMock();
$loader = $this->getMockBuilder(AbstractMultiLoader::class)
->setMethods(['loadAll', 'supports'])
->getMock();
$loader->expects($this->never())
->method('supports');
$loader->expects($this->once())
->method('loadAll')
->with(
$this->equalTo([$target]),
$this->equalTo($subjects),
$this->equalTo($service)
)->willReturnCallback(function ($targets, $subs, $svc) use ($acl, $target) {
return [
((string) $target) => $acl,
];
});

$acl2 = $loader->load($target, $subjects, $service);
$this->assertSame($acl, $acl2);

$this->verifyMockObjects();
}
}

0 comments on commit 77f0f4c

Please sign in to comment.