5 require_once(CONST_LibDir.'/Phrase.php');
 
   9     public function containsAny($sTerm)
 
  15 // phpcs:ignore PSR1.Classes.ClassDeclaration.MultipleClasses
 
  16 class TokensPartialSet
 
  18     public function __construct($aTokens)
 
  20         $this->aTokens = array_flip($aTokens);
 
  23     public function containsAny($sTerm)
 
  25         return isset($this->aTokens[$sTerm]);
 
  29 // phpcs:ignore PSR1.Classes.ClassDeclaration.MultipleClasses
 
  30 class PhraseTest extends \PHPUnit\Framework\TestCase
 
  34     private function serializeSets($aSets)
 
  37         foreach ($aSets as $aSet) {
 
  38             $aParts[] = '(' . join('|', $aSet) . ')';
 
  40         return join(',', $aParts);
 
  44     public function testEmptyPhrase()
 
  46         $oPhrase = new Phrase('', '');
 
  47         $oPhrase->computeWordSets(array(), new TokensFullSet());
 
  49         $this->assertNull($oPhrase->getWordSets());
 
  53     public function testSingleWordPhrase()
 
  55         $oPhrase = new Phrase('a', '');
 
  56         $oPhrase->computeWordSets(array('a'), new TokensFullSet());
 
  60             $this->serializeSets($oPhrase->getWordSets())
 
  65     public function testMultiWordPhrase()
 
  67         $oPhrase = new Phrase('a b', '');
 
  68         $oPhrase->computeWordSets(array('a', 'b'), new TokensFullSet());
 
  71             $this->serializeSets($oPhrase->getWordSets())
 
  74         $oPhrase = new Phrase('a b c', '');
 
  75         $oPhrase->computeWordSets(array('a', 'b', 'c'), new TokensFullSet());
 
  77             '(a b c),(a|b c),(a b|c),(a|b|c)',
 
  78             $this->serializeSets($oPhrase->getWordSets())
 
  81         $oPhrase = new Phrase('a b c d', '');
 
  82         $oPhrase->computeWordSets(array('a', 'b', 'c', 'd'), new TokensFullSet());
 
  84             '(a b c d),(a b c|d),(a b|c d),(a|b c d),(a b|c|d),(a|b c|d),(a|b|c d),(a|b|c|d)',
 
  85             $this->serializeSets($oPhrase->getWordSets())
 
  90     public function testInverseWordSets()
 
  92         $oPhrase = new Phrase('a b c', '');
 
  93         $oPhrase->computeWordSets(array('a', 'b', 'c'), new TokensFullSet());
 
  94         $oPhrase->invertWordSets();
 
  97             '(a b c),(b c|a),(c|a b),(c|b|a)',
 
  98             $this->serializeSets($oPhrase->getWordSets())
 
 103     public function testMaxWordSets()
 
 105         $aWords = array_fill(0, 4, 'a');
 
 106         $oPhrase = new Phrase(join(' ', $aWords), '');
 
 107         $oPhrase->computeWordSets($aWords, new TokensFullSet());
 
 108         $this->assertEquals(8, count($oPhrase->getWordSets()));
 
 109         $oPhrase->invertWordSets();
 
 110         $this->assertEquals(8, count($oPhrase->getWordSets()));
 
 112         $aWords = array_fill(0, 18, 'a');
 
 113         $oPhrase = new Phrase(join(' ', $aWords), '');
 
 114         $oPhrase->computeWordSets($aWords, new TokensFullSet());
 
 115         $this->assertEquals(100, count($oPhrase->getWordSets()));
 
 116         $oPhrase->invertWordSets();
 
 117         $this->assertEquals(100, count($oPhrase->getWordSets()));
 
 121     public function testPartialTokensShortTerm()
 
 123         $oPhrase = new Phrase('a b c d', '');
 
 124         $oPhrase->computeWordSets(array('a', 'b', 'c', 'd'), new TokensPartialSet(array('a', 'b', 'd', 'b c', 'b c d')));
 
 126             '(a|b c d),(a|b c|d)',
 
 127             $this->serializeSets($oPhrase->getWordSets())
 
 132     public function testPartialTokensLongTerm()
 
 134         $aWords = array_fill(0, 18, 'a');
 
 135         $oPhrase = new Phrase(join(' ', $aWords), '');
 
 136         $oPhrase->computeWordSets($aWords, new TokensPartialSet(array('a', 'a a a a a')));
 
 137         $this->assertEquals(80, count($oPhrase->getWordSets()));