Skip to content

Commit 09bfc14

Browse files
committed
more tests
1 parent cdd405e commit 09bfc14

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

tests/App/Models/Customer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function toElasticsearch(IndexInterface $indexConfig): array
2222

2323
public function shouldIndex(IndexInterface $indexConfig): bool
2424
{
25-
return true;
25+
return ($this->id % 2) === 0;
2626
}
2727

2828
protected static function newFactory(): Factory

tests/Unit/ModelTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,27 @@
55
use Elastica\Document;
66
use Limenet\LaravelElasticaBridge\Index\IndexInterface;
77
use Limenet\LaravelElasticaBridge\Tests\App\Elasticsearch\CustomerIndex;
8+
use Limenet\LaravelElasticaBridge\Tests\App\Elasticsearch\ProductIndex;
89
use Limenet\LaravelElasticaBridge\Tests\App\Models\Customer;
10+
use Limenet\LaravelElasticaBridge\Tests\App\Models\Product;
911

1012
class ModelTest extends TestCase
1113
{
1214
protected CustomerIndex $customerIndex;
15+
protected ProductIndex $productIndex;
1316

1417
public function setUp():void
1518
{
1619
parent::setUp();
1720

1821
$this->customerIndex = $this->app->make(CustomerIndex::class);
22+
$this->productIndex = $this->app->make(ProductIndex::class);
1923
}
2024
/** @test */
21-
public function convert_to_elastica_document()
25+
public function convert_to_elastica_document_customized()
2226
{
2327
Customer::all()
28+
->filter(fn(Customer $customer):bool=>$customer->shouldIndex($this->customerIndex))
2429
->each(function (Customer $customer):void {
2530
$document = $customer->toElasticaDocument($this->customerIndex);
2631
$this->assertInstanceOf(Document::class, $document);
@@ -36,4 +41,22 @@ public function convert_to_elastica_document()
3641
$this->assertSame($customer::class, $document->get(IndexInterface::DOCUMENT_MODEL_CLASS));
3742
});
3843
}
44+
/** @test */
45+
public function convert_to_elastica_document_default()
46+
{
47+
Product::all()
48+
->filter(fn(Product $product):bool=>$product->shouldIndex($this->productIndex))
49+
->each(function (Product $product):void {
50+
$document = $product->toElasticaDocument($this->productIndex);
51+
$this->assertInstanceOf(Document::class, $document);
52+
53+
$this->assertSame($product->name, $document->get('name'));
54+
55+
$this->assertStringContainsString('|'.$product->id, $document->getId());
56+
$this->assertStringContainsString($product::class . '|', $document->getId());
57+
58+
$this->assertSame($product->id, $document->get(IndexInterface::DOCUMENT_MODEL_ID));
59+
$this->assertSame($product::class, $document->get(IndexInterface::DOCUMENT_MODEL_CLASS));
60+
});
61+
}
3962
}

0 commit comments

Comments
 (0)