Skip to content

Commit bf57a2b

Browse files
committed
test empty index
1 parent 5abf180 commit bf57a2b

File tree

8 files changed

+62
-5
lines changed

8 files changed

+62
-5
lines changed

tests/App/Elasticsearch/AllIndex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ProductIndex extends AbstractIndex
1111
{
1212
public function getName(): string
1313
{
14-
return 'testing_product';
14+
return 'testing_all';
1515
}
1616

1717
public function getAllowedDocuments(): array
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
4+
namespace Limenet\LaravelElasticaBridge\Tests\App\Elasticsearch;
5+
6+
use Limenet\LaravelElasticaBridge\Index\AbstractIndex;
7+
use Limenet\LaravelElasticaBridge\Tests\App\Models\Customer;
8+
use Limenet\LaravelElasticaBridge\Tests\App\Models\Order;
9+
10+
class OrderIndex extends AbstractIndex
11+
{
12+
public function getName(): string
13+
{
14+
return 'testing_order';
15+
}
16+
17+
public function getAllowedDocuments(): array
18+
{
19+
return [Order::class];
20+
}
21+
}

tests/App/Models/Order.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Limenet\LaravelElasticaBridge\Tests\App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Illuminate\Database\Eloquent\Factories\HasFactory;
7+
use Illuminate\Database\Eloquent\Model;
8+
use Limenet\LaravelElasticaBridge\Index\IndexInterface;
9+
use Limenet\LaravelElasticaBridge\Model\ElasticsearchableInterface;
10+
use Limenet\LaravelElasticaBridge\Model\ElasticsearchableTrait;
11+
use Limenet\LaravelElasticaBridge\Tests\Database\Factories\CustomerFactory;
12+
13+
class Order extends Model implements ElasticsearchableInterface
14+
{
15+
use ElasticsearchableTrait;
16+
17+
public function toElasticsearch(IndexInterface $indexConfig): array
18+
{
19+
return $this->toArray();
20+
}
21+
}

tests/Feature/CommandTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,10 @@ public function index_command_respects_should_index()
3636
$this->assertCount(0, $this->customerIndex->searchForElements(new MatchQuery(IndexInterface::DOCUMENT_MODEL_ID, 1)));
3737
$this->assertCount(1, $this->customerIndex->searchForElements(new MatchQuery(IndexInterface::DOCUMENT_MODEL_ID, 2)));
3838
}
39+
/** @test */
40+
public function index_command_works_with_no_model_entries()
41+
{
42+
$this->index($this->orderIndex);
43+
$this->assertSame(0, $this->orderIndex->getElasticaIndex()->count());
44+
}
3945
}

tests/Feature/TestCase.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
use Limenet\LaravelElasticaBridge\Index\IndexInterface;
99
use Limenet\LaravelElasticaBridge\Repository\IndexRepository;
1010
use Limenet\LaravelElasticaBridge\Tests\App\Elasticsearch\CustomerIndex;
11+
use Limenet\LaravelElasticaBridge\Tests\App\Elasticsearch\OrderIndex;
1112
use Limenet\LaravelElasticaBridge\Tests\App\Elasticsearch\ProductIndex;
1213
use Limenet\LaravelElasticaBridge\Tests\TestCase as TestsTestCase;
1314

1415
class TestCase extends TestsTestCase
1516
{
1617
protected CustomerIndex $customerIndex;
18+
protected OrderIndex $orderIndex;
1719
protected ProductIndex $productIndex;
1820
protected IndexRepository $indexRepository;
1921
protected ElasticaClient $elasticaClient;
@@ -22,6 +24,7 @@ public function setUp(): void
2224
parent::setUp();
2325

2426
$this->customerIndex = $this->app->make(CustomerIndex::class);
27+
$this->orderIndex = $this->app->make(OrderIndex::class);
2528
$this->productIndex = $this->app->make(ProductIndex::class);
2629
$this->indexRepository = $this->app->make(IndexRepository::class);
2730
$this->elasticaClient = $this->app->make(ElasticaClient::class);
@@ -32,12 +35,11 @@ public function tearDown(): void
3235
{
3336
parent::tearDown();
3437

35-
3638
$this->cleanupIndices();
3739
}
3840
protected function cleanupIndices()
3941
{
40-
foreach ([$this->customerIndex, $this->productIndex] as $index) {
42+
foreach ([$this->customerIndex, $this->orderIndex, $this->productIndex] as $index) {
4143
try {
4244
if ($index->getElasticaIndex()->hasAlias($index->getName())) {
4345
$index->getElasticaIndex()->removeAlias($index->getName());

tests/TestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Database\Eloquent\Factories\Factory;
66
use Limenet\LaravelElasticaBridge\LaravelElasticaBridgeServiceProvider;
77
use Limenet\LaravelElasticaBridge\Tests\App\Elasticsearch\CustomerIndex;
8+
use Limenet\LaravelElasticaBridge\Tests\App\Elasticsearch\OrderIndex;
89
use Limenet\LaravelElasticaBridge\Tests\App\Elasticsearch\ProductIndex;
910
use Limenet\LaravelElasticaBridge\Tests\Database\Seeders\DatabaseSeeder;
1011
use Orchestra\Testbench\TestCase as Orchestra;
@@ -24,7 +25,7 @@ protected function resolveApplicationConfiguration($app)
2425
{
2526
parent::resolveApplicationConfiguration($app);
2627

27-
$app['config']->set('elastica-bridge.indices', [CustomerIndex::class,ProductIndex::class]);
28+
$app['config']->set('elastica-bridge.indices', [CustomerIndex::class, OrderIndex::class, ProductIndex::class]);
2829
}
2930

3031
protected function getPackageProviders($app)

tests/Unit/RepositoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function setUp():void
2424
/** @test */
2525
public function all()
2626
{
27-
$this->assertCount(2, $this->indexRepository->all());
27+
$this->assertCount(3, $this->indexRepository->all());
2828
foreach ($this->indexRepository->all() as $index) {
2929
$this->assertInstanceOf(IndexInterface::class, $index);
3030
}

tests/database/migrations/SetupTables.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public function up():void
2222
$table->timestamps();
2323
});
2424

25+
Schema::create('orders', function (Blueprint $table) {
26+
$table->id();
27+
$table->dateTime('ordered_at');
28+
$table->timestamps();
29+
});
30+
2531
Schema::create('failed_jobs', function (Blueprint $table): void {
2632
$table->id();
2733
$table->string('uuid')->unique();

0 commit comments

Comments
 (0)