|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Apicart\FQL\Tests\Tokenizer; |
|
4
|
|
|
|
|
5
|
|
|
use Apicart\FQL\Tokenizer\AbstractTokenExtractor; |
|
6
|
|
|
use Apicart\FQL\Tokenizer\Full; |
|
7
|
|
|
use Apicart\FQL\Tokenizer\Text; |
|
8
|
|
|
use Apicart\FQL\Tokenizer\Tokenizer; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use ReflectionClass; |
|
11
|
|
|
use RuntimeException; |
|
12
|
|
|
|
|
13
|
|
|
final class TokenizerTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
public function testExtractThrowsExceptionPCRE(): void |
|
17
|
|
|
{ |
|
18
|
|
|
self::expectException(RuntimeException::class); |
|
|
|
|
|
|
19
|
|
|
self::expectExceptionMessage('PCRE regex error code: 2'); |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
$extractorMock = $this->getMockBuilder(AbstractTokenExtractor::class) |
|
|
|
|
|
|
22
|
|
|
->setMethods(['getExpressionTypeMap']) |
|
23
|
|
|
->getMockForAbstractClass(); |
|
24
|
|
|
|
|
25
|
|
|
$extractorMock->expects(self::once()) |
|
26
|
|
|
->method('getExpressionTypeMap') |
|
27
|
|
|
->willReturn([ |
|
28
|
|
|
'/(?:\D+|<\d+>)*[!?]/' => Tokenizer::TOKEN_WHITESPACE, |
|
29
|
|
|
]); |
|
30
|
|
|
|
|
31
|
|
|
/** @var AbstractTokenExtractor $extractor */ |
|
32
|
|
|
$extractor = $extractorMock; |
|
33
|
|
|
$extractor->extract('foobar foobar foobar', 0); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
public function testFullExtractTermTokenThrowsException(): void |
|
38
|
|
|
{ |
|
39
|
|
|
self::expectException(RuntimeException::class); |
|
|
|
|
|
|
40
|
|
|
self::expectExceptionMessage('Could not extract term token from the given data'); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
$extractor = new Full(); |
|
43
|
|
|
$reflectedClass = new ReflectionClass($extractor); |
|
44
|
|
|
$reflectedProperty = $reflectedClass->getProperty('expressionTypeMap'); |
|
45
|
|
|
$reflectedProperty->setAccessible(true); |
|
46
|
|
|
$reflectedProperty->setValue([ |
|
47
|
|
|
'/(?<lexeme>foobar)/' => Tokenizer::TOKEN_TERM, |
|
48
|
|
|
]); |
|
49
|
|
|
$extractor->extract('foobar', 0); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
public function testTextExtractTermTokenThrowsException(): void |
|
54
|
|
|
{ |
|
55
|
|
|
self::expectException(RuntimeException::class); |
|
|
|
|
|
|
56
|
|
|
self::expectExceptionMessage('Could not extract term token from the given data'); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
$extractor = new Text(); |
|
59
|
|
|
$reflectedClass = new ReflectionClass($extractor); |
|
60
|
|
|
$reflectedProperty = $reflectedClass->getProperty('expressionTypeMap'); |
|
61
|
|
|
$reflectedProperty->setAccessible(true); |
|
62
|
|
|
$reflectedProperty->setValue([ |
|
63
|
|
|
'/(?<lexeme>foobar)/' => Tokenizer::TOKEN_TERM, |
|
64
|
|
|
]); |
|
65
|
|
|
$extractor->extract('foobar', 0); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|