Skip to content

Commit

Permalink
typo's & entity test case
Browse files Browse the repository at this point in the history
  • Loading branch information
sunel committed Aug 26, 2018
1 parent 899b0d5 commit 1294fca
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 71 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
},
"require-dev": {
"mockery/mockery": "~1.0",
"fzaninotto/faker": "~1.4",
"orchestra/testbench": "~3.0"
"fzaninotto/faker": "~1.4"
},
"authors": [
{
Expand Down
19 changes: 18 additions & 1 deletion docs/master/ideology/flat-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,21 @@ To Deactivate the Flat table for the entity

```bash
$ php artisan eav:flat:entity [entity_code] -E false
```
```

::: warning
When Flat table is enabled and if you try to insert or update a entity, it will update only the flat table.

You can temporarily enabled or disable flat table though the code.

```php

$product = new Product();

// Disable
$product->setUseFlat(false);

// Enable
$product->setUseFlat(true);
```
:::
3 changes: 2 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
</filter>

<php>
<env name="DB_CONNECTION" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
4 changes: 2 additions & 2 deletions src/Attribute/Concerns/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ public function addAttributeJoin($query, $joinType = 'inner', $callback = null)
} else {
if ($joinType == 'left') {
$query->leftJoin("{$this->getBackendTable()} as {$this->getAttributeCode()}_attr", function ($join) use ($query) {
$join->on("{$query->from}.{$this->getEntity()->getEnityKey()}", '=', "{$this->getAttributeCode()}_attr.entity_id")
$join->on("{$query->from}.{$this->getEntity()->getEntityKey()}", '=', "{$this->getAttributeCode()}_attr.entity_id")
->where("{$this->getAttributeCode()}_attr.attribute_id", "=", $this->getAttributeId());
});
} else {
$query->join("{$this->getBackendTable()} as {$this->getAttributeCode()}_attr", function ($join) use ($query) {
$join->on("{$query->from}.{$this->getEntity()->getEnityKey()}", '=', "{$this->getAttributeCode()}_attr.entity_id")
$join->on("{$query->from}.{$this->getEntity()->getEntityKey()}", '=', "{$this->getAttributeCode()}_attr.entity_id")
->where("{$this->getAttributeCode()}_attr.attribute_id", "=", $this->getAttributeId());
});
}
Expand Down
12 changes: 6 additions & 6 deletions src/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ public function update(array $values)

$loadedAttributes->validate($values);

$this->select($this->baseEntity()->getEnityKey(), ...$loadedAttributes->keys()->toArray());
$this->select($this->baseEntity()->getEntityKey(), ...$loadedAttributes->keys()->toArray());

$entityIds = $this->pluck($this->baseEntity()->getEnityKey())->toArray();
$entityIds = $this->pluck($this->baseEntity()->getEntityKey())->toArray();

$loadedAttributes->each(function ($attr, $code) use (&$values, $entityIds) {
if (!$attr->isStatic()) {
Expand All @@ -143,7 +143,7 @@ public function update(array $values)
});

$query = $this->newQuery()->from($this->from)
->whereIn($this->baseEntity()->getEnityKey(), $entityIds);
->whereIn($this->baseEntity()->getEntityKey(), $entityIds);

$sql = $this->grammar->compileUpdate($query, $values);

Expand Down Expand Up @@ -252,7 +252,7 @@ protected function fixColumns()

$columns = [];
$removeCol = [
'attr.*', '*', $this->baseEntity()->getEnityKey()
'attr.*', '*', $this->baseEntity()->getEntityKey()
];

$allAttr = $orgColumns->get('columns')->contains('attr.*');
Expand All @@ -263,8 +263,8 @@ protected function fixColumns()
$columns[] = "{$this->from}.*";
}
// ->select(['id']) or ->select(['id', 'color'])
else if ($orgColumns->get('columns')->contains($this->baseEntity()->getEnityKey())) {
$columns[] = "{$this->from}.{$this->baseEntity()->getEnityKey()}";
else if ($orgColumns->get('columns')->contains($this->baseEntity()->getEntityKey())) {
$columns[] = "{$this->from}.{$this->baseEntity()->getEntityKey()}";
}

// We check if the select has only `*`, if so then we have nothing
Expand Down
25 changes: 8 additions & 17 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ abstract class Model extends Eloquent
* @var bool
*/
protected static $unguarded = true;

/**
* Array of loaded Entity.
*
* @var array
*/
protected static $baseEntity = [];

/**
* Create a new Eloquent model instance.
Expand All @@ -60,17 +53,15 @@ public function __construct(array $attributes = [])
*/
public function baseEntity()
{
if (!isset(static::$baseEntity[static::ENTITY])) {
try {
$eavEntity = Entity::findByCode(static::ENTITY);
$eavEntity->setEnityKey($this->getKeyName());
} catch (ModelNotFoundException $e) {
throw new \Exception("Unable to load Entity : ".static::ENTITY);
}
static::$baseEntity[static::ENTITY] = $eavEntity;
try {
$eavEntity = Entity::findByCode(static::ENTITY);
$eavEntity->setEntityKey($this->getKeyName());
$eavEntity->setEntityCustomTable($this->table);
} catch (ModelNotFoundException $e) {
throw new \Exception("Unable to load Entity : ".static::ENTITY);
}
return static::$baseEntity[static::ENTITY];

return $eavEntity;
}

/**
Expand Down
125 changes: 99 additions & 26 deletions tests/Feature/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,129 @@ class EntityTest extends TestCase
*/
protected $entity;

/**
* @var Eav\Entity
*/
protected $entity_flat;

/**
* Setup the test environment.
*/
protected function setUp(): void
{
parent::setUp();

$this->entity = factory(Entity::class)->create();
$set = factory(AttributeSet::class)->create([
'entity_id' => $this->entity->entity_id,
$this->entity = factory(Entity::class)->create([
'entity_code' => 'custom'
]);

$this->entity->default_attribute_set_id = $set->attribute_set_id;
$this->entity->save();
$this->entity_flat = factory(Entity::class)->states('flat')->create([
'entity_code' => 'custom_1'
]);
}


/** @test */
public function it_creates_entity()
{
$entityDB = \DB::table('entities')->where('entity_id', '=', 1)->first();
public function it_can_be_found_by_code()
{
$entityDB = Entity::findByCode($this->entity->entity_code);
$this->assertEquals($this->entity->entity_id, $entityDB->entity_id);
$this->assertEquals($this->entity->entity_code, $entityDB->entity_code);
}

/** @test */
public function it_can_be_found_by_id()
{
$entityDB = Entity::findById($this->entity->entity_id);
$this->assertEquals($this->entity->entity_code, $entityDB->entity_code);
$this->assertEquals($this->entity->entity_id, $entityDB->entity_id);
}

/** @test */
public function it_can_detect_flat_table()
{
$this->assertEquals($this->entity->canUseFlat(), 0);
$this->assertEquals($this->entity_flat->canUseFlat(), 1);
}

/** @test */
public function it_can_be_found_by_code()
{
$entity = factory(Entity::class, 2)->create();
$entityDB = Entity::findByCode($entity->last()->entity_code);
$this->assertEquals($entity->last()->entity_id, $entityDB->entity_id);
$this->assertEquals($entity->last()->entity_code, $entityDB->entity_code);
public function it_can_detect_flat_table_name()
{
$eloquent = new class() extends \Eav\Model {
const ENTITY = 'custom';
protected $table = 'custom_table';
};

$eloquent_1 = new class() extends \Eav\Model {
const ENTITY = 'custom_1';
protected $table = 'custom_table';
};

$this->assertEquals($eloquent->getTable(), 'custom_table');
$this->assertEquals($eloquent_1->getTable(), 'custom_table_flat');
}

/** @test */
public function it_can_be_found_by_id()
{
$entity = factory(Entity::class, 3)->create();
$entityDB = Entity::findById($entity->last()->entity_id);
$this->assertEquals($entity->last()->entity_code, $entityDB->entity_code);
$this->assertEquals($entity->last()->entity_id, $entityDB->entity_id);
public function it_can_detect_key_name()
{
$eloquent = new class() extends \Eav\Model {
const ENTITY = 'custom';
protected $primaryKey = 'custom_id';
};

$entity = $eloquent->baseEntity();

$this->assertEquals($entity->getEntityKey(), 'custom_id');
$this->assertEquals($eloquent->getKeyName(), $entity->getEntityKey());
}

/** @test */
public function it_can_detect_flat_table()
{
$entity = factory(Entity::class)->create();
$this->assertEquals($entity->canUseFlat(), 0);
public function it_can_detect_entity_table_name()
{
$eloquent = new class() extends \Eav\Model {
const ENTITY = 'custom';
protected $table = 'custom_table';
};

$entity = $eloquent->baseEntity();

$this->assertEquals($entity->getEntityTableName(), $eloquent->getTable());
}

/** @test */
public function it_can_have_many_attributes()
{
$entity = Entity::findByCode('product');

$this->assertTrue($entity->attributes->isNotEmpty());
}

/** @test */
public function it_can_have_many_attribute_set()
{
$entity = Entity::findByCode('product');

$this->assertTrue($entity->attributeSet->isNotEmpty());
}

/** @test */
public function it_must_have_default_attribute_set()
{
$set = factory(AttributeSet::class)->create([
'entity_id' => $this->entity->entity_id,
]);

$this->entity->default_attribute_set_id = $set->attribute_set_id;
$this->entity->save();

$this->assertEquals($this->entity->entity_id, $set->entity_id);
$this->assertEquals($this->entity->defaultAttributeSet->attribute_set_id, $set->attribute_set_id);
}

protected function tearDown()
{
parent::tearDown();

$entity = factory(Entity::class)->states('flat')->create();
$this->assertEquals($entity->canUseFlat(), 1);
Entity::clearStaticCache();
}
}
23 changes: 7 additions & 16 deletions tests/Feature/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,23 @@

namespace Eav\TestCase\Feature;

use Orchestra\Testbench\TestCase as Testbench;
use Eav\Providers\LaravelServiceProvider;
use Tests\TestCase as Testbench;
use Illuminate\Database\Eloquent\Factory as ModelFactory;
use Illuminate\Foundation\Testing\RefreshDatabase;

abstract class TestCase extends Testbench
{
use RefreshDatabase;

/**
* Setup the test environment.
*/
protected function setUp(): void
{
parent::setUp();

$this->withFactories(__DIR__.'/../factories');
$this->artisan('migrate', ['--database' => 'testing']);
$this->artisan('migrate', ['--path' => __DIR__ . '/database/migrations']);
}
app()->make(ModelFactory::class)->load(__DIR__.'/../factories');

/**
* Get package providers.
*
* @param \Illuminate\Foundation\Application $app
*
* @return array
*/
protected function getPackageProviders($app)
{
return [LaravelServiceProvider::class];
$this->artisan('migrate', ['--path' => __DIR__ . '/../migrations/']);
}
}

0 comments on commit 1294fca

Please sign in to comment.