From 90c9dcb19127e5bbc71a31144ef445dea3fd04ec Mon Sep 17 00:00:00 2001 From: Matthew Hailwood Date: Tue, 19 Mar 2019 13:19:56 +1300 Subject: [PATCH] Add test for aggregator transforms --- tests/Features/AggregatorTest.php | 22 +++++++++++++++++++ ...ThreadAggregatorUsingOnlyDateTransform.php | 12 ++++++++++ ...dWithSearchableArrayUsingDateTransform.php | 19 ++++++++++++++++ tests/Features/TransformersTest.php | 19 ++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 tests/Features/Fixtures/ThreadAggregatorUsingOnlyDateTransform.php create mode 100644 tests/Features/Fixtures/ThreadWithSearchableArrayUsingDateTransform.php diff --git a/tests/Features/AggregatorTest.php b/tests/Features/AggregatorTest.php index c52011f8..4a14a84c 100644 --- a/tests/Features/AggregatorTest.php +++ b/tests/Features/AggregatorTest.php @@ -12,6 +12,8 @@ use App\Thread; use Tests\TestCase; use Algolia\ScoutExtended\Searchable\AggregatorCollection; +use Tests\Features\Fixtures\ThreadAggregatorUsingOnlyDateTransform; +use Tests\Features\Fixtures\ThreadWithSearchableArrayUsingDateTransform; final class AggregatorTest extends TestCase { @@ -219,4 +221,24 @@ public function testRelationLoad(): void $this->assertFalse(Wall::search()->get()->first()->relationLoaded('threads')); $this->assertTrue(News::search()->get()->first()->relationLoaded('threads')); } + + public function testModelTransformsAreRespectedOnAggregators(): void + { + ThreadAggregatorUsingOnlyDateTransform::bootSearchable(); + + $threadsIndexMock = $this->mockIndex('threads'); + $threadsAggregator = $this->mockIndex((new ThreadAggregatorUsingOnlyDateTransform)->searchableAs()); + + $threadsIndexMock->shouldReceive('saveObjects')->once()->with(Mockery::on(function ($argument) { + // Ensure the subscriber count on the single thread index has not been transformed + return $argument[0]['subscriber_count'] === '100'; + })); + + $threadsAggregator->shouldReceive('saveObjects')->once()->with(Mockery::on(function ($argument) { + // Ensure the subscriber count on the aggregator index has not been transformed + return $argument[0]['subscriber_count'] === '100'; + })); + + ThreadWithSearchableArrayUsingDateTransform::create(factory(Thread::class)->raw()); + } } diff --git a/tests/Features/Fixtures/ThreadAggregatorUsingOnlyDateTransform.php b/tests/Features/Fixtures/ThreadAggregatorUsingOnlyDateTransform.php new file mode 100644 index 00000000..493d9f82 --- /dev/null +++ b/tests/Features/Fixtures/ThreadAggregatorUsingOnlyDateTransform.php @@ -0,0 +1,12 @@ +toArray(), ['subscriber_count' => '100']); + return $this->transform($data, [ + ConvertDatesToTimestamps::class, + ]); + } +} diff --git a/tests/Features/TransformersTest.php b/tests/Features/TransformersTest.php index aa21aa43..67840af5 100644 --- a/tests/Features/TransformersTest.php +++ b/tests/Features/TransformersTest.php @@ -13,6 +13,7 @@ use Algolia\ScoutExtended\Transformers\ConvertDatesToTimestamps; use Tests\Features\Fixtures\ThreadWithSearchableArrayUsingTransform; use Algolia\ScoutExtended\Transformers\ConvertNumericStringsToNumbers; +use Tests\Features\Fixtures\ThreadWithSearchableArrayUsingDateTransform; final class TransformersTest extends TestCase { @@ -76,4 +77,22 @@ public function testTransformMethod(): void dispatch(new UpdateJob(collect([$threadWithSearchableArrayUsingTransform]))); } + + public function testTransformsAreRespectedOnSingleModel(): void + { + $thread = factory(Thread::class)->create(); + + $extendedThread = new ThreadWithSearchableArrayUsingDateTransform($thread->toArray()); + + $threadsIndexMock = $this->mockIndex($extendedThread->searchableAs()); + + $threadsIndexMock->shouldReceive('saveObjects')->once()->with(\Mockery::on(function ($argument) { + // Assert subscriber count has not been transformed + return $argument[0]['subscriber_count'] === '100'; + })); + + $extendedThread->created_at = now(); + + dispatch(new UpdateJob(collect([$extendedThread]))); + } }