forked from Behat/Behat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add local cache for transformPatternToRegex
- Loading branch information
Showing
2 changed files
with
79 additions
and
3 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
68 changes: 68 additions & 0 deletions
68
tests/Behat/Tests/Definition/Pattern/PatternTransformerTest.php
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,68 @@ | ||
<?php | ||
|
||
namespace Behat\Tests\Definition\Pattern; | ||
|
||
use Behat\Behat\Definition\Pattern\PatternTransformer; | ||
use Behat\Behat\Definition\Pattern\Policy\PatternPolicy; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Class PatternTransformerTest | ||
* @author Julien Deniau <julien.deniau@mapado.com> | ||
*/ | ||
class PatternTransformerTest extends TestCase | ||
{ | ||
public function testTransformPatternToRegexCache() | ||
{ | ||
$observer = $this->prophesize(PatternPolicy::class); | ||
// first pattern | ||
$observer->supportsPattern('hello world')->willReturn(true); | ||
$observer->transformPatternToRegex('hello world') | ||
->shouldBeCalledTimes(1) | ||
->willReturn('/hello world/'); | ||
|
||
// second pattern | ||
$observer->supportsPattern('hi world')->willReturn(true); | ||
$observer->transformPatternToRegex('hi world') | ||
->shouldBeCalledTimes(1) | ||
->willReturn('/hi world/'); | ||
|
||
$testedInstance = new PatternTransformer(); | ||
$testedInstance->registerPatternPolicy($observer->reveal()); | ||
$regex = $testedInstance->transformPatternToRegex('hello world'); | ||
$regex2 = $testedInstance->transformPatternToRegex('hello world'); | ||
|
||
$regex3 = $testedInstance->transformPatternToRegex('hi world'); | ||
|
||
$this->assertEquals('/hello world/', $regex); | ||
$this->assertEquals('/hello world/', $regex2); | ||
$this->assertEquals('/hi world/', $regex3); | ||
} | ||
|
||
public function testTransformPatternToRegexCacheAndRegisterNewPolicy() | ||
{ | ||
// first pattern | ||
$policy1Prophecy = $this->prophesize(PatternPolicy::class); | ||
$policy1Prophecy->supportsPattern('hello world')->willReturn(true); | ||
$policy1Prophecy->transformPatternToRegex('hello world') | ||
->shouldBeCalledTimes(2) | ||
->willReturn('/hello world/'); | ||
|
||
// second pattern | ||
$policy2Prophecy = $this->prophesize(PatternPolicy::class); | ||
$policy1Prophecy->supportsPattern()->shouldNotBeCalled(); | ||
$policy1Prophecy->transformPatternToRegex()->shouldNotBeCalled(); | ||
|
||
$testedInstance = new PatternTransformer(); | ||
$testedInstance->registerPatternPolicy($policy1Prophecy->reveal()); | ||
$regex = $testedInstance->transformPatternToRegex('hello world'); | ||
$regex2 = $testedInstance->transformPatternToRegex('hello world'); | ||
|
||
$testedInstance->registerPatternPolicy($policy2Prophecy->reveal()); | ||
$regex3 = $testedInstance->transformPatternToRegex('hello world'); | ||
|
||
$this->assertEquals('/hello world/', $regex); | ||
$this->assertEquals('/hello world/', $regex2); | ||
$this->assertEquals('/hello world/', $regex3); | ||
} | ||
} |