Skip to content

Commit

Permalink
updated migration tables names for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sunel committed Sep 1, 2018
1 parent 82fbbdc commit d7b4e98
Show file tree
Hide file tree
Showing 14 changed files with 503 additions and 226 deletions.
9 changes: 9 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
* text=auto

/.github export-ignore
/docs export-ignore
/tests export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.php_cs export-ignore
51 changes: 41 additions & 10 deletions src/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ protected function fixColumns()
$loadedAttributes = null;
$columns = $this->columns;

if(is_null($columns)) {
return $this;
}

$orgColumns = collect((array) $columns)->mapToGroups(function ($item, $key) {
if (is_a($item, Expression::class)) {
return ['expression' => $item];
Expand Down Expand Up @@ -313,7 +317,6 @@ protected function fixColumns()
}

/**
* TODO
*
* Reads the column that is added to the select clause and process it
* based on the given sudo code eg : attr.*, * for flat table.
Expand All @@ -323,16 +326,44 @@ protected function fixColumns()
protected function fixFlatColumns()
{
$columns = $this->columns;

if ($columns == ['attr.*'] || $columns == 'attr.*') {
$columns = ["{$this->from}.*"];
} else {
if ($columns != ['*']) {
$columns = array_merge(
\Config::get('eav.entity.'.$this->baseEntity()->entity_code.'.columns', []),
$columns
);

$orgColumns = collect((array) $columns)->mapToGroups(function ($item, $key) {
if (is_a($item, Expression::class)) {
return ['expression' => $item];
} else {
return ['columns' => $item];
}
});

$columns = [];

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

$allAttr = $orgColumns->get('columns')->contains('attr.*');
$allMain = $orgColumns->get('columns')->contains('*');

// ->select(['attr.*']) or ->select(['*'])
if($allAttr || $allMain) {
$columns[] = "{$this->from}.*";
}
// ->select(['id']) or ->select(['id', 'color'])
else if ($orgColumns->get('columns')->contains($this->baseEntity()->getEntityKey())) {
$columns[] = "{$this->from}.{$this->baseEntity()->getEntityKey()}";
}

$columns = $orgColumns
->get('columns')
->merge($columns)
->filter(function ($value, $key) use ($removeCol) {
return !(in_array($value, $removeCol));
})->unique()->toArray();

// Merge the expression back to the query

if ($expression = $orgColumns->get('expression')) {
$columns = $expression->merge($columns)->all();
}

$this->columns = $columns;
Expand Down
2 changes: 1 addition & 1 deletion src/ProcessAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static function process(
->filter(function ($attribute) use ($filterAttr) {
return isset($filterAttr[$attribute->getAttributeCode()]);
});

foreach ((array) $query->attributeOrderBy as $bindings) {
foreach ($bindings as $binding) {
$attribute = $usedAttributes->get($binding['column']);
Expand Down
18 changes: 9 additions & 9 deletions tests/Feature/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,14 @@ public function it_can_insert_data()
{
$sku = $this->addSku([
'attribute_code' => 'upc',
'entity_code' => 'product',
'entity_code' => 'car',
]);

$value = 'HGKHDGEYTT'. rand();

$eloquent = new class() extends \Eav\Model {
const ENTITY = 'product';
protected $table = 'products';
const ENTITY = 'car';
protected $table = 'cars';
};

$eloquent->save();
Expand All @@ -230,14 +230,14 @@ public function it_can_update_data()
{
$sku = $this->addSku([
'attribute_code' => 'upc',
'entity_code' => 'product',
'entity_code' => 'car',
]);

$value = 'HGKHDGEYTT'. rand();

$eloquent = new class() extends \Eav\Model {
const ENTITY = 'product';
protected $table = 'products';
const ENTITY = 'car';
protected $table = 'cars';
};

$eloquent->save();
Expand Down Expand Up @@ -268,14 +268,14 @@ public function it_can_fetch_data()
{
$sku = $this->addSku([
'attribute_code' => 'upc',
'entity_code' => 'product',
'entity_code' => 'car',
]);

$value = 'HGKHDGEYTT'. rand();

$eloquent = new class() extends \Eav\Model {
const ENTITY = 'product';
protected $table = 'products';
const ENTITY = 'car';
protected $table = 'cars';
};

$eloquent->save();
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ public function it_can_detect_entity_table_name()
/** @test */
public function it_can_have_many_attributes()
{
$entity = Entity::findByCode('product');
$entity = Entity::findByCode('car');

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

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

$this->assertTrue($entity->attributeSet->isNotEmpty());
}
Expand Down
74 changes: 35 additions & 39 deletions tests/Feature/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
use Eav\AttributeGroup;
use Eav\EntityAttribute;

class Products extends \Eav\Model {
const ENTITY = 'product';
}

class QueryBuilderTest extends TestCase
{

Expand All @@ -27,57 +23,57 @@ public function it_must_entity_assigned()
/** @test */
public function it_can_create_entity()
{
$eloquent = $this->product();
$eloquent = $this->car();

$this->assertNotNull($eloquent->getKey());
}

/** @test */
public function it_can_update_entity()
{
$eloquent = $this->product();
$eloquent = $this->car();

$this->assertNotNull($eloquent->getKey());

$eloquent->name = 'Not a Flamethrower';

$eloquent->save();

$db = Products::select(['name'])->find($eloquent->getKey());
$db = Cars::select(['name'])->find($eloquent->getKey());

$this->assertEquals($db->name, 'Not a Flamethrower');
}

/** @test */
public function it_can_mass_update()
{
$eloquent = $this->product();
$eloquent = $this->car();

Products::create([
Cars::create([
'name' => 'Flamethrower',
'sku' => '1HJK92_2',
'description' => 'Not a Flamethrower'
]);

$this->assertNull($eloquent->search);

$p = Products::whereNullAttribute('search');
$p = Cars::whereNullAttribute('search');

$p->update(['search' => 1]);

$db = Products::select(['attr.*'])->find($eloquent->getKey());
$db = cars::select(['attr.*'])->find($eloquent->getKey());

$this->assertNotNull($db->search);
}

/** @test */
public function it_can_fetch_without_attributes()
{
$eloquent = $this->product();
$eloquent = $this->car();

$products = Products::all();
$cars = Cars::all();

$p = $products->first();
$p = $cars->first();

$this->assertNull($p->name);

Expand All @@ -87,11 +83,11 @@ public function it_can_fetch_without_attributes()
/** @test */
public function it_can_fetch_with_attributes()
{
$eloquent = $this->product();
$eloquent = $this->car();

$products = Products::all(['attr.*']);
$cars = Cars::all(['attr.*']);

$p = $products->first();
$p = $cars->first();

$this->assertNotNull($p->name);

Expand All @@ -101,11 +97,11 @@ public function it_can_fetch_with_attributes()
/** @test */
public function it_can_fetch_specific_attributes()
{
$eloquent = $this->product();
$eloquent = $this->car();

$products = Products::all(['name','description']);
$cars = Cars::all(['name','description']);

$p = $products->first();
$p = $cars->first();

$this->assertNull($p->sku);
$this->assertNull($p->getKey());
Expand All @@ -116,11 +112,11 @@ public function it_can_fetch_specific_attributes()
/** @test */
public function it_can_fetch_with_entity_table()
{
$eloquent = $this->product();
$eloquent = $this->car();

$products = Products::all(['*','name','description']);
$cars = Cars::all(['*','name','description']);

$p = $products->first();
$p = $cars->first();

$this->assertNull($p->sku);

Expand All @@ -131,11 +127,11 @@ public function it_can_fetch_with_entity_table()
/** @test */
public function it_can_fetch_with_specific_entity_table()
{
$eloquent = $this->product();
$eloquent = $this->car();

$products = Products::all(['id','name','description']);
$cars = Cars::all(['id','name','description']);

$p = $products->first();
$p = $cars->first();

$this->assertNull($p->sku);

Expand All @@ -146,12 +142,12 @@ public function it_can_fetch_with_specific_entity_table()
/** @test */
public function it_can_fetch_with_get()
{
$eloquent = $this->product();
$eloquent = $this->car();

$products = Products::whereAttribute('sku', '1HJK92')
$cars = Cars::whereAttribute('sku', '1HJK92')
->get(['name']);

$p = $products->first();
$p = $cars->first();

$this->assertNull($p->sku);
$this->assertNull($p->getKey());
Expand All @@ -162,13 +158,13 @@ public function it_can_fetch_with_get()
/** @test */
public function it_can_fetch_with_select()
{
$eloquent = $this->product();
$eloquent = $this->car();

$products = Products::whereAttribute('sku', '1HJK92')
$cars = Cars::whereAttribute('sku', '1HJK92')
->select(['attr.*'])
->get();

$p = $products->first();
$p = $cars->first();

$this->assertNotNull($p->sku);
$this->assertNotNull($p->getKey());
Expand All @@ -178,18 +174,18 @@ public function it_can_fetch_with_select()
/** @test */
public function it_can_join_an_attribute()
{
$products = Products::select('*');
$cars = Cars::select('*');

$attribute = \Eav\Attribute::findByCode('sku', 'product');
$attribute->setEntity($products->baseEntity());
$attribute->addAttributeJoin($products->getQuery(), 'left');
$attribute = \Eav\Attribute::findByCode('sku', 'car');
$attribute->setEntity($cars->baseEntity());
$attribute->addAttributeJoin($cars->getQuery(), 'left');

$this->assertNotNull($products->getQuery()->joins);
$this->assertNotNull($cars->getQuery()->joins);
}

private function product()
private function car()
{
return Products::create([
return Cars::create([
'name' => 'Flamethrower',
'sku' => '1HJK92',
'description' => 'Not a Flamethrower'
Expand Down
14 changes: 12 additions & 2 deletions tests/Feature/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use Illuminate\Database\Eloquent\Factory as ModelFactory;
use Illuminate\Foundation\Testing\RefreshDatabase;

class Cars extends \Eav\Model {
const ENTITY = 'car';
}

abstract class TestCase extends Testbench
{
use RefreshDatabase;
Expand All @@ -16,11 +20,17 @@ abstract class TestCase extends Testbench
*/
protected function setUp(): void
{
if (! $this->app) {
$this->refreshApplication();
}

$this->app->afterResolving('migrator', function ($migrator) {
$migrator->path(realpath(__DIR__ . '/../migrations'));
});

parent::setUp();

app()->make(ModelFactory::class)->load(__DIR__.'/../factories');

$this->artisan('migrate', ['--path' => __DIR__ . '/../migrations/']);
}

protected function addSku($override = null)
Expand Down
Loading

0 comments on commit d7b4e98

Please sign in to comment.