-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #5 Add LazyChainProvider for lazy loading providers (sstok)
This PR was merged into the 1.0-dev branch. Discussion ---------- | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | Fixes #2 | License | MIT Commits ------- 9dc1ef3 Add LazyChainProvider for lazy loading providers
- Loading branch information
Showing
5 changed files
with
186 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the RollerworksPasswordStrengthValidator package. | ||
* | ||
* (c) Sebastiaan Stok <s.stok@rollerscapes.net> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Rollerworks\Component\PasswordStrength\Blacklist; | ||
|
||
use Psr\Container\ContainerInterface; | ||
|
||
/** | ||
* Chained blacklist provider. | ||
* | ||
* @author Sebastiaan Stok <s.stok@rollerscapes.net> | ||
*/ | ||
final class LazyChainProvider implements BlacklistProviderInterface | ||
{ | ||
private $container; | ||
private $providers; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param ContainerInterface $container | ||
* @param string[] $providers | ||
*/ | ||
public function __construct(ContainerInterface $container, array $providers) | ||
{ | ||
$this->container = $container; | ||
$this->providers = $providers; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* Runs trough all the providers until one returns true. | ||
*/ | ||
public function isBlacklisted($password) | ||
{ | ||
foreach ($this->providers as $provider) { | ||
if (true === $this->container->get($provider)->isBlacklisted($password)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the RollerworksPasswordStrengthValidator package. | ||
* | ||
* (c) Sebastiaan Stok <s.stok@rollerscapes.net> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Rollerworks\Component\PasswordStrength\Tests; | ||
|
||
use Prophecy\Argument; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Psr\Container\ContainerInterface; | ||
use Rollerworks\Component\PasswordStrength\Blacklist\BlacklistProviderInterface; | ||
|
||
/** | ||
* Trait BlackListMockProviderTrait. | ||
* | ||
* @method ObjectProphecy prophesize($class) | ||
*/ | ||
trait BlackListMockProviderTrait | ||
{ | ||
protected function createMockedProvider($blacklisted) | ||
{ | ||
$mockProvider = $this->prophesize(BlacklistProviderInterface::class); | ||
$mockProvider->isBlacklisted($blacklisted)->willReturn(true); | ||
$mockProvider->isBlacklisted(Argument::any())->willReturn(false); | ||
|
||
return $mockProvider->reveal(); | ||
} | ||
|
||
/** | ||
* @param array $loaders | ||
* | ||
* @return ContainerInterface|object | ||
*/ | ||
protected function createLoadersContainer(array $loaders) | ||
{ | ||
$loadersProphecy = $this->prophesize(ContainerInterface::class); | ||
$loadersProphecy->has(Argument::any())->willReturn(false); | ||
|
||
foreach ($loaders as $name => $loader) { | ||
$loadersProphecy->has($name)->willReturn(true); | ||
$loadersProphecy->get($name)->willReturn($loader); | ||
} | ||
|
||
return $loadersProphecy->reveal(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the RollerworksPasswordStrengthValidator package. | ||
* | ||
* (c) Sebastiaan Stok <s.stok@rollerscapes.net> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Rollerworks\Component\PasswordStrength\Tests\Blacklist; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Rollerworks\Component\PasswordStrength\Blacklist\BlacklistProviderInterface; | ||
use Rollerworks\Component\PasswordStrength\Blacklist\LazyChainProvider; | ||
use Rollerworks\Component\PasswordStrength\Tests\BlackListMockProviderTrait; | ||
|
||
final class LazyChainProviderTest extends TestCase | ||
{ | ||
use BlackListMockProviderTrait; | ||
|
||
public function testBlackList() | ||
{ | ||
$loader1 = $this->createMockedProvider('foobar'); | ||
$loader2 = $this->createMockedProvider('god'); | ||
|
||
$this->createLoadersContainer(['first' => $loader1, 'second' => $loader2]); | ||
|
||
$provider = new LazyChainProvider( | ||
$this->createLoadersContainer(['first' => $loader1, 'second' => $loader2]), | ||
['first', 'second'] | ||
); | ||
|
||
self::assertTrue($provider->isBlacklisted('god')); | ||
self::assertTrue($provider->isBlacklisted('foobar')); | ||
|
||
self::assertFalse($provider->isBlacklisted('tests')); | ||
self::assertFalse($provider->isBlacklisted(null)); | ||
self::assertFalse($provider->isBlacklisted(false)); | ||
} | ||
|
||
public function testStopsLoadingOnFirstHit() | ||
{ | ||
$loader1 = $this->createMockedProvider('foobar'); | ||
$loader2 = $this->createNotExpectedMockedProvider(); | ||
|
||
$this->createLoadersContainer(['first' => $loader1, 'second' => $loader2]); | ||
|
||
$provider = new LazyChainProvider( | ||
$this->createLoadersContainer(['first' => $loader1, 'second' => $loader2]), | ||
['first', 'second'] | ||
); | ||
|
||
self::assertTrue($provider->isBlacklisted('foobar')); | ||
} | ||
|
||
protected function createNotExpectedMockedProvider() | ||
{ | ||
// Prophesize acts funny... | ||
$mock = $this->createMock(BlacklistProviderInterface::class); | ||
$mock->expects(self::never())->method('isBlacklisted'); | ||
|
||
return $mock; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters