public function testTryingToTransformLinkWithInvalidPatternWillThrowRegexException() { $configurationName = md5(rand()); //Some random name, can be anything $this->aggregatorConfiguration->expects($this->any())->method('getName')->willReturn($configurationName); $this->aggregatorConfiguration->expects($this->any())->method('getLinkExtractPattern')->willReturn('/(.+)/'); $this->aggregatorConfiguration->expects($this->atLeastOnce())->method('getLinkTransformPattern')->willReturn('^wtf /r'); $this->setExpectedException('\\noFlash\\TorrentGhost\\Exception\\RegexException', 'Pattern was configured for ' . $configurationName . ' aggregator to transform link'); $this->subjectUnderTest->extractLink('this is example string'); }
/** * This method will try to extract link & transform it according to rules defined in config. * * @param $string * * @return bool|mixed $string false|string Will return title or false if extraction didn't produced any result. * @throws RegexException Supplied extraction/transform regex is invalid. */ public function extractLink($string) { $matchResult = @preg_match($this->configuration->getLinkExtractPattern(), $string, $matches); if ($matchResult === false) { throw new RegexException('Pattern was configured for ' . $this->getName() . ' aggregator to extract link', $this->configuration->getLinkExtractPattern()); } elseif ($matchResult === 0 || !isset($matches[1])) { return false; } elseif (isset($matches[2])) { $this->logger->warning('Pattern ' . $this->configuration->getLinkExtractPattern() . ' configured in ' . $this->getName() . ' aggregator to extract link resulted in ' . (count($matches) - 1) . ' matches instead of one for input string "' . $string . '". Only first one will be used.'); } $transformPattern = $this->configuration->getLinkTransformPattern(); if ($transformPattern !== null) { //Check if transformation is needed $matches[1] = @preg_replace($transformPattern[0], $transformPattern[1], $matches[1]); if ($matches[1] === null) { //PHP weirdo - null will be returned for error throw new RegexException('Pattern was configured for ' . $this->getName() . ' aggregator to transform link', "[{$transformPattern[0]}, {$transformPattern[0]}]"); } } return $matches[1]; }
public function testConfigurationIsConsideredValidOnFreshObject() { $this->assertTrue($this->subjectUnderTest->isValid()); }