From 4c93539b1929bfd3861b25c56caf77f7c03cb7ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20K=C3=A4hm?= Date: Thu, 26 Oct 2023 22:36:14 +0200 Subject: [PATCH] [TASK] Fix PHP CS issues --- Classes/Domain/Result/Document/Collection.php | 15 +- Classes/Domain/Result/Document/Document.php | 88 ++-- .../Result/Document/Field/Collection.php | 4 +- .../Domain/Result/Document/Field/Field.php | 111 +++-- Classes/Domain/Result/Explanation/Content.php | 33 +- .../Result/Explanation/ExplainResult.php | 206 ++++---- .../Result/Explanation/ExplainService.php | 53 +- .../Domain/Result/Explanation/MetaData.php | 89 ++-- .../Result/Explanation/Nodes/Explain.php | 456 +++++++++--------- .../Domain/Result/Explanation/Nodes/Leaf.php | 15 +- .../Domain/Result/Explanation/Nodes/Max.php | 38 +- .../Result/Explanation/Nodes/Product.php | 15 +- .../Domain/Result/Explanation/Nodes/Sum.php | 12 +- Classes/Domain/Result/Explanation/Parser.php | 371 +++++++------- .../Visitors/ExplainNodeVisitorInterface.php | 12 +- .../Visitors/SummarizeFieldImpacts.php | 99 ++-- .../Visitors/SummarizeLeafImpacts.php | 40 +- Classes/Domain/Result/Parser.php | 421 ++++++++-------- Classes/Domain/Result/Result.php | 213 ++++---- Classes/Domain/Result/Timing/Item.php | 82 ++-- .../Domain/Result/Timing/ItemCollection.php | 39 +- Classes/Domain/Result/Timing/Timing.php | 113 +++-- Tests/Domain/AbstractSolrTestCase.php | 41 +- .../AbstractExplanationTestCase.php | 27 +- .../Explanation/ExplainServiceTestCase.php | 35 +- .../Explanation/ExplanationSolr34TestCase.php | 202 ++++---- .../Explanation/ExplanationSolr3TestCase.php | 153 +++--- .../Explanation/ExplanationSolr40TestCase.php | 37 +- .../Result/Explanation/Nodes/MaxTestCase.php | 15 +- .../SummarizeFieldImpactsTestCase.php | 187 ++++--- .../Visitors/SummarizeLeafImpactsTestCase.php | 143 +++--- Tests/Domain/Result/ParserTestCase.php | 99 ++-- Tests/Domain/Result/Timing/ItemTestCase.php | 34 +- 33 files changed, 1718 insertions(+), 1780 deletions(-) diff --git a/Classes/Domain/Result/Document/Collection.php b/Classes/Domain/Result/Document/Collection.php index 53b6230..ec7633d 100644 --- a/Classes/Domain/Result/Document/Collection.php +++ b/Classes/Domain/Result/Document/Collection.php @@ -6,13 +6,12 @@ class Collection extends ArrayObject { - - /** - * @param $offset - * @return Document - */ - public function getDocument($offset): Document + /** + * @param $offset + * @return Document + */ + public function getDocument($offset): Document { - return $this->offsetGet($offset); - } + return $this->offsetGet($offset); + } } diff --git a/Classes/Domain/Result/Document/Document.php b/Classes/Domain/Result/Document/Document.php index ddd9078..92623ea 100644 --- a/Classes/Domain/Result/Document/Document.php +++ b/Classes/Domain/Result/Document/Document.php @@ -7,63 +7,59 @@ class Document { + /** + * @var Collection + */ + protected $fieldCollection; - /** - * @var Collection - */ - protected $fieldCollection; + /** + * @var string + */ + protected $rawExplainData; - /** - * @var string - */ - protected $rawExplainData; - - /** - * @return void - */ - public function __construct() + public function __construct() { - $this->fieldCollection = new Collection(); - } + $this->fieldCollection = new Collection(); + } - /** - * @param Field $field - */ - public function addField(Field $field) + /** + * @param Field $field + */ + public function addField(Field $field) { - $this->fieldCollection->offsetSet($field->getName(),$field); - } + $this->fieldCollection->offsetSet($field->getName(), $field); + } - /** - * @return Collection - */ - public function getFields(): Collection + /** + * @return Collection + */ + public function getFields(): Collection { - return $this->fieldCollection; - } + return $this->fieldCollection; + } - /** - * @param string - * @return Field - */ - public function getFieldByName(string $fieldName): Field + /** + * @param string + * @return Field + */ + public function getFieldByName(string $fieldName): Field { - return $this->fieldCollection->offsetGet($fieldName); - } + return $this->fieldCollection->offsetGet($fieldName); + } - /** - * @param string $rawExplainData - */ - public function setRawExplainData(string $rawExplainData) + /** + * @param string $rawExplainData + */ + public function setRawExplainData(string $rawExplainData) { - $this->rawExplainData = $rawExplainData; - } + $this->rawExplainData = $rawExplainData; + } - /** - * @return string - */ - public function getRawExplainData(): string + /** + * @return string + */ + public function getRawExplainData(): string { - return $this->rawExplainData; - } + return $this->rawExplainData; + } } diff --git a/Classes/Domain/Result/Document/Field/Collection.php b/Classes/Domain/Result/Document/Field/Collection.php index 76da759..96e20dc 100644 --- a/Classes/Domain/Result/Document/Field/Collection.php +++ b/Classes/Domain/Result/Document/Field/Collection.php @@ -4,6 +4,4 @@ use ArrayObject; -class Collection extends ArrayObject -{ -} +class Collection extends ArrayObject {} diff --git a/Classes/Domain/Result/Document/Field/Field.php b/Classes/Domain/Result/Document/Field/Field.php index b53f734..e9d5bb4 100644 --- a/Classes/Domain/Result/Document/Field/Field.php +++ b/Classes/Domain/Result/Document/Field/Field.php @@ -4,75 +4,74 @@ class Field { + /** + * @var string + */ + protected $name; - /** - * @var string - */ - protected $name; + /** + * @var mixed + */ + protected $value; - /** - * @var mixed - */ - protected $value; + /** + * @var int + */ + protected $type; - /** - * @var int - */ - protected $type; - - /** - * @param string $name - */ - public function setName(string $name) + /** + * @param string $name + */ + public function setName(string $name) { - $this->name = $name; - } + $this->name = $name; + } - /** - * @return string - */ - public function getName(): string + /** + * @return string + */ + public function getName(): string { - return $this->name; - } + return $this->name; + } - /** - * @param int $type - */ - public function setType(int $type) + /** + * @param int $type + */ + public function setType(int $type) { - $this->type = $type; - } + $this->type = $type; + } - /** - * @return int - */ - public function getType(): int + /** + * @return int + */ + public function getType(): int { - return $this->type; - } + return $this->type; + } - /** - * @param mixed $value - */ - public function setValue($value) + /** + * @param mixed $value + */ + public function setValue($value) { - $this->value = $value; - } + $this->value = $value; + } - /** - * @return mixed - */ - public function getValue() + /** + * @return mixed + */ + public function getValue() { - return $this->value; - } + return $this->value; + } - /** - * @boolean - */ - public function isMultiValue(): bool + /** + * @boolean + */ + public function isMultiValue(): bool { - return is_array($this->value); - } -} \ No newline at end of file + return is_array($this->value); + } +} diff --git a/Classes/Domain/Result/Explanation/Content.php b/Classes/Domain/Result/Explanation/Content.php index 0f7e55a..2f19b4c 100644 --- a/Classes/Domain/Result/Explanation/Content.php +++ b/Classes/Domain/Result/Explanation/Content.php @@ -21,25 +21,24 @@ */ class Content { + /** + * @var string + */ + protected $content; - /** - * @var string - */ - protected $content; - - /** - * @param string $content - */ - public function __construct(string $content) + /** + * @param string $content + */ + public function __construct(string $content) { - $this->content = $content; - } + $this->content = $content; + } - /** - * @return string - */ - public function getContent(): string + /** + * @return string + */ + public function getContent(): string { - return $this->content; - } + return $this->content; + } } diff --git a/Classes/Domain/Result/Explanation/ExplainResult.php b/Classes/Domain/Result/Explanation/ExplainResult.php index f1880a1..d4d3d3c 100644 --- a/Classes/Domain/Result/Explanation/ExplainResult.php +++ b/Classes/Domain/Result/Explanation/ExplainResult.php @@ -17,121 +17,117 @@ */ class ExplainResult { - - /** - * Id of the relevant document. - * - * @var string - */ - protected $documentId = ''; - - /** - * @var ArrayObject - */ - protected $children = null; - - /** - * @var Explain - */ - protected $rootNode = null; - - /** - * @var ArrayObject - */ - protected $attributes = null; - - /** - * @return void - */ - public function __construct() + /** + * Id of the relevant document. + * + * @var string + */ + protected $documentId = ''; + + /** + * @var ArrayObject + */ + protected $children; + + /** + * @var Explain + */ + protected $rootNode; + + /** + * @var ArrayObject + */ + protected $attributes; + + public function __construct() { - $this->attributes = new ArrayObject(); - } + $this->attributes = new ArrayObject(); + } - /** - * @param ArrayObject $children - */ - public function setChildren(ArrayObject $children) + /** + * @param ArrayObject $children + */ + public function setChildren(ArrayObject $children) { - $this->children = $children; - } + $this->children = $children; + } - /** - * @return ArrayObject - */ - public function getChildren(): ?ArrayObject + /** + * @return ArrayObject + */ + public function getChildren(): ?ArrayObject { - return $this->children; - } - - /** - * @param $index - * @return Explain - */ - public function getChild($index): Explain + return $this->children; + } + + /** + * @param $index + * @return Explain + */ + public function getChild($index): Explain { - return $this->children[$index]; - } + return $this->children[$index]; + } - /** - * @param Explain $rootNode - */ - public function setRootNode(Explain $rootNode) + /** + * @param Explain $rootNode + */ + public function setRootNode(Explain $rootNode) { - $this->rootNode = $rootNode; - } - - /** - * Method to retrieve the root node of the explain. - * - * @return Explain - */ - public function getRootNode(): ?Explain + $this->rootNode = $rootNode; + } + + /** + * Method to retrieve the root node of the explain. + * + * @return Explain + */ + public function getRootNode(): ?Explain { - return $this->rootNode; - } - - /** - * Method to set the corresponding document id. - * - * @param string $documentId - */ - public function setDocumentId(string $documentId) + return $this->rootNode; + } + + /** + * Method to set the corresponding document id. + * + * @param string $documentId + */ + public function setDocumentId(string $documentId) { - $this->documentId = $documentId; - } - - /** - * Document id where this explanation belongs to. - * - * @return string - */ - public function getDocumentId(): string + $this->documentId = $documentId; + } + + /** + * Document id where this explanation belongs to. + * + * @return string + */ + public function getDocumentId(): string { - return $this->documentId; - } - - /** - * Method to set a single attribute - * - * Eg: :query - * - * @param string $key - * @param mixed $value - */ - public function setAttribute(string $key, $value) + return $this->documentId; + } + + /** + * Method to set a single attribute + * + * Eg: :query + * + * @param string $key + * @param mixed $value + */ + public function setAttribute(string $key, $value) { - $this->attributes->offsetSet($key,$value); - } - - /** - * Return the value for a single attribute. - * - * @param string $key - * @return mixed - */ - public function getAttribute(string $key) + $this->attributes->offsetSet($key, $value); + } + + /** + * Return the value for a single attribute. + * + * @param string $key + * @return mixed + */ + public function getAttribute(string $key) { - return $this->attributes->offsetGet($key); - } -} \ No newline at end of file + return $this->attributes->offsetGet($key); + } +} diff --git a/Classes/Domain/Result/Explanation/ExplainService.php b/Classes/Domain/Result/Explanation/ExplainService.php index d7c5969..30881cf 100644 --- a/Classes/Domain/Result/Explanation/ExplainService.php +++ b/Classes/Domain/Result/Explanation/ExplainService.php @@ -9,35 +9,34 @@ */ class ExplainService { - - /** - * @param string $explainContent - * @param string $documentId - * @param string $mode - * @return ExplainResult - */ - public static function getExplainFromRawContent(string $explainContent, string $documentId, string $mode): ExplainResult + /** + * @param string $explainContent + * @param string $documentId + * @param string $mode + * @return ExplainResult + */ + public static function getExplainFromRawContent(string $explainContent, string $documentId, string $mode): ExplainResult { - $content = new Content($explainContent); - $metaData = new MetaData($documentId, $mode); - $parser = new Parser(); - $parser->injectExplainResult(new ExplainResult()); + $content = new Content($explainContent); + $metaData = new MetaData($documentId, $mode); + $parser = new Parser(); + $parser->injectExplainResult(new ExplainResult()); - return $parser->parse($content,$metaData); - } + return $parser->parse($content, $metaData); + } - /** - * @param string $explainContent - * @param string $documentId - * @param string $mode - * @return float[] - */ - public static function getFieldImpactsFromRawContent(string $explainContent, string $documentId, string $mode): array + /** + * @param string $explainContent + * @param string $documentId + * @param string $mode + * @return float[] + */ + public static function getFieldImpactsFromRawContent(string $explainContent, string $documentId, string $mode): array { - $explain = self::getExplainFromRawContent($explainContent, $documentId, $mode); - $fieldImpactVisitor = new SummarizeFieldImpacts(); - $explain->getRootNode()->visitNodes($fieldImpactVisitor); + $explain = self::getExplainFromRawContent($explainContent, $documentId, $mode); + $fieldImpactVisitor = new SummarizeFieldImpacts(); + $explain->getRootNode()->visitNodes($fieldImpactVisitor); - return $fieldImpactVisitor->getFieldImpacts(); - } -} \ No newline at end of file + return $fieldImpactVisitor->getFieldImpacts(); + } +} diff --git a/Classes/Domain/Result/Explanation/MetaData.php b/Classes/Domain/Result/Explanation/MetaData.php index 64c85da..9ad0dd3 100644 --- a/Classes/Domain/Result/Explanation/MetaData.php +++ b/Classes/Domain/Result/Explanation/MetaData.php @@ -8,56 +8,55 @@ */ class MetaData { - - /** - * @var string - */ - protected $documentId = ''; - - /** - * @var string - */ - protected $mode = ''; - - /** - * @param string $documentId - * @param string $mode - */ - public function __construct(string $documentId, string $mode) + /** + * @var string + */ + protected $documentId = ''; + + /** + * @var string + */ + protected $mode = ''; + + /** + * @param string $documentId + * @param string $mode + */ + public function __construct(string $documentId, string $mode) { - $this->setDocumentId($documentId); - $this->setMode($mode); - } - - /** - * @param string $documentId - */ - public function setDocumentId(string $documentId) + $this->setDocumentId($documentId); + $this->setMode($mode); + } + + /** + * @param string $documentId + */ + public function setDocumentId(string $documentId) { - $this->documentId = $documentId; - } + $this->documentId = $documentId; + } - /** - * @return string - */ - public function getDocumentId(): string + /** + * @return string + */ + public function getDocumentId(): string { - return $this->documentId; - } + return $this->documentId; + } - /** - * @param string $mode - */ - public function setMode(string $mode) + /** + * @param string $mode + */ + public function setMode(string $mode) { - $this->mode = $mode; - } + $this->mode = $mode; + } - /** - * @return string - */ - public function getMode(): string + /** + * @return string + */ + public function getMode(): string { - return $this->mode; - } -} \ No newline at end of file + return $this->mode; + } +} diff --git a/Classes/Domain/Result/Explanation/Nodes/Explain.php b/Classes/Domain/Result/Explanation/Nodes/Explain.php index dd02ccf..ed2c159 100644 --- a/Classes/Domain/Result/Explanation/Nodes/Explain.php +++ b/Classes/Domain/Result/Explanation/Nodes/Explain.php @@ -10,287 +10,279 @@ */ class Explain { + /** + * Different types of nodes that need to be handled different during calculation + */ + public const NODE_TYPE_SUM = 1; + public const NODE_TYPE_MAX = 2; + public const NODE_TYPE_PRODUCT = 4; + public const NODE_TYPE_LEAF = 8; + + /** + * @var int + */ + protected $level = 0; + + /** + * @var string + */ + protected $content = ''; + + /** + * @var float + */ + protected $score = 0.0; + + /** + * @var ArrayObject + */ + protected $children; + + /** + * @var Explain + */ + protected $parent; + + /** + * @var int + */ + protected $nodeType = -1; - /** - * Different types of nodes that need to be handled different during calculation - */ - const NODE_TYPE_SUM = 1; - const NODE_TYPE_MAX = 2; - const NODE_TYPE_PRODUCT = 4; - const NODE_TYPE_LEAF = 8; - - /** - * @var int - */ - protected $level = 0; - - /** - * @var string - */ - protected $content = ''; - - /** - * @var float - */ - protected $score = 0.0; - - /** - * @var ArrayObject - */ - protected $children = null; - - /** - * @var Explain - */ - protected $parent = null; - - /** - * @var int - */ - protected $nodeType = -1; - - /** - * @var string - */ - protected $fieldName = '*'; - - /** - * @return void - */ - public function __construct() + /** + * @var string + */ + protected $fieldName = '*'; + + public function __construct() { - $this->children = new ArrayObject(); - } + $this->children = new ArrayObject(); + } /** * @return ?float|?int */ - public function getAbsoluteImpactPercentage() + public function getAbsoluteImpactPercentage() { - if($this->level == 0) { - return 100.0; - } + if ($this->level == 0) { + return 100.0; + } - if($this->getParent()->getNodeType() == self::NODE_TYPE_SUM) { + if ($this->getParent()->getNodeType() == self::NODE_TYPE_SUM) { return $this->handleSumParent(); } - if($this->getParent()->getNodeType() == self::NODE_TYPE_MAX) { + if ($this->getParent()->getNodeType() == self::NODE_TYPE_MAX) { return $this->handleMaxParent(); } - if($this->getParent()->getNodeType() == self::NODE_TYPE_PRODUCT) { + if ($this->getParent()->getNodeType() == self::NODE_TYPE_PRODUCT) { return $this->handleProductParent(); } return null; - } + } - /** - * @return float - */ - protected function handleProductParent() + /** + * @return float + */ + protected function handleProductParent() { - $neighbors = $this->getParent()->getChildren(); - - if ($neighbors->count() > 1) { - $neighborScorePart = []; - $parentPercentage = $this->getParent()->getAbsoluteImpactPercentage(); - - foreach ($neighbors as $neighbor) { - if ($neighbor != $this) { - $neighborScore = $neighbor->getScore(); - $neighborScorePart[] = $neighborScore; - } - } - - $scoreSum = array_sum($neighborScorePart) + $this->getScore(); - - $multiplier = 100 / $scoreSum; - $parentMultiplier = $parentPercentage / 100; - return $this->getScore() * $multiplier * $parentMultiplier; - } else { - //when only one leaf in product is present we can inherit the parent score - return $this->getParent()->getAbsoluteImpactPercentage(); - } - } - - /** - * @return float - */ - protected function handleSumParent() + $neighbors = $this->getParent()->getChildren(); + + if ($neighbors->count() > 1) { + $neighborScorePart = []; + $parentPercentage = $this->getParent()->getAbsoluteImpactPercentage(); + + foreach ($neighbors as $neighbor) { + if ($neighbor != $this) { + $neighborScore = $neighbor->getScore(); + $neighborScorePart[] = $neighborScore; + } + } + + $scoreSum = array_sum($neighborScorePart) + $this->getScore(); + + $multiplier = 100 / $scoreSum; + $parentMultiplier = $parentPercentage / 100; + return $this->getScore() * $multiplier * $parentMultiplier; + } + //when only one leaf in product is present we can inherit the parent score + return $this->getParent()->getAbsoluteImpactPercentage(); + } + + /** + * @return float + */ + protected function handleSumParent() { - $parentScore = $this->getParent()->getScore(); - $parentPercentage = $this->getParent()->getAbsoluteImpactPercentage(); - - //part of this node relative to the parent - $scorePercentageToParent = (100 / $parentScore) * $this->getScore(); - return ($parentPercentage / 100) * $scorePercentageToParent; - } - - /** - * @return float - */ - protected function handleMaxParent() + $parentScore = $this->getParent()->getScore(); + $parentPercentage = $this->getParent()->getAbsoluteImpactPercentage(); + + //part of this node relative to the parent + $scorePercentageToParent = (100 / $parentScore) * $this->getScore(); + return ($parentPercentage / 100) * $scorePercentageToParent; + } + + /** + * @return float + */ + protected function handleMaxParent() { - $neighbors = $this->getParent()->getChildren(); - $tieBreaker = $this->getParent()->getTieBreaker(); - - $parentScore = $this->getParent()->getScore(); - $parentScorePart = ($this->getScore() / $parentScore) * (100); - $parentScorePartPercentage = $this->getParent()->getAbsoluteImpactPercentage() / 100.0; - - $isMaxNode = true; - foreach ($neighbors as $neighbor) { - if ($neighbor != $this && $neighbor->getScore() > $this->getScore()) { - $isMaxNode = false; - break; - } - } - - if ($tieBreaker > 0) { - if ($isMaxNode) { - return $parentScorePart * $parentScorePartPercentage; - } else { - return $parentScorePart * $parentScorePartPercentage * $tieBreaker; - } - } else { - if ($isMaxNode) { - return $this->getParent()->getAbsoluteImpactPercentage(); - } else { - return 0.0; - } - } - } + $neighbors = $this->getParent()->getChildren(); + $tieBreaker = $this->getParent()->getTieBreaker(); + + $parentScore = $this->getParent()->getScore(); + $parentScorePart = ($this->getScore() / $parentScore) * (100); + $parentScorePartPercentage = $this->getParent()->getAbsoluteImpactPercentage() / 100.0; + + $isMaxNode = true; + foreach ($neighbors as $neighbor) { + if ($neighbor != $this && $neighbor->getScore() > $this->getScore()) { + $isMaxNode = false; + break; + } + } + + if ($tieBreaker > 0) { + if ($isMaxNode) { + return $parentScorePart * $parentScorePartPercentage; + } + return $parentScorePart * $parentScorePartPercentage * $tieBreaker; + } + if ($isMaxNode) { + return $this->getParent()->getAbsoluteImpactPercentage(); + } + return 0.0; + } /** * @param ?Explain $parent */ - public function setParent(?Explain $parent) + public function setParent(?Explain $parent) { - $this->parent = $parent; - } + $this->parent = $parent; + } - /** - * @return Explain - */ - public function getParent(): ?Explain + /** + * @return Explain + */ + public function getParent(): ?Explain { - return $this->parent; - } + return $this->parent; + } - /** - * @param string $content - */ - public function setContent(string $content) + /** + * @param string $content + */ + public function setContent(string $content) { - $this->content = $content; - } + $this->content = $content; + } - /** - * @return string - */ - public function getContent(): string + /** + * @return string + */ + public function getContent(): string { - return $this->content; - } + return $this->content; + } - /** - * @param int $level - */ - public function setLevel(int $level) + /** + * @param int $level + */ + public function setLevel(int $level) { - $this->level = $level; - } + $this->level = $level; + } - /** - * @param ArrayObject $children - */ - public function setChildren(ArrayObject $children) + /** + * @param ArrayObject $children + */ + public function setChildren(ArrayObject $children) { - $this->children = $children; - } + $this->children = $children; + } - /** - * @return ArrayObject - */ - public function getChildren(): ?ArrayObject + /** + * @return ArrayObject + */ + public function getChildren(): ?ArrayObject { - return $this->children; - } - - /** - * @param $index - * @return Explain - */ - public function getChild($index): Explain + return $this->children; + } + + /** + * @param $index + * @return Explain + */ + public function getChild($index): Explain { - return $this->children[$index]; - } + return $this->children[$index]; + } - /** - * @return int - */ - public function getLevel(): int + /** + * @return int + */ + public function getLevel(): int { - return $this->level; - } + return $this->level; + } - /** - * @param float $score - */ - public function setScore(float $score) + /** + * @param float $score + */ + public function setScore(float $score) { - $this->score = $score; - } + $this->score = $score; + } - /** - * @return float - */ - public function getScore(): float + /** + * @return float + */ + public function getScore(): float { - return $this->score; - } + return $this->score; + } - /** - * @param int $nodeType - */ - protected function setNodeType(int $nodeType) + /** + * @param int $nodeType + */ + protected function setNodeType(int $nodeType) { - $this->nodeType = $nodeType; - } + $this->nodeType = $nodeType; + } - /** - * @return int - */ - public function getNodeType(): int + /** + * @return int + */ + public function getNodeType(): int { - return $this->nodeType; - } + return $this->nodeType; + } - /** - * @param string $fieldName - */ - public function setFieldName(string $fieldName) + /** + * @param string $fieldName + */ + public function setFieldName(string $fieldName) { - $this->fieldName = $fieldName; - } + $this->fieldName = $fieldName; + } - /** - * @return string - */ - public function getFieldName(): string + /** + * @return string + */ + public function getFieldName(): string { - return $this->fieldName; - } - - /** - * This method can be used to traverse all child nodes. - * - * @param ExplainNodeVisitorInterface $visitor - */ - public function visitNodes(ExplainNodeVisitorInterface $visitor) + return $this->fieldName; + } + + /** + * This method can be used to traverse all child nodes. + * + * @param ExplainNodeVisitorInterface $visitor + */ + public function visitNodes(ExplainNodeVisitorInterface $visitor) { - $visitor->visit($this); - foreach($this->getChildren() as $child) { - $child->visitNodes($visitor); - } - } -} \ No newline at end of file + $visitor->visit($this); + foreach ($this->getChildren() as $child) { + $child->visitNodes($visitor); + } + } +} diff --git a/Classes/Domain/Result/Explanation/Nodes/Leaf.php b/Classes/Domain/Result/Explanation/Nodes/Leaf.php index eebaecf..bd4c697 100644 --- a/Classes/Domain/Result/Explanation/Nodes/Leaf.php +++ b/Classes/Domain/Result/Explanation/Nodes/Leaf.php @@ -4,14 +4,9 @@ class Leaf extends Explain { - - /** - * @return void - */ - public function __construct() + public function __construct() { - parent::__construct(); - $this->setNodeType(self::NODE_TYPE_LEAF); - } - -} \ No newline at end of file + parent::__construct(); + $this->setNodeType(self::NODE_TYPE_LEAF); + } +} diff --git a/Classes/Domain/Result/Explanation/Nodes/Max.php b/Classes/Domain/Result/Explanation/Nodes/Max.php index 52eacd9..a915b54 100644 --- a/Classes/Domain/Result/Explanation/Nodes/Max.php +++ b/Classes/Domain/Result/Explanation/Nodes/Max.php @@ -4,29 +4,23 @@ class Max extends Explain { - - /** - * @return void - */ - public function __construct() + public function __construct() { - parent::__construct(); - $this->setNodeType(self::NODE_TYPE_MAX); - } + parent::__construct(); + $this->setNodeType(self::NODE_TYPE_MAX); + } - /** - * @return float - */ - public function getTieBreaker() + /** + * @return float + */ + public function getTieBreaker() { + $matches = []; + preg_match('~plus (?.*) times~', $this->getContent(), $matches); + if (isset($matches['tiebreaker'])) { + return $matches['tiebreaker']; + } - $matches = []; - preg_match('~plus (?.*) times~',$this->getContent(),$matches); - if(isset($matches['tiebreaker'])) { - return $matches['tiebreaker']; - } - - return 0; - } - -} \ No newline at end of file + return 0; + } +} diff --git a/Classes/Domain/Result/Explanation/Nodes/Product.php b/Classes/Domain/Result/Explanation/Nodes/Product.php index 612879c..2822a42 100644 --- a/Classes/Domain/Result/Explanation/Nodes/Product.php +++ b/Classes/Domain/Result/Explanation/Nodes/Product.php @@ -4,14 +4,9 @@ class Product extends Explain { - - /** - * @return void - */ - public function __construct() + public function __construct() { - parent::__construct(); - $this->setNodeType(self::NODE_TYPE_PRODUCT); - } - -} \ No newline at end of file + parent::__construct(); + $this->setNodeType(self::NODE_TYPE_PRODUCT); + } +} diff --git a/Classes/Domain/Result/Explanation/Nodes/Sum.php b/Classes/Domain/Result/Explanation/Nodes/Sum.php index 5001ef8..3f44a03 100644 --- a/Classes/Domain/Result/Explanation/Nodes/Sum.php +++ b/Classes/Domain/Result/Explanation/Nodes/Sum.php @@ -4,13 +4,9 @@ class Sum extends Explain { - - /** - * @return void - */ - public function __construct() + public function __construct() { - parent::__construct(); - $this->setNodeType(self::NODE_TYPE_SUM); - } + parent::__construct(); + $this->setNodeType(self::NODE_TYPE_SUM); + } } diff --git a/Classes/Domain/Result/Explanation/Parser.php b/Classes/Domain/Result/Explanation/Parser.php index 4203aec..69d3625 100644 --- a/Classes/Domain/Result/Explanation/Parser.php +++ b/Classes/Domain/Result/Explanation/Parser.php @@ -14,38 +14,36 @@ */ class Parser { + /** + * @var ExplainResult + */ + protected $explain; - /** - * @var ExplainResult - */ - protected $explain; - - /** - * @param ExplainResult $explain - */ - public function injectExplainResult(ExplainResult $explain) + /** + * @param ExplainResult $explain + */ + public function injectExplainResult(ExplainResult $explain) { - $this->explain = $explain; - } - - /** - * Invokes the parsing of the raw content and returns the root node. - * - * @param string - * @return Explain - */ - protected function getRootNode($content): Explain + $this->explain = $explain; + } + + /** + * Invokes the parsing of the raw content and returns the root node. + * + * @param string + * @return Explain + */ + protected function getRootNode($content): Explain { - $tokens = new ArrayObject(); - $this->parseChildNodes($content,$tokens); + $tokens = new ArrayObject(); + $this->parseChildNodes($content, $tokens); - if(isset($tokens[0])) { - return $tokens[0]; - } else { - //error in parsing return a new empty dummy node - return new Explain(); - } - } + if (isset($tokens[0])) { + return $tokens[0]; + } + //error in parsing return a new empty dummy node + return new Explain(); + } /** * This method is used to parse the node type from the content and retrieve the @@ -54,23 +52,23 @@ protected function getRootNode($content): Explain * @param string $tokenName * @return Leaf|Max|Product|Sum */ - protected function getNodeFromName(string $tokenName): Explain + protected function getNodeFromName(string $tokenName): Explain { - if(mb_strpos($tokenName,'sum of:') !== false || mb_strpos($tokenName,'result of:') !== false) { - return new Sum(); - } + if (mb_strpos($tokenName, 'sum of:') !== false || mb_strpos($tokenName, 'result of:') !== false) { + return new Sum(); + } - if(mb_strpos($tokenName,'product of:') !== false) { - return new Product(); - } + if (mb_strpos($tokenName, 'product of:') !== false) { + return new Product(); + } - if(mb_strpos($tokenName,'max') !== false && mb_strpos($tokenName,'of:')) { - return new Max(); - } + if (mb_strpos($tokenName, 'max') !== false && mb_strpos($tokenName, 'of:')) { + return new Max(); + } - //when nothing else matched we have a leaf node - return new Leaf(); - } + //when nothing else matched we have a leaf node + return new Leaf(); + } /** * Recursive method to parse the explain content into a child node structure. @@ -80,149 +78,152 @@ protected function getNodeFromName(string $tokenName): Explain * @param Explain|null $parent * @param int $level */ - protected function parseChildNodes(string $contextContent, ArrayObject $collection, Explain $parent = null, int $level = 0) + protected function parseChildNodes(string $contextContent, ArrayObject $collection, Explain $parent = null, int $level = 0) + { + $matches = []; + + //look for tokens stating with 0-9* and get all following lines stating with " " space + preg_match_all('~((?<=^)|(?<=\n))(?[0-9].*?\n)((?=[0-9])|(?=$))~s', $contextContent, $matches); + + if (array_key_exists('token', $matches)) { + foreach ($matches['token'] as $tokenKey => $tokenContent) { + $nodeParts = explode(PHP_EOL, $tokenContent); + $nodeContent = trim(array_shift($nodeParts)); + $node = $this->getNodeFromName($nodeContent); + $score = $this->getScoreFromContent($nodeContent); + $nodeFieldName = $this->getFieldNameFromNodeName($nodeContent); + + $node->setContent($nodeContent); + $node->setParent($parent); + $node->setScore($score); + $node->setLevel($level); + $node->setFieldName($nodeFieldName); + $collection->append($node); + + $nextLevelContent = $this->removeLeadingSpacesFromNextLevelContent($nodeParts); + + if (trim($nextLevelContent) != '') { + $level++; + //walk recursive through the input + $this->parseChildNodes($nextLevelContent, $node->getChildren(), $node, $level); + } + } + } + } + + /** + * @param $tokenParts + * @return string + */ + protected function removeLeadingSpacesFromNextLevelContent($tokenParts) + { + $nextLevelContent = ''; + if (count($tokenParts)) { + $preparedTokens = preg_replace('~^ ~ims', '', $tokenParts); + $nextLevelContent = implode(PHP_EOL, $preparedTokens); + return trim($nextLevelContent) . PHP_EOL; + } + return $nextLevelContent; + } + + /** + * Extracts the score from a token name. + * + * Input eg: 3.8332133 = idf(docFreq=0, maxDocs=17) + * Output eg: 3.8332133 + * + * @param string $nodeName + * @return float + */ + protected function getScoreFromContent($nodeName) + { + $score = 0.0; + $scoreMatches = []; + preg_match('~(?[0-9]*\.[^ ]*)~', $nodeName, $scoreMatches); + if (isset($scoreMatches['score']) && (float)$scoreMatches['score'] > 0) { + $score = (float)$scoreMatches['score']; + } + + return $score; + } + + /** + * @param $content + * @return string + */ + protected function getQueryAttribute($content) + { + $querystring = ''; + $matches = []; + preg_match("~^#(?[^\n]*)~ism", $content, $matches); + + if (isset($matches['attributes'])) { + $attributes = $matches['attributes']; + $attributeMatches = []; + preg_match('~.*q=(?[^&]*)~ism', $attributes, $attributeMatches); + if (isset($attributeMatches['querystring'])) { + $querystring = $attributeMatches['querystring']; + //convert boostvalues without decimals to boost value with + //decimal eg: foo^20 => foo^20.0 + $querystring = preg_replace('~\^([0-9^.]+)~i', '^$1.0', $querystring); + } + } + + return $querystring; + } + + /** + * @param $nodeName + */ + protected function getFieldNameFromNodeName($nodeName) { - $matches = []; - - //look for tokens stating with 0-9* and get all following lines stating with " " space - preg_match_all('~((?<=^)|(?<=\n))(?[0-9].*?\n)((?=[0-9])|(?=$))~s', $contextContent,$matches); - - if( array_key_exists('token',$matches)) { - - foreach($matches['token'] as $tokenKey => $tokenContent) { - $nodeParts = explode(PHP_EOL,$tokenContent); - $nodeContent = trim(array_shift($nodeParts)); - $node = $this->getNodeFromName($nodeContent); - $score = $this->getScoreFromContent($nodeContent); - $nodeFieldName = $this->getFieldNameFromNodeName($nodeContent); - - $node->setContent($nodeContent); - $node->setParent($parent); - $node->setScore($score); - $node->setLevel($level); - $node->setFieldName($nodeFieldName); - $collection->append($node); - - $nextLevelContent = $this->removeLeadingSpacesFromNextLevelContent($nodeParts); - - if(trim($nextLevelContent) != '') { - $level++; - //walk recursive through the input - $this->parseChildNodes($nextLevelContent,$node->getChildren(),$node,$level); - } - } - } - } - - /** - * @param $tokenParts - * @return string - */ - protected function removeLeadingSpacesFromNextLevelContent($tokenParts) { - $nextLevelContent = ''; - if (count($tokenParts)) { - $preparedTokens = preg_replace('~^ ~ims', '', $tokenParts); - $nextLevelContent = implode(PHP_EOL, $preparedTokens); - return trim($nextLevelContent).PHP_EOL; - } - return $nextLevelContent; - } - - /** - * Extracts the score from a token name. - * - * Input eg: 3.8332133 = idf(docFreq=0, maxDocs=17) - * Output eg: 3.8332133 - * - * @param string $nodeName - * @return float - */ - protected function getScoreFromContent($nodeName) { - $score = 0.0; - $scoreMatches = []; - preg_match('~(?[0-9]*\.[^ ]*)~',$nodeName,$scoreMatches); - if(isset($scoreMatches['score']) && (float) $scoreMatches['score'] > 0) { - $score = (float) $scoreMatches['score']; - } - - return $score; - } - - /** - * @param $content - * @return string - */ - protected function getQueryAttribute($content) { - $querystring = ''; - $matches = []; - preg_match("~^#(?[^\n]*)~ism", $content, $matches); - - if(isset($matches['attributes'])) { - $attributes = $matches['attributes']; - $attributeMatches = []; - preg_match('~.*q=(?[^&]*)~ism',$attributes,$attributeMatches); - if(isset($attributeMatches['querystring'])) { - $querystring = $attributeMatches['querystring']; - //convert boostvalues without decimals to boost value with - //decimal eg: foo^20 => foo^20.0 - $querystring = preg_replace('~\^([0-9^.]+)~i','^$1.0',$querystring); - } - } - - return $querystring; - } - - /** - * @param $nodeName - */ - protected function getFieldNameFromNodeName($nodeName) { - $result = ''; - $matches = []; - if (mb_strpos($nodeName,'weight(Synonym(') !== false ) { + $result = ''; + $matches = []; + if (mb_strpos($nodeName, 'weight(Synonym(') !== false) { preg_match('~weight\(Synonym\((?[^\):]*)~', $nodeName, $matches); - } elseif(mb_strpos($nodeName,'weight(') !== false ){ - preg_match('~weight\((?[^\):]*)~', $nodeName, $matches); - } elseif (mb_strpos($nodeName, 'queryWeight(') !== false ) { - preg_match('~queryWeight\((?[^\):]*)~', $nodeName, $matches); - } elseif (mb_strpos($nodeName,'fieldWeight(') !== false ) { - preg_match('~fieldWeight\((?[^\):]*)~', $nodeName, $matches); - } elseif (mb_strpos($nodeName, 'FunctionQuery(') !== false ) { - preg_match('~FunctionQuery\([^\(]*\((?[^\):]*)~', $nodeName, $matches); - - //check if it is a nested function query an get inner fieldname - $lastBracketPos = mb_strpos($matches['fieldname'],'('); - if($lastBracketPos !== false) { - $fieldMatch = mb_substr($matches['fieldname'], $lastBracketPos+1, mb_strlen($matches['fieldname'])); - if(!is_numeric($fieldMatch)) { - $matches['fieldname'] = $fieldMatch; - } - } - } - - if(isset($matches['fieldname'])) { - $result = $matches['fieldname']; - } - - return $result; - } - - /** - * Parses the explain content to an explain object wit child nodes. - * - * @return ExplainResult - */ - public function parse(Content $content, MetaData $metaData) { - - $rawContent = $content->getContent(); - $rootNode = $this->getRootNode($rawContent.PHP_EOL); - $this->explain->setRootNode($rootNode); - - $children = $rootNode->getChildren(); - $this->explain->setChildren($children); - - $this->explain->setDocumentId($metaData->getDocumentId()); - $this->explain->setAttribute(':query',$this->getQueryAttribute($rawContent)); - - return $this->explain; - } -} \ No newline at end of file + } elseif (mb_strpos($nodeName, 'weight(') !== false) { + preg_match('~weight\((?[^\):]*)~', $nodeName, $matches); + } elseif (mb_strpos($nodeName, 'queryWeight(') !== false) { + preg_match('~queryWeight\((?[^\):]*)~', $nodeName, $matches); + } elseif (mb_strpos($nodeName, 'fieldWeight(') !== false) { + preg_match('~fieldWeight\((?[^\):]*)~', $nodeName, $matches); + } elseif (mb_strpos($nodeName, 'FunctionQuery(') !== false) { + preg_match('~FunctionQuery\([^\(]*\((?[^\):]*)~', $nodeName, $matches); + + //check if it is a nested function query an get inner fieldname + $lastBracketPos = mb_strpos($matches['fieldname'], '('); + if ($lastBracketPos !== false) { + $fieldMatch = mb_substr($matches['fieldname'], $lastBracketPos + 1, mb_strlen($matches['fieldname'])); + if (!is_numeric($fieldMatch)) { + $matches['fieldname'] = $fieldMatch; + } + } + } + + if (isset($matches['fieldname'])) { + $result = $matches['fieldname']; + } + + return $result; + } + + /** + * Parses the explain content to an explain object wit child nodes. + * + * @return ExplainResult + */ + public function parse(Content $content, MetaData $metaData) + { + $rawContent = $content->getContent(); + $rootNode = $this->getRootNode($rawContent . PHP_EOL); + $this->explain->setRootNode($rootNode); + + $children = $rootNode->getChildren(); + $this->explain->setChildren($children); + + $this->explain->setDocumentId($metaData->getDocumentId()); + $this->explain->setAttribute(':query', $this->getQueryAttribute($rawContent)); + + return $this->explain; + } +} diff --git a/Classes/Domain/Result/Explanation/Visitors/ExplainNodeVisitorInterface.php b/Classes/Domain/Result/Explanation/Visitors/ExplainNodeVisitorInterface.php index 950fd67..1deb06a 100644 --- a/Classes/Domain/Result/Explanation/Visitors/ExplainNodeVisitorInterface.php +++ b/Classes/Domain/Result/Explanation/Visitors/ExplainNodeVisitorInterface.php @@ -7,13 +7,11 @@ /** * Visitor interface, needs to be implemented by a node visitor. */ - interface ExplainNodeVisitorInterface { - - /** - * @param Explain $node - * @return mixed - */ - public function visit(Explain $node); + /** + * @param Explain $node + * @return mixed + */ + public function visit(Explain $node); } diff --git a/Classes/Domain/Result/Explanation/Visitors/SummarizeFieldImpacts.php b/Classes/Domain/Result/Explanation/Visitors/SummarizeFieldImpacts.php index b611a1c..bd65cb4 100644 --- a/Classes/Domain/Result/Explanation/Visitors/SummarizeFieldImpacts.php +++ b/Classes/Domain/Result/Explanation/Visitors/SummarizeFieldImpacts.php @@ -10,34 +10,31 @@ */ class SummarizeFieldImpacts implements ExplainNodeVisitorInterface { + /** + * @var float[] + */ + protected $sums = []; - /** - * @var float[] - */ - protected $sums = []; - - /** - * @param Explain $node - * @return void - */ - public function visit(Explain $node) + /** + * @param Explain $node + */ + public function visit(Explain $node) { - if($node->getNodeType() == $node::NODE_TYPE_LEAF) { - if($node->getParent() != null) { - $fieldName = $this->getClosestFieldName($node); - if(trim($fieldName) === '') { + if ($node->getNodeType() == $node::NODE_TYPE_LEAF) { + if ($node->getParent() != null) { + $fieldName = $this->getClosestFieldName($node); + if (trim($fieldName) === '') { return; } - if(!isset($this->sums[$fieldName])) { - $this->sums[$fieldName] = 0; - } - - $this->sums[$fieldName] += $node->getAbsoluteImpactPercentage(); + if (!isset($this->sums[$fieldName])) { + $this->sums[$fieldName] = 0; + } - } - } - } + $this->sums[$fieldName] += $node->getAbsoluteImpactPercentage(); + } + } + } /** * Returns the closest fieldname in the parent root line and and empty string when none is present. @@ -45,9 +42,9 @@ public function visit(Explain $node) * @param $node * @return string */ - protected function getClosestFieldName($node): string + protected function getClosestFieldName($node): string { - while(!is_null($node->getParent())) { + while (!is_null($node->getParent())) { $parent = $node->getParent(); if ($parent->getFieldName() !== '') { return $parent->getFieldName(); @@ -59,38 +56,38 @@ protected function getClosestFieldName($node): string return ''; } - /** - * Returns the fieldnames that have been relevant during the explain. - * - * @return string[] - */ - public function getRelevantFieldNames(): array + /** + * Returns the fieldnames that have been relevant during the explain. + * + * @return string[] + */ + public function getRelevantFieldNames(): array { - return array_keys($this->sums); - } + return array_keys($this->sums); + } - /** - * Returns the impact for a certain field by name. - * - * @param $fieldName - * @return float - */ - public function getFieldImpact($fieldName): float + /** + * Returns the impact for a certain field by name. + * + * @param $fieldName + * @return float + */ + public function getFieldImpact($fieldName): float { - if(!array_key_exists($fieldName,$this->sums)) { - return 0.0; - } + if (!array_key_exists($fieldName, $this->sums)) { + return 0.0; + } - return $this->sums[$fieldName]; - } + return $this->sums[$fieldName]; + } - /** - * Returns the fieldname => impact array. - * - * @return float[] + /** + * Returns the fieldname => impact array. + * + * @return float[] */ - public function getFieldImpacts(): array + public function getFieldImpacts(): array { - return $this->sums; - } + return $this->sums; + } } diff --git a/Classes/Domain/Result/Explanation/Visitors/SummarizeLeafImpacts.php b/Classes/Domain/Result/Explanation/Visitors/SummarizeLeafImpacts.php index 9725627..044b728 100644 --- a/Classes/Domain/Result/Explanation/Visitors/SummarizeLeafImpacts.php +++ b/Classes/Domain/Result/Explanation/Visitors/SummarizeLeafImpacts.php @@ -11,28 +11,26 @@ */ class SummarizeLeafImpacts implements ExplainNodeVisitorInterface { + /** + * @var float + */ + protected $sum = 0.0; - /** - * @var float - */ - protected $sum = 0.0; - - /** - * @param Explain $node - * @return void - */ - public function visit(Explain $node) + /** + * @param Explain $node + */ + public function visit(Explain $node) { - if($node->getNodeType() == $node::NODE_TYPE_LEAF) { - $this->sum += $node->getAbsoluteImpactPercentage(); - } - } + if ($node->getNodeType() == $node::NODE_TYPE_LEAF) { + $this->sum += $node->getAbsoluteImpactPercentage(); + } + } - /** - * @return float - */ - public function getSum(): float + /** + * @return float + */ + public function getSum(): float { - return round($this->sum,1); - } -} \ No newline at end of file + return round($this->sum, 1); + } +} diff --git a/Classes/Domain/Result/Parser.php b/Classes/Domain/Result/Parser.php index 3c788b5..6c9918a 100644 --- a/Classes/Domain/Result/Parser.php +++ b/Classes/Domain/Result/Parser.php @@ -13,260 +13,261 @@ use DOMNodeList; use DOMXPath; -class Parser { - - /** - * @var - */ - protected $explainNodes; - - /** - * @param string $xml - * @return Result - */ - public function parse(string $xml): Result +class Parser +{ + /** + * @var + */ + protected $explainNodes; + + /** + * @param string $xml + * @return Result + */ + public function parse(string $xml): Result { - $result = new Result(); - $dom = new DOMDocument(1.0,"UTF-8"); - $dom->loadXML(trim($xml)); + $result = new Result(); + $dom = new DOMDocument(1.0, 'UTF-8'); + $dom->loadXML(trim($xml)); - $xpath = new DOMXPath($dom); + $xpath = new DOMXPath($dom); - $completeResultCount = $this->extractCompleteResultCount($xpath); - $result->setCompleteResultCount($completeResultCount); + $completeResultCount = $this->extractCompleteResultCount($xpath); + $result->setCompleteResultCount($completeResultCount); - $queryTime = $this->extractQueryTime($xpath); - $result->setQueryTime($queryTime); + $queryTime = $this->extractQueryTime($xpath); + $result->setQueryTime($queryTime); - $documentCollection = $this->extractDocumentCollection($xpath); - $result->setDocumentCollection($documentCollection); + $documentCollection = $this->extractDocumentCollection($xpath); + $result->setDocumentCollection($documentCollection); - $timing = $this->extractTiming($xpath); - $result->setTiming($timing); + $timing = $this->extractTiming($xpath); + $result->setTiming($timing); - $queryParserName = $this->extractParserName($xpath); - $result->setQueryParser($queryParserName); + $queryParserName = $this->extractParserName($xpath); + $result->setQueryParser($queryParserName); - return $result; - } + return $result; + } - /** - * @param DOMXPath $resultXpath - * @return int - */ - protected function extractCompleteResultCount(DOMXPath $resultXpath): int + /** + * @param DOMXPath $resultXpath + * @return int + */ + protected function extractCompleteResultCount(DOMXPath $resultXpath): int { - $result = 0; - $numFound = $resultXpath->query("//response/result/@numFound"); + $result = 0; + $numFound = $resultXpath->query('//response/result/@numFound'); - if(isset($numFound->item(0)->textContent)) { - $result = (int) $numFound->item(0)->textContent; - } + if (isset($numFound->item(0)->textContent)) { + $result = (int)$numFound->item(0)->textContent; + } - return $result; - } + return $result; + } - /** - * @param DOMXPath $resultXpath - * @return int - */ - protected function extractQueryTime(DOMXPath $resultXpath): int + /** + * @param DOMXPath $resultXpath + * @return int + */ + protected function extractQueryTime(DOMXPath $resultXpath): int { - $result = 0; - $responseTime = $resultXpath->query("//lst[@name='responseHeader']/int[@name='QTime']"); + $result = 0; + $responseTime = $resultXpath->query("//lst[@name='responseHeader']/int[@name='QTime']"); - if(isset($responseTime->item(0)->textContent)) { - $result = (int) $responseTime->item(0)->textContent; - } + if (isset($responseTime->item(0)->textContent)) { + $result = (int)$responseTime->item(0)->textContent; + } - return $result; - } + return $result; + } - /** - * @param DOMXPath $resultXpath - * @return Collection - */ - protected function extractDocumentCollection(DOMXPath $resultXpath): Collection + /** + * @param DOMXPath $resultXpath + * @return Collection + */ + protected function extractDocumentCollection(DOMXPath $resultXpath): Collection { - $result = new Collection(); - $documentNodes = $resultXpath->query("//doc"); - $documentCount = 0; - - foreach($documentNodes as $documentNode) { - $document = new Document(); - - /* @var DOMElement $documentNode */ - foreach($documentNode->childNodes as $fieldNode) { - $this->extractDocumentFields($fieldNode, $document); - } - - $explainContent = $this->extractExplainContent($resultXpath, $documentCount); - $document->setRawExplainData($explainContent); - - $result->append($document); - $documentCount++; - } - - return $result; - } - - /** - * This method is used to extract the fields from the xml response - * and attach them to the document object. - * - * @param $fieldNode - * @param Document $document - */ - protected function extractDocumentFields($fieldNode, Document $document) { - if ($fieldNode instanceof DOMElement) { - $field = new Field(); - - if ($fieldNode->nodeName == 'arr') { - //multivalue field - $value = []; - foreach ($fieldNode->childNodes as $singleField) { - $value[] = $singleField->textContent; - } - } else { - //single value field - $value = $fieldNode->textContent; - } - - $field->setValue($value); - - $fieldName = $fieldNode->getAttribute('name'); - $field->setName($fieldName); - - $document->addField($field); - } - } - - /** - * @param DOMXPath $resultXPath - * @return DOMNodeList|false a DOMNodeList containing all nodes matching + $result = new Collection(); + $documentNodes = $resultXpath->query('//doc'); + $documentCount = 0; + + foreach ($documentNodes as $documentNode) { + $document = new Document(); + + /* @var DOMElement $documentNode */ + foreach ($documentNode->childNodes as $fieldNode) { + $this->extractDocumentFields($fieldNode, $document); + } + + $explainContent = $this->extractExplainContent($resultXpath, $documentCount); + $document->setRawExplainData($explainContent); + + $result->append($document); + $documentCount++; + } + + return $result; + } + + /** + * This method is used to extract the fields from the xml response + * and attach them to the document object. + * + * @param $fieldNode + * @param Document $document + */ + protected function extractDocumentFields($fieldNode, Document $document) + { + if ($fieldNode instanceof DOMElement) { + $field = new Field(); + + if ($fieldNode->nodeName == 'arr') { + //multivalue field + $value = []; + foreach ($fieldNode->childNodes as $singleField) { + $value[] = $singleField->textContent; + } + } else { + //single value field + $value = $fieldNode->textContent; + } + + $field->setValue($value); + + $fieldName = $fieldNode->getAttribute('name'); + $field->setName($fieldName); + + $document->addField($field); + } + } + + /** + * @param DOMXPath $resultXPath + * @return DOMNodeList|false a DOMNodeList containing all nodes matching * the given XPath expression. Any expression which does not return nodes * will return an empty DOMNodeList. The return is false if the expression * is malformed or the contextnode is invalid. - */ - protected function getExplainNodes(DOMXPath $resultXPath) + */ + protected function getExplainNodes(DOMXPath $resultXPath) { - if($this->explainNodes == null) { - $this->explainNodes = $resultXPath->query("//lst[@name='debug']/lst[@name='explain']/str"); - } + if ($this->explainNodes == null) { + $this->explainNodes = $resultXPath->query("//lst[@name='debug']/lst[@name='explain']/str"); + } - return $this->explainNodes; - } + return $this->explainNodes; + } /** * @param DOMXPath $resultXPath * @param * @return string */ - protected function extractExplainContent(DOMXPath $resultXPath, $documentCount): string + protected function extractExplainContent(DOMXPath $resultXPath, $documentCount): string { - $explainContent = ''; + $explainContent = ''; - $explainNodes = $this->getExplainNodes($resultXPath); + $explainNodes = $this->getExplainNodes($resultXPath); - if(isset($explainNodes->item($documentCount)->textContent)) { - $explainContent = $explainNodes->item($documentCount)->textContent; - } + if (isset($explainNodes->item($documentCount)->textContent)) { + $explainContent = $explainNodes->item($documentCount)->textContent; + } - return $explainContent; - } + return $explainContent; + } - /** - * @param DOMXPath $xpath - * @return Timing - */ - protected function extractTiming(DOMXPath $xpath): Timing + /** + * @param DOMXPath $xpath + * @return Timing + */ + protected function extractTiming(DOMXPath $xpath): Timing { - $prepareItemCollection = new ItemCollection(); - $processingItemCollection = new ItemCollection(); - - $path = "//lst[@name='debug']/lst[@name='timing']/*[@name='time']"; - $overallTime = $this->getTimeFromNode($xpath, $path); - - //get prepare timing and items - $path = "//lst[@name='debug']/lst[@name='timing']/lst[@name='prepare']/*[@name='time']"; - $prepareTime = $this->getTimeFromNode($xpath, $path); - $prepareItemCollection->setTimeSpend($prepareTime); - - $prepareNodesPath = "//lst[@name='debug']/lst[@name='timing']/lst[@name='prepare']/lst"; - $this->extractTimingSubNodes($xpath, $prepareNodesPath, $prepareItemCollection); - - //get processing time and items - $path = "//lst[@name='debug']/lst[@name='timing']/lst[@name='process']/*[@name='time']"; - $processingTime = $this->getTimeFromNode($xpath, $path); - $processingItemCollection->setTimeSpend($processingTime); - - $processingNodesPath = "//lst[@name='debug']/lst[@name='timing']/lst[@name='process']/lst"; - $this->extractTimingSubNodes($xpath, $processingNodesPath, $processingItemCollection); - - //build all and return - $result = new Timing($prepareItemCollection, $processingItemCollection); - $result->setTimeSpend($overallTime); - - return $result; - } - - /** - * This method is used to build timing items from timing subnodes. - * - * @param DOMXPath $xpath - * @param string $nodeXPath - * @param ItemCollection $itemCollection - */ - protected function extractTimingSubNodes(DOMXPath $xpath, string $nodeXPath, ItemCollection $itemCollection) + $prepareItemCollection = new ItemCollection(); + $processingItemCollection = new ItemCollection(); + + $path = "//lst[@name='debug']/lst[@name='timing']/*[@name='time']"; + $overallTime = $this->getTimeFromNode($xpath, $path); + + //get prepare timing and items + $path = "//lst[@name='debug']/lst[@name='timing']/lst[@name='prepare']/*[@name='time']"; + $prepareTime = $this->getTimeFromNode($xpath, $path); + $prepareItemCollection->setTimeSpend($prepareTime); + + $prepareNodesPath = "//lst[@name='debug']/lst[@name='timing']/lst[@name='prepare']/lst"; + $this->extractTimingSubNodes($xpath, $prepareNodesPath, $prepareItemCollection); + + //get processing time and items + $path = "//lst[@name='debug']/lst[@name='timing']/lst[@name='process']/*[@name='time']"; + $processingTime = $this->getTimeFromNode($xpath, $path); + $processingItemCollection->setTimeSpend($processingTime); + + $processingNodesPath = "//lst[@name='debug']/lst[@name='timing']/lst[@name='process']/lst"; + $this->extractTimingSubNodes($xpath, $processingNodesPath, $processingItemCollection); + + //build all and return + $result = new Timing($prepareItemCollection, $processingItemCollection); + $result->setTimeSpend($overallTime); + + return $result; + } + + /** + * This method is used to build timing items from timing subnodes. + * + * @param DOMXPath $xpath + * @param string $nodeXPath + * @param ItemCollection $itemCollection + */ + protected function extractTimingSubNodes(DOMXPath $xpath, string $nodeXPath, ItemCollection $itemCollection) { - $nodes = $xpath->query($nodeXPath); - - foreach ($nodes as $node) { - /** @var $node DOMElement */ - $name = $node->getAttribute('name'); - $time = 0.0; - if (isset($node->childNodes->item(0)->textContent)) { - $time = (float)$node->childNodes->item(0)->textContent; - } - - $item = new Item(); - $item->setComponentName($name); - $item->setTimeSpend($time); - - $itemCollection->append($item); - } - } - - /** - * @param DOMXPath $xpath - * @param string $path - * @return float - */ - protected function getTimeFromNode(DOMXPath $xpath, string $path): float + $nodes = $xpath->query($nodeXPath); + + foreach ($nodes as $node) { + /** @var $node DOMElement */ + $name = $node->getAttribute('name'); + $time = 0.0; + if (isset($node->childNodes->item(0)->textContent)) { + $time = (float)$node->childNodes->item(0)->textContent; + } + + $item = new Item(); + $item->setComponentName($name); + $item->setTimeSpend($time); + + $itemCollection->append($item); + } + } + + /** + * @param DOMXPath $xpath + * @param string $path + * @return float + */ + protected function getTimeFromNode(DOMXPath $xpath, string $path): float { - $timeNode = $xpath->query($path); - $time = 0.0; - if (isset($timeNode->item(0)->textContent)) { - $time = (float)$timeNode->item(0)->textContent; - } + $timeNode = $xpath->query($path); + $time = 0.0; + if (isset($timeNode->item(0)->textContent)) { + $time = (float)$timeNode->item(0)->textContent; + } - return $time; - } + return $time; + } /** * @param DOMXPath $xpath * @return string */ - protected function extractParserName(DOMXPath $xpath): string + protected function extractParserName(DOMXPath $xpath): string { - $result = ''; - $path = "//lst[@name='debug']/str[@name='QParser']"; - $queryParserNode = $xpath->query($path); + $result = ''; + $path = "//lst[@name='debug']/str[@name='QParser']"; + $queryParserNode = $xpath->query($path); - if(isset($queryParserNode->item(0)->textContent)) { - $result = $queryParserNode->item(0)->textContent; - } + if (isset($queryParserNode->item(0)->textContent)) { + $result = $queryParserNode->item(0)->textContent; + } - return $result; - } + return $result; + } } diff --git a/Classes/Domain/Result/Result.php b/Classes/Domain/Result/Result.php index 5a8baef..fc88233 100644 --- a/Classes/Domain/Result/Result.php +++ b/Classes/Domain/Result/Result.php @@ -5,121 +5,124 @@ use ApacheSolrForTypo3\SolrExplain\Domain\Result\Document\Collection; use ApacheSolrForTypo3\SolrExplain\Domain\Result\Timing\Timing; -class Result { - - /** - * @var int - */ - protected $completeResultCount = 0; - - /** - * @var int - */ - protected $queryTime = 0; - - /** - * @var string - */ - protected $queryParser = ''; - - /** - * @var Collection - */ - protected $documentCollection; - - /** - * @var Timing - */ - protected $timing = null; - - /** - * @param int $completeResultCount - * @return self - */ - public function setCompleteResultCount(int $completeResultCount): Result +class Result +{ + /** + * @var int + */ + protected $completeResultCount = 0; + + /** + * @var int + */ + protected $queryTime = 0; + + /** + * @var string + */ + protected $queryParser = ''; + + /** + * @var Collection + */ + protected $documentCollection; + + /** + * @var Timing + */ + protected $timing; + + /** + * @param int $completeResultCount + * @return self + */ + public function setCompleteResultCount(int $completeResultCount): Result { - $this->completeResultCount = $completeResultCount; + $this->completeResultCount = $completeResultCount; - return $this; - } + return $this; + } - /** - * @return int - */ - public function getCompleteResultCount(): int + /** + * @return int + */ + public function getCompleteResultCount(): int { - return $this->completeResultCount; - } + return $this->completeResultCount; + } - /** - * @return int - */ - public function getResultCount(): int + /** + * @return int + */ + public function getResultCount(): int { - return $this->documentCollection->count(); - } - - /** - * @param int $queryTime - * @return self - */ - public function setQueryTime(int $queryTime): Result + return $this->documentCollection->count(); + } + + /** + * @param int $queryTime + * @return self + */ + public function setQueryTime(int $queryTime): Result { - $this->queryTime = $queryTime; + $this->queryTime = $queryTime; - return $this; - } + return $this; + } - /** - * @return int - */ - public function getQueryTime(): int + /** + * @return int + */ + public function getQueryTime(): int { - return $this->queryTime; - } - - /** - * @param Collection $documentCollection - */ - public function setDocumentCollection(Collection $documentCollection) { - $this->documentCollection = $documentCollection; - } - - /** - * @return Collection - */ - public function getDocumentCollection(): Collection + return $this->queryTime; + } + + /** + * @param Collection $documentCollection + */ + public function setDocumentCollection(Collection $documentCollection) { - return $this->documentCollection; - } - - /** - * @param Timing $timing - */ - public function setTiming(Timing $timing) { - $this->timing = $timing; - } - - /** - * @return Timing - */ - public function getTiming(): ?Timing + $this->documentCollection = $documentCollection; + } + + /** + * @return Collection + */ + public function getDocumentCollection(): Collection { - return $this->timing; - } - - /** - * @param string $queryParser - */ - public function setQueryParser(string $queryParser) { - $this->queryParser = $queryParser; - } - - /** - * @return string - */ - public function getQueryParser(): string + return $this->documentCollection; + } + + /** + * @param Timing $timing + */ + public function setTiming(Timing $timing) + { + $this->timing = $timing; + } + + /** + * @return Timing + */ + public function getTiming(): ?Timing + { + return $this->timing; + } + + /** + * @param string $queryParser + */ + public function setQueryParser(string $queryParser) + { + $this->queryParser = $queryParser; + } + + /** + * @return string + */ + public function getQueryParser(): string { - return $this->queryParser; - } -} \ No newline at end of file + return $this->queryParser; + } +} diff --git a/Classes/Domain/Result/Timing/Item.php b/Classes/Domain/Result/Timing/Item.php index 432034b..3778741 100644 --- a/Classes/Domain/Result/Timing/Item.php +++ b/Classes/Domain/Result/Timing/Item.php @@ -2,53 +2,53 @@ namespace ApacheSolrForTypo3\SolrExplain\Domain\Result\Timing; -class Item { - - /** - * @var float - */ - protected $timeSpend = 0.0; - - /** - * @var string - */ - protected $componentName = ''; - - /** - * @param string $componentName - * @return self - */ - public function setComponentName(string $componentName): Item +class Item +{ + /** + * @var float + */ + protected $timeSpend = 0.0; + + /** + * @var string + */ + protected $componentName = ''; + + /** + * @param string $componentName + * @return self + */ + public function setComponentName(string $componentName): Item { - $this->componentName = $componentName; + $this->componentName = $componentName; - return $this; - } + return $this; + } - /** - * @return string - */ - public function getComponentName(): string + /** + * @return string + */ + public function getComponentName(): string { - return $this->componentName; - } - - /** - * @param float $timeSpend - * @return self - */ - public function setTimeSpend(float $timeSpend): Item + return $this->componentName; + } + + /** + * @param float $timeSpend + * @return self + */ + public function setTimeSpend(float $timeSpend): Item { - $this->timeSpend = $timeSpend; + $this->timeSpend = $timeSpend; - return $this; - } + return $this; + } - /** - * @return float - */ - public function getTimeSpend(): float + /** + * @return float + */ + public function getTimeSpend(): float { - return $this->timeSpend; - } + return $this->timeSpend; + } } diff --git a/Classes/Domain/Result/Timing/ItemCollection.php b/Classes/Domain/Result/Timing/ItemCollection.php index 025662b..6626a0b 100644 --- a/Classes/Domain/Result/Timing/ItemCollection.php +++ b/Classes/Domain/Result/Timing/ItemCollection.php @@ -10,28 +10,27 @@ */ class ItemCollection extends ArrayObject { + /** + * @var float + */ + protected $timeSpend = 0.0; - /** - * @var float - */ - protected $timeSpend = 0.0; - - /** - * @param float $timeSpend - * @return self - */ - public function setTimeSpend(float $timeSpend): ItemCollection + /** + * @param float $timeSpend + * @return self + */ + public function setTimeSpend(float $timeSpend): ItemCollection { - $this->timeSpend = $timeSpend; + $this->timeSpend = $timeSpend; - return $this; - } + return $this; + } - /** - * @return float - */ - public function getTimeSpend(): float + /** + * @return float + */ + public function getTimeSpend(): float { - return $this->timeSpend; - } -} \ No newline at end of file + return $this->timeSpend; + } +} diff --git a/Classes/Domain/Result/Timing/Timing.php b/Classes/Domain/Result/Timing/Timing.php index 203db86..82c9cfd 100644 --- a/Classes/Domain/Result/Timing/Timing.php +++ b/Classes/Domain/Result/Timing/Timing.php @@ -7,77 +7,76 @@ */ class Timing { + /** + * @var ItemCollection + */ + protected $preparationItems; - /** - * @var ItemCollection - */ - protected $preparationItems; + /** + * @var ItemCollection + */ + protected $processingItems; - /** - * @var ItemCollection - */ - protected $processingItems; + /** + * @var float + */ + protected $timeSpend = 0.0; - /** - * @var float - */ - protected $timeSpend = 0.0; - - /** - * @param ItemCollection $preparationItems - * @param ItemCollection $processingItems - */ - public function __construct(ItemCollection $preparationItems, ItemCollection $processingItems) + /** + * @param ItemCollection $preparationItems + * @param ItemCollection $processingItems + */ + public function __construct(ItemCollection $preparationItems, ItemCollection $processingItems) { - $this->preparationItems = $preparationItems; - $this->processingItems = $processingItems; - } + $this->preparationItems = $preparationItems; + $this->processingItems = $processingItems; + } - /** - * @param float $timeSpend - */ - public function setTimeSpend(float $timeSpend) + /** + * @param float $timeSpend + */ + public function setTimeSpend(float $timeSpend) { - $this->timeSpend = $timeSpend; - } + $this->timeSpend = $timeSpend; + } - /** - * @return float - */ - public function getTimeSpend(): float + /** + * @return float + */ + public function getTimeSpend(): float { - return $this->timeSpend; - } + return $this->timeSpend; + } - /** - * @param ItemCollection $processingItems - */ - public function setProcessingItems(ItemCollection $processingItems) + /** + * @param ItemCollection $processingItems + */ + public function setProcessingItems(ItemCollection $processingItems) { - $this->processingItems = $processingItems; - } + $this->processingItems = $processingItems; + } - /** - * @return ItemCollection - */ - public function getProcessingItems(): ItemCollection + /** + * @return ItemCollection + */ + public function getProcessingItems(): ItemCollection { - return $this->processingItems; - } + return $this->processingItems; + } - /** - * @param ItemCollection $preparationItems - */ - public function setPreparationItems(ItemCollection $preparationItems) + /** + * @param ItemCollection $preparationItems + */ + public function setPreparationItems(ItemCollection $preparationItems) { - $this->preparationItems = $preparationItems; - } + $this->preparationItems = $preparationItems; + } - /** - * @return ItemCollection - */ - public function getPreparationItems(): ItemCollection + /** + * @return ItemCollection + */ + public function getPreparationItems(): ItemCollection { - return $this->preparationItems; - } + return $this->preparationItems; + } } diff --git a/Tests/Domain/AbstractSolrTestCase.php b/Tests/Domain/AbstractSolrTestCase.php index 031b635..2b51ad3 100644 --- a/Tests/Domain/AbstractSolrTestCase.php +++ b/Tests/Domain/AbstractSolrTestCase.php @@ -6,28 +6,27 @@ abstract class AbstractSolrTest extends TestCase { - /** - * Helper method to get the path of a "fixtures" folder that is relative to the current class folder. - * - * @return string - */ - protected function getTestFixturePath() + /** + * Helper method to get the path of a "fixtures" folder that is relative to the current class folder. + * + * @return string + */ + protected function getTestFixturePath() { - $reflector = new \ReflectionClass(get_class($this)); - $path = dirname($reflector->getFileName()).'/Fixtures/'; - return $path; - } + $reflector = new \ReflectionClass(static::class); + $path = dirname($reflector->getFileName()) . '/Fixtures/'; + return $path; + } - /** - * Helper method to get a content of a fixture that is located in the "fixtures" folder beside the - * current testcase. - * - * @param $fixtureFilename - * @return string - */ - protected function getFixtureContent($fixtureFilename) + /** + * Helper method to get a content of a fixture that is located in the "fixtures" folder beside the + * current testcase. + * + * @param $fixtureFilename + * @return string + */ + protected function getFixtureContent($fixtureFilename) { - return file_get_contents($this->getTestFixturePath().$fixtureFilename); - } - + return file_get_contents($this->getTestFixturePath() . $fixtureFilename); + } } diff --git a/Tests/Domain/Result/Explanation/AbstractExplanationTestCase.php b/Tests/Domain/Result/Explanation/AbstractExplanationTestCase.php index 55877ff..8c048fc 100644 --- a/Tests/Domain/Result/Explanation/AbstractExplanationTestCase.php +++ b/Tests/Domain/Result/Explanation/AbstractExplanationTestCase.php @@ -10,21 +10,20 @@ abstract class AbstractExplanationTestCase extends AbstractSolrTest { - - /** - * @param $filename - * @return ExplainResult - */ - protected function getExplainFromFixture($filename) + /** + * @param $filename + * @return ExplainResult + */ + protected function getExplainFromFixture($filename) { - $fileContent = $this->getFixtureContent($filename.".txt"); - $content = new Content($fileContent); - $metaData = new MetaData('P_164345','auto'); + $fileContent = $this->getFixtureContent($filename . '.txt'); + $content = new Content($fileContent); + $metaData = new MetaData('P_164345', 'auto'); - $parser = new Parser(); - $parser->injectExplainResult(new ExplainResult()); - $explain = $parser->parse($content,$metaData); + $parser = new Parser(); + $parser->injectExplainResult(new ExplainResult()); + $explain = $parser->parse($content, $metaData); - return $explain; - } + return $explain; + } } diff --git a/Tests/Domain/Result/Explanation/ExplainServiceTestCase.php b/Tests/Domain/Result/Explanation/ExplainServiceTestCase.php index e70da97..5ff1fb6 100644 --- a/Tests/Domain/Result/Explanation/ExplainServiceTestCase.php +++ b/Tests/Domain/Result/Explanation/ExplainServiceTestCase.php @@ -9,21 +9,20 @@ */ class ExplainServiceTestCase extends AbstractExplanationTestCase { - - /** - * @test - */ - public function testFixture1() + /** + * @test + */ + public function testFixture1() { - $content = $this->getFixtureContent('3.0.001.txt'); - $result = ExplainService::getFieldImpactsFromRawContent( - $content, - 'foo', - 'bar' - ); + $content = $this->getFixtureContent('3.0.001.txt'); + $result = ExplainService::getFieldImpactsFromRawContent( + $content, + 'foo', + 'bar' + ); - $this->assertEquals(['name' => 100], $result); - } + self::assertEquals(['name' => 100], $result); + } /** * @test @@ -40,9 +39,9 @@ public function testCanNotCreateEmptyNodes() $expectedResult = [ 'keywords' => 3.9746099859253836, 'title' => 7.1126007378396707, - 'content' => 88.912788999915335 + 'content' => 88.912788999915335, ]; - $this->assertEquals($expectedResult, $result); + self::assertEquals($expectedResult, $result); } /** @@ -64,7 +63,7 @@ public function testCanParseSynonymNodes() 'content' => 81.02878024976832, ]; - $this->assertEquals($expectedResult, $result); + self::assertEquals($expectedResult, $result); } /** @@ -82,9 +81,9 @@ public function testCanParse82Response() $expectedResult = [ 'content' => 85.44380986095436, 'tagsH2H3' => 4.056216176545581, - 'title' => 10.499972051284612 + 'title' => 10.499972051284612, ]; - $this->assertEquals($expectedResult, $result); + self::assertEquals($expectedResult, $result); } } diff --git a/Tests/Domain/Result/Explanation/ExplanationSolr34TestCase.php b/Tests/Domain/Result/Explanation/ExplanationSolr34TestCase.php index 7c7af48..db49478 100644 --- a/Tests/Domain/Result/Explanation/ExplanationSolr34TestCase.php +++ b/Tests/Domain/Result/Explanation/ExplanationSolr34TestCase.php @@ -5,126 +5,126 @@ /** * Testcases for the php port of solr explain for solr version 3.4 responses. */ -class ExplanationSolr34TestCase extends AbstractExplanationTestCase{ - - /** - * @test - */ - public function testFixture001() +class ExplanationSolr34TestCase extends AbstractExplanationTestCase +{ + /** + * @test + */ + public function testFixture001() { - $explain = $this->getExplainFromFixture('3.4.001'); - - $this->assertNotNull($explain); - $this->assertEquals('P_164345',$explain->getDocumentId()); - $this->assertEquals(1.0,$explain->getRootNode()->getScore()); - $this->assertEquals(1,$explain->getChildren()->count()); - $this->assertEquals(0,$explain->getChild(0)->getChildren()->count()); - $this->assertEquals(1.0,$explain->getChild(0)->getScore()); - $this->assertEquals('*:*',$explain->getAttribute(':query')); - } - - /** - * @test - */ - public function testFixture002() + $explain = $this->getExplainFromFixture('3.4.001'); + + self::assertNotNull($explain); + self::assertEquals('P_164345', $explain->getDocumentId()); + self::assertEquals(1.0, $explain->getRootNode()->getScore()); + self::assertEquals(1, $explain->getChildren()->count()); + self::assertEquals(0, $explain->getChild(0)->getChildren()->count()); + self::assertEquals(1.0, $explain->getChild(0)->getScore()); + self::assertEquals('*:*', $explain->getAttribute(':query')); + } + + /** + * @test + */ + public function testFixture002() { - $explain = $this->getExplainFromFixture('3.4.002'); + $explain = $this->getExplainFromFixture('3.4.002'); - $this->assertNotNull($explain); - $this->assertEquals('P_164345',$explain->getDocumentId()); - $this->assertEquals(1.0,$explain->getRootNode()->getScore()); - $this->assertEquals(2,$explain->getChildren()->count()); + self::assertNotNull($explain); + self::assertEquals('P_164345', $explain->getDocumentId()); + self::assertEquals(1.0, $explain->getRootNode()->getScore()); + self::assertEquals(2, $explain->getChildren()->count()); - $this->assertEquals(0,$explain->getChild(0)->getChildren()->count()); - $this->assertEquals(0,$explain->getChild(1)->getChildren()->count()); + self::assertEquals(0, $explain->getChild(0)->getChildren()->count()); + self::assertEquals(0, $explain->getChild(1)->getChildren()->count()); - $this->assertEquals(10.0,$explain->getChild(0)->getScore()); - $this->assertEquals(0.1,$explain->getChild(1)->getScore()); + self::assertEquals(10.0, $explain->getChild(0)->getScore()); + self::assertEquals(0.1, $explain->getChild(1)->getScore()); - $this->assertEquals('*:*^10.0',$explain->getAttribute(':query')); - } + self::assertEquals('*:*^10.0', $explain->getAttribute(':query')); + } - /** - * @test - */ - public function testFixture003() + /** + * @test + */ + public function testFixture003() { - $explain = $this->getExplainFromFixture('3.4.003'); + $explain = $this->getExplainFromFixture('3.4.003'); - $this->assertNotNull($explain); - $this->assertEquals('P_164345', $explain->getDocumentId()); - $this->assertEquals(100.48211, $explain->getRootNode()->getScore()); + self::assertNotNull($explain); + self::assertEquals('P_164345', $explain->getDocumentId()); + self::assertEquals(100.48211, $explain->getRootNode()->getScore()); - $this->assertEquals(2, $explain->getChildren()->count()); - $this->assertEquals(2, $explain->getChild(0)->getChildren()->count()); - $this->assertEquals(3, $explain->getChild(1)->getChildren()->count()); + self::assertEquals(2, $explain->getChildren()->count()); + self::assertEquals(2, $explain->getChild(0)->getChildren()->count()); + self::assertEquals(3, $explain->getChild(1)->getChildren()->count()); - $this->assertEquals(0.99999994, $explain->getChild(0)->getScore()); - $this->assertEquals(100.48212, $explain->getChild(1)->getScore()); - } + self::assertEquals(0.99999994, $explain->getChild(0)->getScore()); + self::assertEquals(100.48212, $explain->getChild(1)->getScore()); + } - public function testFixture004() + public function testFixture004() { - $explain = $this->getExplainFromFixture('3.4.004'); - - $this->assertNotNull($explain); - $this->assertEquals('P_164345', $explain->getDocumentId()); - $this->assertEquals(0.0,$explain->getRootNode()->getScore()); - $this->assertEquals(3,$explain->getChildren()->count()); - $this->assertEquals(0, $explain->getChild(0)->getChildren()->count()); - $this->assertEquals(0, $explain->getChild(1)->getChildren()->count()); - $this->assertEquals(0, $explain->getChild(2)->getChildren()->count()); - - $this->assertEquals(3.8332133, $explain->getChild(1)->getScore()); - $this->assertEquals(32.0, $explain->getChild(2)->getScore()); - } - - /** - * @test - */ - public function testFixture005() + $explain = $this->getExplainFromFixture('3.4.004'); + + self::assertNotNull($explain); + self::assertEquals('P_164345', $explain->getDocumentId()); + self::assertEquals(0.0, $explain->getRootNode()->getScore()); + self::assertEquals(3, $explain->getChildren()->count()); + self::assertEquals(0, $explain->getChild(0)->getChildren()->count()); + self::assertEquals(0, $explain->getChild(1)->getChildren()->count()); + self::assertEquals(0, $explain->getChild(2)->getChildren()->count()); + + self::assertEquals(3.8332133, $explain->getChild(1)->getScore()); + self::assertEquals(32.0, $explain->getChild(2)->getScore()); + } + + /** + * @test + */ + public function testFixture005() { - $explain = $this->getExplainFromFixture('3.4.005'); + $explain = $this->getExplainFromFixture('3.4.005'); - $this->assertNotNull($explain); - $this->assertEquals('P_164345', $explain->getDocumentId()); - $this->assertEquals(142.10316,$explain->getRootNode()->getScore()); - $this->assertEquals(2,$explain->getChildren()->count()); - $this->assertEquals(2,$explain->getChild(0)->getChildren()->count()); - $this->assertEquals(2,$explain->getChild(1)->getChildren()->count()); + self::assertNotNull($explain); + self::assertEquals('P_164345', $explain->getDocumentId()); + self::assertEquals(142.10316, $explain->getRootNode()->getScore()); + self::assertEquals(2, $explain->getChildren()->count()); + self::assertEquals(2, $explain->getChild(0)->getChildren()->count()); + self::assertEquals(2, $explain->getChild(1)->getChildren()->count()); - $this->assertEquals(71.05158,$explain->getChild(0)->getScore()); - $this->assertEquals(71.05158,$explain->getChild(1)->getScore()); - } + self::assertEquals(71.05158, $explain->getChild(0)->getScore()); + self::assertEquals(71.05158, $explain->getChild(1)->getScore()); + } - public function testFixture006() + public function testFixture006() { - $explain = $this->getExplainFromFixture('3.4.006'); + $explain = $this->getExplainFromFixture('3.4.006'); - $this->assertNotNull($explain); - $this->assertEquals('P_164345', $explain->getDocumentId()); - $this->assertEquals(0.0, $explain->getRootNode()->getScore()); - $this->assertEquals(2, $explain->getChildren()->count()); - $this->assertEquals(2, $explain->getChild(0)->getChildren()->count()); - $this->assertEquals(0, $explain->getChild(1)->getChildren()->count()); + self::assertNotNull($explain); + self::assertEquals('P_164345', $explain->getDocumentId()); + self::assertEquals(0.0, $explain->getRootNode()->getScore()); + self::assertEquals(2, $explain->getChildren()->count()); + self::assertEquals(2, $explain->getChild(0)->getChildren()->count()); + self::assertEquals(0, $explain->getChild(1)->getChildren()->count()); - $this->assertEquals(63.675232, $explain->getChild(0)->getScore()); - $this->assertEquals(0.0, $explain->getChild(1)->getScore()); - } + self::assertEquals(63.675232, $explain->getChild(0)->getScore()); + self::assertEquals(0.0, $explain->getChild(1)->getScore()); + } - public function testFixture007() + public function testFixture007() { - $explain = $this->getExplainFromFixture('3.4.007'); - - $this->assertNotNull($explain); - $this->assertEquals('P_164345', $explain->getDocumentId()); - $this->assertEquals(200.96422,$explain->getRootNode()->getScore()); - $this->assertEquals(2,$explain->getChildren()->count()); - $this->assertEquals(2, $explain->getChild(0)->getChildren()->count()); - $this->assertEquals(3, $explain->getChild(1)->getChildren()->count()); - $this->assertEquals(0.99999994,$explain->getChild(0)->getScore()); - $this->assertEquals(200.96423, $explain->getChild(1)->getScore()); - - $this->assertEquals('name:"Dell Widescreen"',$explain->getAttribute(':query')); - } -} \ No newline at end of file + $explain = $this->getExplainFromFixture('3.4.007'); + + self::assertNotNull($explain); + self::assertEquals('P_164345', $explain->getDocumentId()); + self::assertEquals(200.96422, $explain->getRootNode()->getScore()); + self::assertEquals(2, $explain->getChildren()->count()); + self::assertEquals(2, $explain->getChild(0)->getChildren()->count()); + self::assertEquals(3, $explain->getChild(1)->getChildren()->count()); + self::assertEquals(0.99999994, $explain->getChild(0)->getScore()); + self::assertEquals(200.96423, $explain->getChild(1)->getScore()); + + self::assertEquals('name:"Dell Widescreen"', $explain->getAttribute(':query')); + } +} diff --git a/Tests/Domain/Result/Explanation/ExplanationSolr3TestCase.php b/Tests/Domain/Result/Explanation/ExplanationSolr3TestCase.php index a0730be..b241c3b 100644 --- a/Tests/Domain/Result/Explanation/ExplanationSolr3TestCase.php +++ b/Tests/Domain/Result/Explanation/ExplanationSolr3TestCase.php @@ -9,103 +9,102 @@ */ class ExplanationSolr3TestCase extends AbstractExplanationTestCase { - - /** - * @test - */ - public function testFixture001GetScore() + /** + * @test + */ + public function testFixture001GetScore() { - $explain = $this->getExplainFromFixture('3.0.001'); - - $this->assertNotNull($explain); - $this->assertEquals(1, $explain->getChildren()->count()); - $this->assertEquals(2,$explain->getChild(0)->getChildren()->count()); - $this->assertEquals(2,$explain->getChild(0)->getParent()->getChild(0)->getChildren()->count()); - $this->assertEquals(0.8621642, $explain->getRootNode()->getScore()); - $this->assertEquals(0.8621642,$explain->getChild(0)->getScore()); - } - - /** - * @test - */ - public function testFixture001GetImpact() + $explain = $this->getExplainFromFixture('3.0.001'); + + self::assertNotNull($explain); + self::assertEquals(1, $explain->getChildren()->count()); + self::assertEquals(2, $explain->getChild(0)->getChildren()->count()); + self::assertEquals(2, $explain->getChild(0)->getParent()->getChild(0)->getChildren()->count()); + self::assertEquals(0.8621642, $explain->getRootNode()->getScore()); + self::assertEquals(0.8621642, $explain->getChild(0)->getScore()); + } + + /** + * @test + */ + public function testFixture001GetImpact() { - $explain = $this->getExplainFromFixture('3.0.001'); + $explain = $this->getExplainFromFixture('3.0.001'); - $this->assertNotNull($explain); - $this->assertEquals(0.8621642, $explain->getRootNode()->getScore()); - $this->assertEquals(0.8621642,$explain->getChild(0)->getScore()); - $this->assertEquals(0.4310821,$explain->getChild(0)->getChild(0)->getScore()); + self::assertNotNull($explain); + self::assertEquals(0.8621642, $explain->getRootNode()->getScore()); + self::assertEquals(0.8621642, $explain->getChild(0)->getScore()); + self::assertEquals(0.4310821, $explain->getChild(0)->getChild(0)->getScore()); - $this->assertEquals(100.0,$explain->getRootNode()->getAbsoluteImpactPercentage()); + self::assertEquals(100.0, $explain->getRootNode()->getAbsoluteImpactPercentage()); - $this->assertEquals(Explain::NODE_TYPE_SUM,$explain->getRootNode()->getNodeType()); - $this->assertEquals(Explain::NODE_TYPE_SUM,$explain->getRootNode()->getChild(0)->getNodeType()); + self::assertEquals(Explain::NODE_TYPE_SUM, $explain->getRootNode()->getNodeType()); + self::assertEquals(Explain::NODE_TYPE_SUM, $explain->getRootNode()->getChild(0)->getNodeType()); - $this->assertEquals(100.0,$explain->getRootNode()->getChild(0)->getAbsoluteImpactPercentage()); + self::assertEquals(100.0, $explain->getRootNode()->getChild(0)->getAbsoluteImpactPercentage()); - //the sum nodes - $this->assertEquals(50.0,$explain->getRootNode()->getChild(0)->getChild(0)->getAbsoluteImpactPercentage()); - $this->assertEquals(50.0,$explain->getRootNode()->getChild(0)->getChild(1)->getAbsoluteImpactPercentage()); + //the sum nodes + self::assertEquals(50.0, $explain->getRootNode()->getChild(0)->getChild(0)->getAbsoluteImpactPercentage()); + self::assertEquals(50.0, $explain->getRootNode()->getChild(0)->getChild(1)->getAbsoluteImpactPercentage()); - //the max node - $this->assertEquals(50.0,$explain->getRootNode()->getChild(0)->getChild(0)->getChild(0)->getAbsoluteImpactPercentage()); + //the max node + self::assertEquals(50.0, $explain->getRootNode()->getChild(0)->getChild(0)->getChild(0)->getAbsoluteImpactPercentage()); - // 0.5044475 + 0.8545628 = 1,3590103 - // 100 / 1,3590103 = 73,582959599 - // 0.5044475 * 73,582959599 = 37,118740012 / 2 => 18,559370006 - // 0.8545628 * 73,582959599 = 62,881259988 / 2 => 31,440629994 + // 0.5044475 + 0.8545628 = 1,3590103 + // 100 / 1,3590103 = 73,582959599 + // 0.5044475 * 73,582959599 = 37,118740012 / 2 => 18,559370006 + // 0.8545628 * 73,582959599 = 62,881259988 / 2 => 31,440629994 - $this->assertEquals(18.55937000624646,$explain->getRootNode()->getChild(0)->getChild(0)->getChild(0)->getChild(0)->getAbsoluteImpactPercentage()); - $this->assertEquals(31.440629993753543,$explain->getRootNode()->getChild(0)->getChild(0)->getChild(0)->getChild(1)->getAbsoluteImpactPercentage()); - $this->assertEquals(Explain::NODE_TYPE_MAX,$explain->getRootNode()->getChild(0)->getChild(0)->getNodeType()); - } + self::assertEquals(18.55937000624646, $explain->getRootNode()->getChild(0)->getChild(0)->getChild(0)->getChild(0)->getAbsoluteImpactPercentage()); + self::assertEquals(31.440629993753543, $explain->getRootNode()->getChild(0)->getChild(0)->getChild(0)->getChild(1)->getAbsoluteImpactPercentage()); + self::assertEquals(Explain::NODE_TYPE_MAX, $explain->getRootNode()->getChild(0)->getChild(0)->getNodeType()); + } - /** - * @test - */ - public function testFixture002() + /** + * @test + */ + public function testFixture002() { - $explain = $this->getExplainFromFixture('3.0.002'); + $explain = $this->getExplainFromFixture('3.0.002'); - $this->assertNotNull($explain); - $this->assertEquals(1.6506158, $explain->getRootNode()->getScore()); - $this->assertEquals(2, $explain->getChildren()->count()); - } + self::assertNotNull($explain); + self::assertEquals(1.6506158, $explain->getRootNode()->getScore()); + self::assertEquals(2, $explain->getChildren()->count()); + } - /** - * @test - */ - public function testFixture003() + /** + * @test + */ + public function testFixture003() { - $explain = $this->getExplainFromFixture('3.0.003'); + $explain = $this->getExplainFromFixture('3.0.003'); - $this->assertNotNull($explain); - $this->assertEquals(36.50278,$explain->getRootNode()->getScore()); - $this->assertEquals(2, $explain->getChildren()->count()); - } + self::assertNotNull($explain); + self::assertEquals(36.50278, $explain->getRootNode()->getScore()); + self::assertEquals(2, $explain->getChildren()->count()); + } - /** - * @test - */ - public function testFixture004() + /** + * @test + */ + public function testFixture004() { - $explain = $this->getExplainFromFixture('3.0.004'); + $explain = $this->getExplainFromFixture('3.0.004'); - $this->assertNotNull($explain); - $this->assertEquals(0.524427,$explain->getRootNode()->getScore()); - $this->assertEquals(2, $explain->getChildren()->count()); - } + self::assertNotNull($explain); + self::assertEquals(0.524427, $explain->getRootNode()->getScore()); + self::assertEquals(2, $explain->getChildren()->count()); + } - /** - * @test - */ - public function testFixture005() + /** + * @test + */ + public function testFixture005() { - $explain = $this->getExplainFromFixture('3.0.005'); + $explain = $this->getExplainFromFixture('3.0.005'); - $this->assertNotNull($explain); - $this->assertEquals(5.8746934,$explain->getRootNode()->getScore()); - $this->assertEquals(2, $explain->getChildren()->count()); - } + self::assertNotNull($explain); + self::assertEquals(5.8746934, $explain->getRootNode()->getScore()); + self::assertEquals(2, $explain->getChildren()->count()); + } } diff --git a/Tests/Domain/Result/Explanation/ExplanationSolr40TestCase.php b/Tests/Domain/Result/Explanation/ExplanationSolr40TestCase.php index b42e0d0..02beb29 100644 --- a/Tests/Domain/Result/Explanation/ExplanationSolr40TestCase.php +++ b/Tests/Domain/Result/Explanation/ExplanationSolr40TestCase.php @@ -7,25 +7,24 @@ */ class ExplanationSolr40TestCase extends AbstractExplanationTestCase { - - /** - * @test - */ - public function testFixture001() + /** + * @test + */ + public function testFixture001() { - $explain = $this->getExplainFromFixture('4.0.001'); + $explain = $this->getExplainFromFixture('4.0.001'); - $this->assertNotNull($explain); - $this->assertEquals(0.6495038, $explain->getRootNode()->getScore()); - $this->assertEquals(1, $explain->getChildren()->count()); - $this->assertEquals(0.6495038, $explain->getChild(0)->getScore()); - $this->assertEquals(3, $explain->getChild(0)->getChildren()->count()); - $this->assertEquals(1, $explain->getChild(0)->getChild(0)->getChildren()->count()); - $this->assertEquals(0, $explain->getChild(0)->getChild(1)->getChildren()->count()); - $this->assertEquals(0, $explain->getChild(0)->getChild(2)->getChildren()->count()); + self::assertNotNull($explain); + self::assertEquals(0.6495038, $explain->getRootNode()->getScore()); + self::assertEquals(1, $explain->getChildren()->count()); + self::assertEquals(0.6495038, $explain->getChild(0)->getScore()); + self::assertEquals(3, $explain->getChild(0)->getChildren()->count()); + self::assertEquals(1, $explain->getChild(0)->getChild(0)->getChildren()->count()); + self::assertEquals(0, $explain->getChild(0)->getChild(1)->getChildren()->count()); + self::assertEquals(0, $explain->getChild(0)->getChild(2)->getChildren()->count()); - //TODO: - // $this->assertEquals(4, $explain->getMetaData()->getVersion()); - // assert_equal('P_164345', explain.metadata[:id]) - } -} \ No newline at end of file + //TODO: + // $this->assertEquals(4, $explain->getMetaData()->getVersion()); + // assert_equal('P_164345', explain.metadata[:id]) + } +} diff --git a/Tests/Domain/Result/Explanation/Nodes/MaxTestCase.php b/Tests/Domain/Result/Explanation/Nodes/MaxTestCase.php index d693949..34557e9 100644 --- a/Tests/Domain/Result/Explanation/Nodes/MaxTestCase.php +++ b/Tests/Domain/Result/Explanation/Nodes/MaxTestCase.php @@ -6,13 +6,10 @@ class MaxTestCase extends AbstractExplanationTestCase { - - /** - */ - public function testGetTieBreaker() + public function testGetTieBreaker() { - $maxNode = new \ApacheSolrForTypo3\SolrExplain\Domain\Result\Explanation\Nodes\Max(); - $maxNode->setContent('8.040816E-4 = (MATCH) max plus 0.7 times others of:'); - $this->assertEquals(0.7, $maxNode->getTieBreaker()); - } -} \ No newline at end of file + $maxNode = new \ApacheSolrForTypo3\SolrExplain\Domain\Result\Explanation\Nodes\Max(); + $maxNode->setContent('8.040816E-4 = (MATCH) max plus 0.7 times others of:'); + self::assertEquals(0.7, $maxNode->getTieBreaker()); + } +} diff --git a/Tests/Domain/Result/Explanation/Visitors/SummarizeFieldImpactsTestCase.php b/Tests/Domain/Result/Explanation/Visitors/SummarizeFieldImpactsTestCase.php index c43a3f4..e324ccc 100644 --- a/Tests/Domain/Result/Explanation/Visitors/SummarizeFieldImpactsTestCase.php +++ b/Tests/Domain/Result/Explanation/Visitors/SummarizeFieldImpactsTestCase.php @@ -11,105 +11,104 @@ class SummarizeFieldImpactsTestCase extends AbstractExplanationTestCase { + /** + * @return ExplainResult + */ + protected function getExplain($filename) + { + $fileContent = $this->getFixtureContent($filename . '.txt'); + $content = new Content($fileContent); + $metaData = new MetaData('P_164345', 'auto'); + $parser = new Parser(); + + $parser->injectExplainResult(new ExplainResult()); + $explain = $parser->parse($content, $metaData); + + return $explain; + } - /** - * @return ExplainResult - */ - protected function getExplain($filename) { - $fileContent = $this->getFixtureContent($filename.".txt"); - $content = new Content($fileContent); - $metaData = new MetaData('P_164345','auto'); - $parser = new Parser(); - - $parser->injectExplainResult(new ExplainResult()); - $explain = $parser->parse($content,$metaData); - - return $explain; - } - - /** - * @test - */ - public function testCanSummarizeFieldImpactFixture001() + /** + * @test + */ + public function testCanSummarizeFieldImpactFixture001() { - $explain = $this->getExplain('3.0.001'); - $visitor = new SummarizeFieldImpacts(); - $explain->getRootNode()->visitNodes($visitor); - - $this->assertEquals(100.0,$visitor->getFieldImpact('name')); - $this->assertEquals(['name'],$visitor->getRelevantFieldNames()); - } - - /** - * @test - */ - public function testCanSummarizeFieldImpactFixture003() + $explain = $this->getExplain('3.0.001'); + $visitor = new SummarizeFieldImpacts(); + $explain->getRootNode()->visitNodes($visitor); + + self::assertEquals(100.0, $visitor->getFieldImpact('name')); + self::assertEquals(['name'], $visitor->getRelevantFieldNames()); + } + + /** + * @test + */ + public function testCanSummarizeFieldImpactFixture003() { - $explain = $this->getExplain('3.0.003'); - $visitor = new SummarizeFieldImpacts(); - $explain->getRootNode()->visitNodes($visitor); - - $this->assertEquals(95.75659716876356,$visitor->getFieldImpact('price')); - $this->assertEquals(['name','manu','price'],$visitor->getRelevantFieldNames()); - } - - /** - * @test - */ - public function testCanSummarizeFieldImpactFixture004() + $explain = $this->getExplain('3.0.003'); + $visitor = new SummarizeFieldImpacts(); + $explain->getRootNode()->visitNodes($visitor); + + self::assertEquals(95.75659716876356, $visitor->getFieldImpact('price')); + self::assertEquals(['name', 'manu', 'price'], $visitor->getRelevantFieldNames()); + } + + /** + * @test + */ + public function testCanSummarizeFieldImpactFixture004() { - $explain = $this->getExplain('3.0.004'); - $visitor = new SummarizeFieldImpacts(); - $explain->getRootNode()->visitNodes($visitor); - - $this->assertEquals(100.0,$visitor->getFieldImpact('name')); - $this->assertEquals(['name','price'],$visitor->getRelevantFieldNames()); - } - - /** - * @test - */ - public function testCanSummarizeCustomTieBreakerFixture() + $explain = $this->getExplain('3.0.004'); + $visitor = new SummarizeFieldImpacts(); + $explain->getRootNode()->visitNodes($visitor); + + self::assertEquals(100.0, $visitor->getFieldImpact('name')); + self::assertEquals(['name', 'price'], $visitor->getRelevantFieldNames()); + } + + /** + * @test + */ + public function testCanSummarizeCustomTieBreakerFixture() { - $explain = $this->getExplain('custom.tieBreaker'); - $visitor = new SummarizeFieldImpacts(); - $explain->getRootNode()->visitNodes($visitor); - - $this->assertEquals(['expandedcontent','content','doctype'],$visitor->getRelevantFieldNames()); - $this->assertEquals(47.9,round($visitor->getFieldImpact('doctype'),1)); - $this->assertEquals(47.9,round($visitor->getFieldImpact('expandedcontent'),1)); - $this->assertEquals(4.2,round($visitor->getFieldImpact('content'),1)); - } - - /** - * @test - */ - public function testCanSummarizeCustomTieBreaker2Fixture() + $explain = $this->getExplain('custom.tieBreaker'); + $visitor = new SummarizeFieldImpacts(); + $explain->getRootNode()->visitNodes($visitor); + + self::assertEquals(['expandedcontent', 'content', 'doctype'], $visitor->getRelevantFieldNames()); + self::assertEquals(47.9, round($visitor->getFieldImpact('doctype'), 1)); + self::assertEquals(47.9, round($visitor->getFieldImpact('expandedcontent'), 1)); + self::assertEquals(4.2, round($visitor->getFieldImpact('content'), 1)); + } + + /** + * @test + */ + public function testCanSummarizeCustomTieBreaker2Fixture() { - $explain = $this->getExplain('custom.tieBreaker2'); - $visitor = new SummarizeFieldImpacts(); - $explain->getRootNode()->visitNodes($visitor); - - $this->assertEquals(['pr_title','doctype'],$visitor->getRelevantFieldNames()); - $this->assertEquals(0.07,round($visitor->getFieldImpact('doctype'),2)); - $this->assertEquals(99.93,round($visitor->getFieldImpact('pr_title'),2)); - } - - - /** - * @test - */ - public function testCanSummarizeCustomTieBreaker3Fixture() + $explain = $this->getExplain('custom.tieBreaker2'); + $visitor = new SummarizeFieldImpacts(); + $explain->getRootNode()->visitNodes($visitor); + + self::assertEquals(['pr_title', 'doctype'], $visitor->getRelevantFieldNames()); + self::assertEquals(0.07, round($visitor->getFieldImpact('doctype'), 2)); + self::assertEquals(99.93, round($visitor->getFieldImpact('pr_title'), 2)); + } + + /** + * @test + */ + public function testCanSummarizeCustomTieBreaker3Fixture() { - $explain = $this->getExplain('custom.tieBreaker3'); - $visitor = new SummarizeFieldImpacts(); - $explain->getRootNode()->visitNodes($visitor); - - $this->assertEquals(['keywords','expandedcontent','content','description','doctype'],$visitor->getRelevantFieldNames()); - $this->assertEquals(0.00,round($visitor->getFieldImpact('keywords'),2)); - $this->assertEquals(7.55,round($visitor->getFieldImpact('expandedcontent'),2)); - $this->assertEquals(4.72,round($visitor->getFieldImpact('content'),2)); - $this->assertEquals(85.64,round($visitor->getFieldImpact('description'),2)); - $this->assertEquals(2.09, round($visitor->getFieldImpact('doctype'),2)); - } + $explain = $this->getExplain('custom.tieBreaker3'); + $visitor = new SummarizeFieldImpacts(); + $explain->getRootNode()->visitNodes($visitor); + + self::assertEquals(['keywords', 'expandedcontent', 'content', 'description', 'doctype'], $visitor->getRelevantFieldNames()); + self::assertEquals(0.00, round($visitor->getFieldImpact('keywords'), 2)); + self::assertEquals(7.55, round($visitor->getFieldImpact('expandedcontent'), 2)); + self::assertEquals(4.72, round($visitor->getFieldImpact('content'), 2)); + self::assertEquals(85.64, round($visitor->getFieldImpact('description'), 2)); + self::assertEquals(2.09, round($visitor->getFieldImpact('doctype'), 2)); + } } diff --git a/Tests/Domain/Result/Explanation/Visitors/SummarizeLeafImpactsTestCase.php b/Tests/Domain/Result/Explanation/Visitors/SummarizeLeafImpactsTestCase.php index 1507702..a6ceb42 100644 --- a/Tests/Domain/Result/Explanation/Visitors/SummarizeLeafImpactsTestCase.php +++ b/Tests/Domain/Result/Explanation/Visitors/SummarizeLeafImpactsTestCase.php @@ -7,87 +7,84 @@ use ApacheSolrForTypo3\SolrExplain\Domain\Result\Explanation\MetaData; use ApacheSolrForTypo3\SolrExplain\Domain\Result\Explanation\Parser; use ApacheSolrForTypo3\SolrExplain\Domain\Result\Explanation\Visitors\SummarizeLeafImpacts; -use ApacheSolrForTypo3\SolrExplain\Tests\Domain\Result\Explanation\AbstractExplanationTestCase; class SummarizeLeafImpactsTestCase extends AbstractExplanationTestCase { - - /** - * @return ExplainResult - */ - protected function getExplain($filename) + /** + * @return ExplainResult + */ + protected function getExplain($filename) { - $fileContent = $this->getFixtureContent($filename.".txt"); - $content = new Content($fileContent); - $metaData = new MetaData('P_164345','auto'); - $parser = new Parser(); + $fileContent = $this->getFixtureContent($filename . '.txt'); + $content = new Content($fileContent); + $metaData = new MetaData('P_164345', 'auto'); + $parser = new Parser(); - $parser->injectExplainResult(new ExplainResult()); - $explain = $parser->parse($content,$metaData); + $parser->injectExplainResult(new ExplainResult()); + $explain = $parser->parse($content, $metaData); - return $explain; - } + return $explain; + } - /** - * @return array - */ - public function leafSumFixtureNameDataProvider() + /** + * @return array + */ + public function leafSumFixtureNameDataProvider() { - return [ - ['3.0.001'], - ['3.0.002'], - ['3.0.003'], - ['3.0.004'], - ['3.0.005'], - ['3.4.001'], - ['3.4.002'], - ['3.4.003'], - ['3.4.004'], - ['3.4.005'], - ['3.4.006'], - ['3.4.007'], - ['3.4.008'], - ['3.4.009'], - ['3.4.010'], - ['3.4.011'], - ['3.4.012'], - ['3.4.013'], - ['3.4.014'], - ['3.4.015'], - ['3.4.016'], - ['3.4.017'], - //contains invalid content therefore no overall impact of 100 expected - ['3.4.018',0.0], - ['3.4.019'], - ['3.4.020'], - ['3.4.021'], - ['3.4.022'], - ['3.4.023'], - ['3.4.024'], - ['3.4.025'], - ['3.4.026'], - ['3.4.027'], - ['3.4.028'], - ['4.0.001'], - ['complex'], - ['custom.tieBreaker'] + return [ + ['3.0.001'], + ['3.0.002'], + ['3.0.003'], + ['3.0.004'], + ['3.0.005'], + ['3.4.001'], + ['3.4.002'], + ['3.4.003'], + ['3.4.004'], + ['3.4.005'], + ['3.4.006'], + ['3.4.007'], + ['3.4.008'], + ['3.4.009'], + ['3.4.010'], + ['3.4.011'], + ['3.4.012'], + ['3.4.013'], + ['3.4.014'], + ['3.4.015'], + ['3.4.016'], + ['3.4.017'], + //contains invalid content therefore no overall impact of 100 expected + ['3.4.018', 0.0], + ['3.4.019'], + ['3.4.020'], + ['3.4.021'], + ['3.4.022'], + ['3.4.023'], + ['3.4.024'], + ['3.4.025'], + ['3.4.026'], + ['3.4.027'], + ['3.4.028'], + ['4.0.001'], + ['complex'], + ['custom.tieBreaker'], ]; - } + } - /** - * The sum for all leaf impacts in percetage should be 100%. - * - * This testcase is used to test this for some fixture files. - * - * @test - * @dataProvider leafSumFixtureNameDataProvider - */ - public function verifyFixtureLeafImpactSum($fixtureName, $expectedImpactSum = 100) + /** + * The sum for all leaf impacts in percetage should be 100%. + * + * This testcase is used to test this for some fixture files. + * + * @test + * @dataProvider leafSumFixtureNameDataProvider + */ + public function verifyFixtureLeafImpactSum($fixtureName, $expectedImpactSum = 100) { - $explain = $this->getExplain($fixtureName); - $visitor = new SummarizeLeafImpacts(); - $explain->getRootNode()->visitNodes($visitor); - $this->assertEquals($expectedImpactSum, $visitor->getSum()); - } - -} \ No newline at end of file + $explain = $this->getExplain($fixtureName); + $visitor = new SummarizeLeafImpacts(); + $explain->getRootNode()->visitNodes($visitor); + self::assertEquals($expectedImpactSum, $visitor->getSum()); + } +} diff --git a/Tests/Domain/Result/ParserTestCase.php b/Tests/Domain/Result/ParserTestCase.php index aab3daf..2c43b5b 100644 --- a/Tests/Domain/Result/ParserTestCase.php +++ b/Tests/Domain/Result/ParserTestCase.php @@ -1,4 +1,5 @@ getFixtureContent("3.4.001.xml"); - $parser = new Parser(); - $result = $parser->parse($content); + $content = $this->getFixtureContent('3.4.001.xml'); + $parser = new Parser(); + $result = $parser->parse($content); - $this->assertEquals(17, $result->getCompleteResultCount()); - $this->assertEquals(9, $result->getQueryTime()); - $this->assertEquals(10, $result->getResultCount()); + self::assertEquals(17, $result->getCompleteResultCount()); + self::assertEquals(9, $result->getQueryTime()); + self::assertEquals(10, $result->getResultCount()); - $this->assertEquals("GB18030TEST", - $result->getDocumentCollection()->getDocument(0)->getFieldByName('id')->getValue() - ); + self::assertEquals( + 'GB18030TEST', + $result->getDocumentCollection()->getDocument(0)->getFieldByName('id')->getValue() + ); - $this->assertEquals( - ['electronics','hard drive'], - $result->getDocumentCollection()->getDocument(1)->getFieldByName('cat')->getValue() - ); + self::assertEquals( + ['electronics', 'hard drive'], + $result->getDocumentCollection()->getDocument(1)->getFieldByName('cat')->getValue() + ); - $expectedExplain = PHP_EOL.'1.0 = (MATCH) MatchAllDocsQuery, product of:'.PHP_EOL.' 1.0 = queryNorm'.PHP_EOL; - $actualExplain = $result->getDocumentCollection()->getDocument(9)->getRawExplainData(); - $this->assertEquals($expectedExplain, $actualExplain); - } + $expectedExplain = PHP_EOL . '1.0 = (MATCH) MatchAllDocsQuery, product of:' . PHP_EOL . ' 1.0 = queryNorm' . PHP_EOL; + $actualExplain = $result->getDocumentCollection()->getDocument(9)->getRawExplainData(); + self::assertEquals($expectedExplain, $actualExplain); + } - /** - * @test - */ - public function testFixture004() + /** + * @test + */ + public function testFixture004() { - $content = $this->getFixtureContent("3.4.004.xml"); - $parser = new Parser(); - $result = $parser->parse($content); + $content = $this->getFixtureContent('3.4.004.xml'); + $parser = new Parser(); + $result = $parser->parse($content); - $this->assertEquals(2, $result->getQueryTime()); - $this->assertEquals(10, $result->getResultCount()); + self::assertEquals(2, $result->getQueryTime()); + self::assertEquals(10, $result->getResultCount()); - $expectedExplain4 = PHP_EOL."4.0 = (MATCH) MatchAllDocsQuery, product of:".PHP_EOL." 4.0 = queryNorm".PHP_EOL; - $actualExplain = $result->getDocumentCollection()->getDocument(3)->getRawExplainData(); + $expectedExplain4 = PHP_EOL . '4.0 = (MATCH) MatchAllDocsQuery, product of:' . PHP_EOL . ' 4.0 = queryNorm' . PHP_EOL; + $actualExplain = $result->getDocumentCollection()->getDocument(3)->getRawExplainData(); - $this->assertEquals($expectedExplain4, $actualExplain); - $this->assertEquals(2.0, $result->getTiming()->getTimeSpend()); - $this->assertEquals(6, $result->getTiming()->getProcessingItems()->count()); - } + self::assertEquals($expectedExplain4, $actualExplain); + self::assertEquals(2.0, $result->getTiming()->getTimeSpend()); + self::assertEquals(6, $result->getTiming()->getProcessingItems()->count()); + } - /** - * @test - */ - public function testFixtureSolr4010() + /** + * @test + */ + public function testFixtureSolr4010() { - $content = $this->getFixtureContent("4.0.010.xml"); - $parser = new Parser(); - $result = $parser->parse($content); + $content = $this->getFixtureContent('4.0.010.xml'); + $parser = new Parser(); + $result = $parser->parse($content); - $this->assertEquals(3, $result->getResultCount()); - $this->assertEquals(4, $result->getTiming()->getTimeSpend()); - $this->assertEquals("LuceneQParser",$result->getQueryParser()); - } -} \ No newline at end of file + self::assertEquals(3, $result->getResultCount()); + self::assertEquals(4, $result->getTiming()->getTimeSpend()); + self::assertEquals('LuceneQParser', $result->getQueryParser()); + } +} diff --git a/Tests/Domain/Result/Timing/ItemTestCase.php b/Tests/Domain/Result/Timing/ItemTestCase.php index 39266e1..a9676bb 100644 --- a/Tests/Domain/Result/Timing/ItemTestCase.php +++ b/Tests/Domain/Result/Timing/ItemTestCase.php @@ -5,27 +5,23 @@ class ItemTestCase extends AbstractSolrTest { + /** + * @var Item + */ + protected $timingItem; - /** - * @var Item - */ - protected $timingItem; - - /** - * @return void - */ protected function setUp(): void { - $this->timingItem = new Item(); - } + $this->timingItem = new Item(); + } - /** - * @test - */ - public function testSetTimeSpend() + /** + * @test + */ + public function testSetTimeSpend() { - $this->assertEquals(0.0, $this->timingItem->getTimeSpend()); - $this->timingItem->setTimeSpend(1.0); - $this->assertEquals(1.0, $this->timingItem->getTimeSpend()); - } -} \ No newline at end of file + self::assertEquals(0.0, $this->timingItem->getTimeSpend()); + $this->timingItem->setTimeSpend(1.0); + self::assertEquals(1.0, $this->timingItem->getTimeSpend()); + } +}