Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fail Case] Add test for aggregator transforms #162

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tests/Features/AggregatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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());
}
}
12 changes: 12 additions & 0 deletions tests/Features/Fixtures/ThreadAggregatorUsingOnlyDateTransform.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Tests\Features\Fixtures;

use Algolia\ScoutExtended\Searchable\Aggregator;

class ThreadAggregatorUsingOnlyDateTransform extends Aggregator
{
protected $models = [
ThreadWithSearchableArrayUsingDateTransform::class,
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Tests\Features\Fixtures;

use App\Thread;
use Algolia\ScoutExtended\Transformers\ConvertDatesToTimestamps;

class ThreadWithSearchableArrayUsingDateTransform extends Thread
{
protected $table = 'threads';

public function toSearchableArray(): array
{
$data = array_merge($this->toArray(), ['subscriber_count' => '100']);
return $this->transform($data, [
ConvertDatesToTimestamps::class,
]);
}
}
19 changes: 19 additions & 0 deletions tests/Features/TransformersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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])));
}
}