Skip to content

Commit

Permalink
Dev (#27)
Browse files Browse the repository at this point in the history
* refactor defaults for custom attributes

* add remove default test

* remove dead code

* fix relationship between inventory model and custom attribute value model and add test to confirm
  • Loading branch information
dvicklund authored Mar 13, 2022
1 parent adfa14e commit 22ef3b9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Models/CustomAttributeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CustomAttributeValue extends BaseModel
*/
public function inventories()
{
return $this->belongsTo(Inventory::class);
return $this->belongsTo(Inventory::class, 'inventory_id');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,6 @@ public function customAttributes()
*/
public function customAttributeValues()
{
return $this->hasMany(CustomAttributeValue::class, 'inventory_id');
return $this->hasMany(CustomAttributeValue::class, 'inventory_id');
}
}
33 changes: 33 additions & 0 deletions tests/CustomAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Stevebauman\Inventory\Tests;

use Stevebauman\Inventory\Models\Inventory;
use Stevebauman\Inventory\Models\CustomAttributeValue;

/**
* Custom Attribute Test
*/
Expand Down Expand Up @@ -384,6 +387,36 @@ public function testCanGetAllCustomAttributesForAnInventoryItem() {
$this->assertEquals(strtotime($allAttrVals['the_time_and_day_of_these_most_freshest_of_properties']), strtotime('3-1-2021'));
}

public function testCanQueryInventoryWithCustomAttributeValues() {
$item = $this->newInventory();
$attr = $item->addCustomAttribute('string', 'The newest property around');
$item->setCustomAttribute($attr, 'a value');

$q = Inventory::with('customAttributeValues')->where('inventories.id', $item->id);

$results = $q->get();

$first = $results->first();

$this->assertEquals($first->toArray()['custom_attribute_values'][0]['string_val'], 'a value');
}

public function testCanQueryCustomAttributeValuesWithInventory() {
$item = $this->newInventory();
$attr = $item->addCustomAttribute('string', 'Even newer than that');
$item->setCustomAttribute($attr, 'value again');

$q = CustomAttributeValue::with('inventories')->where('custom_attribute_values.custom_attribute_id', $attr->id);

$results = $q->get();

$first = $results->first();

// Note that since this is a many-to-one relationship (belongsTo), the
// 'inventories' key contains only the single model, as opposed to the previous
// test, where the 'custom_attribute_values' key contains many models.
$this->assertEquals($first->toArray()['inventories']['id'], $item->id);
}


/*
Expand Down

0 comments on commit 22ef3b9

Please sign in to comment.