Skip to content

Commit

Permalink
Test case for issue #14
Browse files Browse the repository at this point in the history
  • Loading branch information
sunel committed Sep 3, 2018
1 parent 60fc58a commit 59b1731
Show file tree
Hide file tree
Showing 5 changed files with 427 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/Feature/Issue14Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Eav\TestCase\Feature;

use Eav\Entity;

class Pcs extends \Eav\Model
{
const ENTITY = 'pc';
}

class Issue14Test extends TestCase
{
/** @test */
public function it_can_show_2_attribute_same_name_of_2_entity()
{
$this->product();

$carSkuAttr = Entity::findByCode('car')->attributes()->where('attribute_code', 'sku')->get();
$pcSkuAttr = Entity::findByCode('pc')->attributes()->where('attribute_code', 'sku')->get();

$this->assertNotEquals($carSkuAttr->first()->getKey(), $pcSkuAttr->first()->getKey());

$this->assertEquals(Cars::all(['sku'])->first()->sku, 'PDO1HJK92');
$this->assertEquals(Pcs::all(['sku'])->first()->sku, 'PDOBEEAM112');
}

private function product()
{
Cars::create([
'sku' => 'PDO1HJK92'
]);

Pcs::create([
'sku' => 'PDOBEEAM112'
]);
}
}
34 changes: 34 additions & 0 deletions tests/migrations/2018_08_29_015142_create_pc_entity_main_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePcEntityMainTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('pcs', function (Blueprint $table) {
$table->increments('id');
$table->integer('entity_id')->unsigned();
$table->integer('attribute_set_id')->unsigned();

$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('pcs');
}
}
271 changes: 271 additions & 0 deletions tests/migrations/2018_08_29_015144_create_pc_entity_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
<?php

use Eav\Entity;
use Eav\Attribute;
use Eav\AttributeSet;
use Eav\AttributeGroup;
use Eav\EntityAttribute;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePcEntityTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('pc_boolean', function (Blueprint $table) {
$table->increments('value_id')->comment('Value ID');
$table->smallInteger('entity_type_id')->unsigned()->default(0)->comment('Entity Type ID');
$table->integer('attribute_id')->unsigned()->default(0)->comment('Attribute ID');
$table->integer('entity_id')->unsigned()->default(0)->comment('Entity ID');

$table->boolean('value')->default(null)->nullable()->comment('Value');

$table->foreign('entity_id')
->references('id')->on('pcs')
->onDelete('cascade');

$table->unique(['entity_id','attribute_id']);
$table->index('attribute_id');
$table->index('entity_id');
});

Schema::create('pc_date', function (Blueprint $table) {
$table->increments('value_id')->comment('Value ID');
$table->smallInteger('entity_type_id')->unsigned()->default(0)->comment('Entity Type ID');
$table->integer('attribute_id')->unsigned()->default(0)->comment('Attribute ID');
$table->integer('entity_id')->unsigned()->default(0)->comment('Entity ID');

$table->date('value')->default(null)->nullable()->comment('Value');

$table->foreign('entity_id')
->references('id')->on('pcs')
->onDelete('cascade');

$table->unique(['entity_id','attribute_id']);
$table->index('attribute_id');
$table->index('entity_id');
});

Schema::create('pc_dateTime', function (Blueprint $table) {
$table->increments('value_id')->comment('Value ID');
$table->smallInteger('entity_type_id')->unsigned()->default(0)->comment('Entity Type ID');
$table->integer('attribute_id')->unsigned()->default(0)->comment('Attribute ID');
$table->integer('entity_id')->unsigned()->default(0)->comment('Entity ID');

$table->dateTime('value')->default(null)->nullable()->comment('Value');

$table->foreign('entity_id')
->references('id')->on('pcs')
->onDelete('cascade');

$table->unique(['entity_id','attribute_id']);
$table->index('attribute_id');
$table->index('entity_id');
});

Schema::create('pc_double', function (Blueprint $table) {
$table->increments('value_id')->comment('Value ID');
$table->smallInteger('entity_type_id')->unsigned()->default(0)->comment('Entity Type ID');
$table->integer('attribute_id')->unsigned()->default(0)->comment('Attribute ID');
$table->integer('entity_id')->unsigned()->default(0)->comment('Entity ID');

$table->double('value')->default(null)->nullable()->comment('Value');

$table->foreign('entity_id')
->references('id')->on('pcs')
->onDelete('cascade');

$table->unique(['entity_id','attribute_id']);
$table->index('attribute_id');
$table->index('entity_id');
});

Schema::create('pc_integer', function (Blueprint $table) {
$table->increments('value_id')->comment('Value ID');
$table->smallInteger('entity_type_id')->unsigned()->default(0)->comment('Entity Type ID');
$table->integer('attribute_id')->unsigned()->default(0)->comment('Attribute ID');
$table->integer('entity_id')->unsigned()->default(0)->comment('Entity ID');

$table->integer('value')->default(null)->nullable()->comment('Value');

$table->foreign('entity_id')
->references('id')->on('pcs')
->onDelete('cascade');

$table->unique(['entity_id','attribute_id']);
$table->index('attribute_id');
$table->index('entity_id');
});

