Skip to content

Commit 753d1fc

Browse files
committed
more tests
1 parent cb73413 commit 753d1fc

File tree

6 files changed

+151
-13
lines changed

6 files changed

+151
-13
lines changed

tests/Feature/CommandTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Limenet\LaravelElasticaBridge\Tests\Feature;
4+
5+
use Limenet\LaravelElasticaBridge\Index\IndexInterface;
6+
7+
class CommandTest extends TestCase
8+
{
9+
/** @test */
10+
public function status()
11+
{
12+
$this->assertSame(0, $this->artisan('elastica-bridge:status')->run());
13+
}
14+
/** @test */
15+
public function index_command_creates_index()
16+
{
17+
$this->assertFalse($this->productIndex->getElasticaIndex()->exists());
18+
$this->assertSame(0, $this->index($this->productIndex));
19+
$this->assertSame(0, $this->artisan('elastica-bridge:status')->run());
20+
$this->assertTrue($this->productIndex->getElasticaIndex()->exists());
21+
}
22+
/** @test */
23+
public function index_command_swtiches_blue_green()
24+
{
25+
$this->index($this->productIndex);
26+
$activeOld = $this->productIndex->getBlueGreenActiveElasticaIndex()->getName();
27+
$this->index($this->productIndex);
28+
$this->assertNotSame($activeOld, $this->productIndex->getBlueGreenActiveElasticaIndex()->getName());
29+
}
30+
}

tests/Feature/FeatureTest.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/Feature/QueryTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Limenet\LaravelElasticaBridge\Tests\Feature;
4+
5+
use Elastica\Query\BoolQuery;
6+
use Elastica\Query\MatchQuery;
7+
use Limenet\LaravelElasticaBridge\Index\IndexInterface;
8+
9+
class QueryTest extends TestCase
10+
{
11+
/** @test */
12+
public function get_by_id()
13+
{
14+
$id=17;
15+
$this->index($this->productIndex);
16+
$elements=$this->productIndex->searchForElements(new MatchQuery(IndexInterface::DOCUMENT_MODEL_ID, $id));
17+
18+
$this->assertCount(1, $elements);
19+
$this->assertSame(17, $elements[0]->id);
20+
}
21+
/** @test */
22+
public function size_and_from()
23+
{
24+
$this->index($this->productIndex);
25+
26+
$elements1=$this->productIndex->searchForElements(new BoolQuery(),5,0);
27+
$this->assertCount(5, $elements1);
28+
29+
$elements2=$this->productIndex->searchForElements(new BoolQuery(),5,5);
30+
$this->assertCount(5, $elements2);
31+
$this->assertEmpty(collect($elements1)->map->id->intersect(collect($elements2)->map->id)->toArray());
32+
}
33+
}

tests/Feature/TestCase.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace Limenet\LaravelElasticaBridge\Tests\Feature;
44

5+
use Elastica\Exception\ResponseException;
6+
use Limenet\LaravelElasticaBridge\Client\ElasticaClient;
7+
use Limenet\LaravelElasticaBridge\Exception\BaseException;
8+
use Limenet\LaravelElasticaBridge\Index\IndexInterface;
59
use Limenet\LaravelElasticaBridge\Repository\IndexRepository;
610
use Limenet\LaravelElasticaBridge\Tests\App\Elasticsearch\CustomerIndex;
711
use Limenet\LaravelElasticaBridge\Tests\App\Elasticsearch\ProductIndex;
@@ -12,12 +16,47 @@ class TestCase extends TestsTestCase
1216
protected CustomerIndex $customerIndex;
1317
protected ProductIndex $productIndex;
1418
protected IndexRepository $indexRepository;
19+
protected ElasticaClient $elasticaClient;
1520
public function setUp(): void
1621
{
1722
parent::setUp();
1823

1924
$this->customerIndex = $this->app->make(CustomerIndex::class);
2025
$this->productIndex = $this->app->make(ProductIndex::class);
2126
$this->indexRepository = $this->app->make(IndexRepository::class);
27+
$this->elasticaClient=$this->app->make(ElasticaClient::class);
28+
29+
$this->cleanupIndices();
30+
}
31+
public function tearDown(): void
32+
{
33+
parent::tearDown();
34+
35+
36+
$this->cleanupIndices();
37+
}
38+
protected function cleanupIndices(){
39+
40+
foreach([$this->customerIndex, $this->productIndex] as $index){
41+
try{
42+
if($index->getElasticaIndex()->hasAlias($index->getName())){
43+
$index->getElasticaIndex()->removeAlias($index->getName());
44+
}
45+
$active=$index->getBlueGreenActiveElasticaIndex();
46+
$inactive=$index->getBlueGreenInactiveElasticaIndex();
47+
if($active->exists()){
48+
$active->delete();
49+
}
50+
if($inactive->exists()){
51+
$inactive->delete();
52+
}
53+
}catch(BaseException|ResponseException){
54+
55+
}
56+
}
57+
}
58+
59+
protected function index(IndexInterface $index):int{
60+
return $this->artisan('elastica-bridge:index', ['index' => [$index->getName()]])->run();
2261
}
2362
}

tests/Unit/FacadeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Limenet\LaravelElasticaBridge\Tests\Unit;
4+
5+
use Elastica\Client;
6+
use Limenet\LaravelElasticaBridge\Client\ElasticaClient;
7+
use Limenet\LaravelElasticaBridge\LaravelElasticaBridgeFacade;
8+
9+
class FacadeTest extends TestCase
10+
{
11+
/** @test */
12+
public function facade()
13+
{
14+
$this->assertInstanceOf(Client::class, LaravelElasticaBridgeFacade::getClient());
15+
}
16+
}

tests/database/migrations/SetupTables.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,38 @@ public function up():void
2121
$table->string('name');
2222
$table->timestamps();
2323
});
24+
25+
Schema::create('failed_jobs', function (Blueprint $table): void {
26+
$table->id();
27+
$table->string('uuid')->unique();
28+
$table->text('connection');
29+
$table->text('queue');
30+
$table->longText('payload');
31+
$table->longText('exception');
32+
$table->timestamp('failed_at')->useCurrent();
33+
});
34+
35+
Schema::create('jobs', function (Blueprint $table): void {
36+
$table->bigIncrements('id');
37+
$table->string('queue')->index();
38+
$table->longText('payload');
39+
$table->unsignedTinyInteger('attempts');
40+
$table->unsignedInteger('reserved_at')->nullable();
41+
$table->unsignedInteger('available_at');
42+
$table->unsignedInteger('created_at');
43+
});
44+
45+
Schema::create('job_batches', function (Blueprint $table): void {
46+
$table->string('id')->primary();
47+
$table->string('name');
48+
$table->integer('total_jobs');
49+
$table->integer('pending_jobs');
50+
$table->integer('failed_jobs');
51+
$table->text('failed_job_ids');
52+
$table->text('options')->nullable();
53+
$table->integer('cancelled_at')->nullable();
54+
$table->integer('created_at');
55+
$table->integer('finished_at')->nullable();
56+
});
2457
}
2558
};

0 commit comments

Comments
 (0)