Skip to content

Commit 6cd6beb

Browse files
committed
WIP tests
1 parent 9721236 commit 6cd6beb

File tree

12 files changed

+209
-21
lines changed

12 files changed

+209
-21
lines changed

CHANGELOG.md

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

33
All notable changes to `laravel-elastica-bridge` will be documented in this file.
44

5+
## 1.1.0 - UNRELEASED
6+
7+
- provide default implementation for `Limenet\LaravelElasticaBridge\Model\ElasticsearchableInterface\toElasticsearch`
8+
- added tests
9+
510
## 1.0.0 - 2021-05-09
611

712
- initial release

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@
3737
},
3838
"autoload-dev": {
3939
"psr-4": {
40-
"Limenet\\LaravelElasticaBridge\\Tests\\": "tests"
40+
"Limenet\\LaravelElasticaBridge\\Tests\\": "tests",
41+
"Limenet\\LaravelElasticaBridge\\Tests\\Database\\Factories\\": "tests/database/factories",
42+
"Limenet\\LaravelElasticaBridge\\Tests\\Database\\Seeders\\": "tests/database/seeders"
4143
}
4244
},
4345
"scripts": {
4446
"psalm": "vendor/bin/psalm",
4547
"test": "./vendor/bin/testbench package:test --parallel --no-coverage",
46-
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
48+
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html coverage",
49+
"code-swift": "docker exec -it --user=laradock -w /var/www/$(git rev-parse --show-toplevel | xargs basename | cat) $(docker ps -q --filter name=laradock_workspace-8.0_) zsh"
4750
},
4851
"config": {
4952
"sort-packages": true

phpunit.xml.dist

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
<directory>tests</directory>
2424
</testsuite>
2525
</testsuites>
26-
<coverage>
26+
<coverage
27+
includeUncoveredFiles="true"
28+
processUncoveredFiles="true"
29+
pathCoverage="true"
30+
>
2731
<include>
2832
<directory suffix=".php">./src</directory>
2933
</include>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
9+
class CustomerIndex extends AbstractIndex {
10+
11+
public function getName(): string { return 'testing_customer'; }
12+
13+
public function getAllowedDocuments(): array {
14+
return [Customer::class];
15+
}
16+
17+
public function getMapping(): array
18+
{
19+
return [
20+
'properties' => [
21+
'group' => [
22+
'type' => 'keyword',
23+
],
24+
]
25+
];
26+
}
27+
}

tests/App/Models/Customer.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 Customer extends Model implements ElasticsearchableInterface{
14+
use HasFactory;
15+
use ElasticsearchableTrait;
16+
17+
public function toElasticsearch(IndexInterface $indexConfig): array { return $this->toArray(); }
18+
19+
public function shouldIndex(IndexInterface $indexConfig): bool { return true;}
20+
protected static function newFactory():Factory
21+
{
22+
return CustomerFactory::new();
23+
}
24+
}

tests/ExampleTest.php

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

tests/IndexTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Limenet\LaravelElasticaBridge\Tests;
4+
5+
use Elastica\Document;
6+
use Limenet\LaravelElasticaBridge\Tests\App\Elasticsearch\CustomerIndex;
7+
use Limenet\LaravelElasticaBridge\Tests\App\Models\Customer;
8+
9+
class IndexTest extends TestCase
10+
{
11+
protected CustomerIndex $customerIndex;
12+
13+
public function setUp():void {
14+
15+
parent::setUp();
16+
17+
$this->customerIndex = $this->app->make(CustomerIndex::class);
18+
}
19+
/** @test */
20+
public function index_settings()
21+
{
22+
$settings=$this->customerIndex->getCreateArguments();
23+
if($this->customerIndex->hasMapping()){
24+
$this->assertArrayHasKey('mappings',$settings);
25+
}
26+
}
27+
}

tests/ModelTest.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;
4+
5+
use Elastica\Document;
6+
use Limenet\LaravelElasticaBridge\Tests\App\Elasticsearch\CustomerIndex;
7+
use Limenet\LaravelElasticaBridge\Tests\App\Models\Customer;
8+
9+
class ModelTest extends TestCase
10+
{
11+
protected CustomerIndex $customerIndex;
12+
13+
public function setUp():void {
14+
15+
parent::setUp();
16+
17+
$this->customerIndex = $this->app->make(CustomerIndex::class);
18+
}
19+
/** @test */
20+
public function convert_to_elastica_document()
21+
{
22+
/** @var Customer $customer */
23+
$customer=Customer::first();
24+
$document = $customer->toElasticaDocument($this->customerIndex);
25+
$this->assertInstanceOf(Document::class, $document);
26+
27+
$this->assertSame($customer->name,$document->get('name'));
28+
$this->assertSame($customer->email,$document->get('email'));
29+
}
30+
}

tests/TestCase.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
namespace Limenet\LaravelElasticaBridge\Tests;
44

55
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
67
use Limenet\LaravelElasticaBridge\LaravelElasticaBridgeServiceProvider;
8+
use Limenet\LaravelElasticaBridge\Tests\Database\Seeders\DatabaseSeeder;
79
use Orchestra\Testbench\TestCase as Orchestra;
10+
use SetupTables;
811

912
class TestCase extends Orchestra
1013
{
14+
// use RefreshDatabase;
1115
public function setUp(): void
1216
{
1317
parent::setUp();
1418

1519
Factory::guessFactoryNamesUsing(
16-
fn (string $modelName) => 'Spatie\\LaravelElasticaBridge\\Database\\Factories\\'.class_basename($modelName).'Factory'
20+
fn (string $modelName) => 'Limenet\\LaravelElasticaBridge\\Tests\\Database\\Factories\\'.class_basename($modelName).'Factory'
1721
);
1822
}
1923

@@ -27,10 +31,11 @@ protected function getPackageProviders($app)
2731
public function getEnvironmentSetUp($app)
2832
{
2933
config()->set('database.default', 'testing');
30-
31-
/*
32-
include_once __DIR__.'/../database/migrations/create_laravel-elastica-bridge_table.php.stub';
33-
(new \CreatePackageTable())->up();
34-
*/
34+
}
35+
protected function defineDatabaseMigrations():void
36+
{
37+
include_once __DIR__.'/database/migrations/SetupTables.php';
38+
(new SetupTables())->up();
39+
$this->artisan('db:seed', ['--class' => DatabaseSeeder::class])->run();
3540
}
3641
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Limenet\LaravelElasticaBridge\Tests\Database\Factories;
4+
5+
use App\Models\User;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
use Illuminate\Support\Str;
8+
use Limenet\LaravelElasticaBridge\Tests\App\Models\Customer;
9+
10+
class CustomerFactory extends Factory
11+
{
12+
/**
13+
* The name of the factory's corresponding model.
14+
*
15+
* @var string
16+
*/
17+
protected $model = Customer::class;
18+
19+
/**
20+
* Define the model's default state.
21+
*
22+
* @return array
23+
*/
24+
public function definition()
25+
{
26+
return [
27+
'name' => $this->faker->name(),
28+
'email' => $this->faker->unique()->safeEmail(),
29+
'type'=>$this->faker->randomElement(['small','medium','big'])
30+
];
31+
}
32+
}

0 commit comments

Comments
 (0)