Skip to content

Commit

Permalink
Adding CompoundKeyTraitTest and removing unused functions from Compou…
Browse files Browse the repository at this point in the history
…ndKeyTrait.
  • Loading branch information
Jeremy Sik committed Oct 23, 2015
1 parent 9e0ceb7 commit 4c20c9b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 70 deletions.
71 changes: 1 addition & 70 deletions api/src/Model/Model/CompoundKeyTrait.php
Expand Up @@ -11,7 +11,6 @@
namespace Spira\Model\Model;

use LogicException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\Builder;

trait CompoundKeyTrait
Expand All @@ -38,64 +37,11 @@ protected static function bootUuidTrait()
*/
protected static function bootCompoundKeyTrait()
{
$vars = get_class_vars(__CLASS__);
if (! is_array($vars['primaryKey'])) {
if(! is_array(self::getPrimaryKey())) {
throw new LogicException('primaryKey must be an array');
}
}

/**
* Find model by compound key.
*
* @param array $compound
*
* @return BaseModel
*/
public function findByCompoundKey(array $compound)
{
return $this->findOrFail($compound);
}

/**
* Find a model by its compound key or throw an exception.
*
* @param array $compound
* @param array $columns
*
* @return BaseModel
*
* @throws ModelNotFoundException
*/
public function findOrFail($compound, $columns = ['*'])
{
$result = $this->find($compound, $columns);

if (! is_null($result)) {
return $result;
}

throw (new ModelNotFoundException)->setModel(get_class($this->model));
}

/**
* Find a model by its compound key.
*
* @param array $compound
* @param array $columns
*
* @return BaseModel|null
*/
public function find(array $compound, $columns = ['*'])
{
$query = $this->newQuery();

foreach ($compound as $column => $value) {
$query->where($this->getQualifiedColumnName($column), '=', $value);
}

return $query->first($columns);
}

/**
* Get an attribute from the model.
*
Expand All @@ -114,21 +60,6 @@ public function getAttribute($key)
return parent::getAttribute($key);
}

/**
* Insert the given attributes on the model.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array $attributes
*
* @return void
*/
protected function insertAndSetId(Builder $query, $attributes)
{
// With the case of a compount primary key, we do not have a specific id
// to set, so we override this method and leave out the part to set id.
$query->insert($attributes);
}

/**
* Get the table qualified column name.
*
Expand Down
42 changes: 42 additions & 0 deletions api/tests/Traits/CompoundKeyTraitTest.php
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Spira framework.
*
* @link https://github.com/spira/spira
*
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
*/
use App\Models\Localization;

/**
* Class CompoundKeyTraitTest.
*/
class CompoundKeyTraitTest extends TestCase
{
/**
* @expectedException LogicException
* @expectedExceptionCode 0
*/
public function testBootCompoundKeyTrait()
{

MockSinglePK::bootCompoundKeyTrait();

}

public function testGetQualifiedColumnName()
{
$localization = new Localization();

$qualifiedColumnName = $localization->getQualifiedColumnName('region_code');

$this->assertEquals($qualifiedColumnName, 'localizations.region_code');
}

}

class MockSinglePK extends Localization
{
protected $primaryKey = 'foo_id';
}

0 comments on commit 4c20c9b

Please sign in to comment.