From 66c06a1fbc0ebd1f817c835d9124326fdfb42375 Mon Sep 17 00:00:00 2001 From: Matthias Steffens Date: Thu, 23 Nov 2023 20:54:28 +0100 Subject: [PATCH] #37 Reuse test documents between weighted search tests --- test/Solr/Solarium/AdapterSearchingTest.php | 49 ++++++++++++--------- test/TestAsset/DocumentBasedTestCase.php | 16 ++++--- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/test/Solr/Solarium/AdapterSearchingTest.php b/test/Solr/Solarium/AdapterSearchingTest.php index d12d38c..4e4af53 100644 --- a/test/Solr/Solarium/AdapterSearchingTest.php +++ b/test/Solr/Solarium/AdapterSearchingTest.php @@ -33,7 +33,6 @@ namespace OpusTest\Search\Solr\Solarium; use Exception; -use Opus\Common\Document; use Opus\Common\Person; use Opus\Search\Query; use Opus\Search\QueryFactory; @@ -48,6 +47,30 @@ class AdapterSearchingTest extends DocumentBasedTestCase { + /** @var array[] */ + protected static $additionalDocumentPropertySets = [ + 'weightedTestDocA' => [ + 'TitleMain' => [ + 'Value' => 'Some Document', + 'Language' => 'eng', + ], + 'TitleAbstract' => [ + 'Value' => 'Abstract of test document A.\nSome more text.', + 'Language' => 'eng', + ], + ], + 'weightedTestDocB' => [ + 'TitleMain' => [ + 'Value' => 'Another Test Document', + 'Language' => 'eng', + ], + 'TitleAbstract' => [ + 'Value' => 'Abstract of document B.\nSome blah blah text.', + 'Language' => 'eng', + ], + ], + ]; + public function testService() { $search = Service::selectSearchingService(null, 'solr'); @@ -253,15 +276,8 @@ public function testMapYearFacetIndexFieldsToYearAsset() public function testWeightedSearch() { - $docA = Document::new(); - $docA->addTitleMain()->setLanguage("eng")->setValue("Some Document"); - $docA->addTitleAbstract()->setLanguage("eng")->setValue("Abstract of test document A.\nSome more text."); - $docA->store(); - - $docB = Document::new(); - $docB->addTitleMain()->setLanguage("eng")->setValue("Another Test Document"); - $docB->addTitleAbstract()->setLanguage("eng")->setValue("Abstract of document B.\nSome blah blah text."); - $docB->store(); + $docA = $this->createDocument('weightedTestDocA'); // 'Some Document' + $docB = $this->createDocument('weightedTestDocB'); // 'Another Test Document' $index = Service::selectIndexingService(null, 'solr'); $index->addDocumentsToIndex([$docA, $docB]); @@ -310,15 +326,8 @@ public function testWeightedSearch() public function testWeightedSearchWithEqualWeights() { - $docA = Document::new(); - $docA->addTitleMain()->setLanguage("eng")->setValue("Some Document"); - $docA->addTitleAbstract()->setLanguage("eng")->setValue("Abstract of test document A.\nSome more text."); - $docA->store(); - - $docB = Document::new(); - $docB->addTitleMain()->setLanguage("eng")->setValue("Another Test Document"); - $docB->addTitleAbstract()->setLanguage("eng")->setValue("Abstract of document B.\nSome blah blah text."); - $docB->store(); + $docA = $this->createDocument('weightedTestDocA'); + $docB = $this->createDocument('weightedTestDocB'); $index = Service::selectIndexingService(null, 'solr'); $index->addDocumentsToIndex([$docA, $docB]); @@ -342,7 +351,7 @@ public function testWeightedSearchWithEqualWeights() $this->assertTrue(abs($matches[0]->getScore() - $matches[1]->getScore()) < 1.0); - // 2. with equal boost factors assigned to fields, expect roughly equal scores + // 2. with equal boost factors, expect roughly equal scores $query->setWeightedFields(['abstract' => 1.0, 'title' => 1.0]); $result = $search->customSearch($query); diff --git a/test/TestAsset/DocumentBasedTestCase.php b/test/TestAsset/DocumentBasedTestCase.php index 561a48e..54bfcaf 100644 --- a/test/TestAsset/DocumentBasedTestCase.php +++ b/test/TestAsset/DocumentBasedTestCase.php @@ -44,6 +44,7 @@ use ReflectionClass; use function array_key_exists; +use function array_merge; use function array_values; use function basename; use function file_get_contents; @@ -155,12 +156,15 @@ class DocumentBasedTestCase extends TestCase ], ]; + /** @var array[] */ + protected static $additionalDocumentPropertySets; + /** * @return array */ public static function documentPropertiesProvider() { - return self::$documentPropertySets; + return array_merge(static::$documentPropertySets, static::$additionalDocumentPropertySets ?? []); } /** @@ -169,11 +173,13 @@ public static function documentPropertiesProvider() */ public static function getDocumentDescriptionByName($name) { - if (! array_key_exists($name, self::$documentPropertySets)) { + $documentPropertySets = self::documentPropertiesProvider(); + + if (! array_key_exists($name, $documentPropertySets)) { throw new InvalidArgumentException("unknown document description"); } - return self::$documentPropertySets[$name]; + return $documentPropertySets[$name]; } /** @@ -187,9 +193,9 @@ public static function getDocumentDescriptionByName($name) protected function createDocument($documentProperties = null) { if ($documentProperties === null) { - $documentProperties = self::$documentPropertySets['article']; + $documentProperties = self::getDocumentDescriptionByName('article'); } if (is_string($documentProperties)) { - $documentProperties = self::$documentPropertySets[$documentProperties]; + $documentProperties = self::getDocumentDescriptionByName($documentProperties); } $document = Document::new();