Schema::create('pc_text', function (Blueprint $table) {
$table->increments('value_id')->comment('Value ID');
$table->smallInteger('entity_type_id')->unsigned()->default(0)->comment('Entity Type ID');
$table->integer('attribute_id')->unsigned()->default(0)->comment('Attribute ID');
$table->integer('entity_id')->unsigned()->default(0)->comment('Entity ID');

$table->text('value')->default(null)->nullable()->comment('Value');

$table->foreign('entity_id')
->references('id')->on('pcs')
->onDelete('cascade');

$table->unique(['entity_id','attribute_id']);
$table->index('attribute_id');
$table->index('entity_id');
});

Schema::create('pc_string', function (Blueprint $table) {
$table->increments('value_id')->comment('Value ID');
$table->smallInteger('entity_type_id')->unsigned()->default(0)->comment('Entity Type ID');
$table->integer('attribute_id')->unsigned()->default(0)->comment('Attribute ID');
$table->integer('entity_id')->unsigned()->default(0)->comment('Entity ID');

$table->string('value')->default(null)->nullable()->comment('Value');

$table->foreign('entity_id')
->references('id')->on('pcs')
->onDelete('cascade');

$table->unique(['entity_id','attribute_id']);
$table->index('attribute_id');
$table->index('entity_id');
});


$entity = Entity::create([
'entity_code' => 'pc',
'entity_class' => '\App\Pcs',
'entity_table' => 'pcs',
]);


$attributeSet = AttributeSet::create([
'attribute_set_name' => 'Default',
'entity_id' => $entity->entity_id,
]);

$entity->default_attribute_set_id = $attributeSet->attribute_set_id;
$entity->save();

$attributeGroup = AttributeGroup::create([
'attribute_set_id' => $attributeSet->attribute_set_id,
'attribute_group_name' => 'General',
]);

$this->addTimeStampAttributes();
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$this->removeTimeStampAttributes();


Schema::drop('pc_boolean');

Schema::drop('pc_date');

Schema::drop('pc_dateTime');

Schema::drop('pc_double');

Schema::drop('pc_integer');

Schema::drop('pc_text');

Schema::drop('pc_string');


$entity = Entity::where('entity_code', '=', 'pc');
$attributeSet = AttributeSet::where('attribute_set_name', '=', 'Default')
->where('entity_id', '=', $entity->first()->entity_id);
$attributeGroup = AttributeGroup::where('attribute_set_id', '=', $attributeSet->first()->attribute_set_id)
->where('attribute_group_name', '=', 'General');


$attributeGroup->delete();
$attributeSet->delete();
$entity->delete();
}


protected function addTimeStampAttributes()
{
Attribute::add([
'attribute_code' => 'created_at',
'entity_code' => 'pc',
'backend_class' => null,
'backend_type' => 'static',
'backend_table' => null,
'frontend_class' => null,
'frontend_type' => 'input',
'frontend_label' => ucwords(str_replace('_', ' ', 'created_at')),
'source_class' => null,
'default_value' => '',
'is_required' => 0,
'required_validate_class' => null
]);

EntityAttribute::map([
'attribute_code' => 'created_at',
'entity_code' => 'pc',
'attribute_set' => 'Default',
'attribute_group' => 'General'
]);

Attribute::add([
'attribute_code' => 'updated_at',
'entity_code' => 'pc',
'backend_class' => null,
'backend_type' => 'static',
'backend_table' => null,
'frontend_class' => null,
'frontend_type' => 'input',
'frontend_label' => ucwords(str_replace('_', ' ', 'updated_at')),
'source_class' => null,
'default_value' => '',
'is_required' => 0,
'required_validate_class' => null
]);

EntityAttribute::map([
'attribute_code' => 'updated_at',
'entity_code' => 'pc',
'attribute_set' => 'Default',
'attribute_group' => 'General'
]);
}

protected function removeTimeStampAttributes()
{
EntityAttribute::unmap([
'attribute_code' => 'created_at',
'entity_code' => 'pc',
]);

Attribute::remove([
'attribute_code' => 'created_at',
'entity_code' => 'pc',
]);

EntityAttribute::unmap([
'attribute_code' => 'updated_at',
'entity_code' => 'pc',
]);

Attribute::remove([
'attribute_code' => 'updated_at',
'entity_code' => 'pc',
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use Eav\Attribute;
use Eav\EntityAttribute;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePcEntityAttributes015205 extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Attribute::add([
'attribute_code' => 'sku',
'entity_code' => 'pc',
'backend_class' => null,
'backend_type' => 'string',
'backend_table' => null,
'frontend_class' => null,
'frontend_type' => 'text',
'frontend_label' => ucwords(str_replace('_', ' ', 'sku')),
'source_class' => null,
'default_value' => '',
'is_required' => 0,
'required_validate_class' => null
]);
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Attribute::remove([
'attribute_code' => 'sku',
'entity_code' => 'pc',
]);
}
}
Loading

0 comments on commit 59b1731

Please sign in to comment.