Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"php": ">=5.3.0"
},
"require-dev": {
"illuminate/support": "4.0.x",
"illuminate/support": "4.1.x",
"mockery/mockery": "*"
},
"autoload": {
Expand Down
10 changes: 10 additions & 0 deletions src/Way/Tests/DataStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ public function getText()
"a risus. Pellentesque et commodo lectus. In ac urna.";
}

/**
* Get some random Lorem text for Blob
*
* @return string
*/
public function getBlob()
{
return $this->getText();
}

/**
* Get some random Street name
*
Expand Down
81 changes: 37 additions & 44 deletions src/Way/Tests/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Factory {
*
* @var array
*/
protected static $columns;
protected $columns;

/**
* Whether models are being
Expand Down Expand Up @@ -112,6 +112,8 @@ public static function attributesFor($class, array $columns = array())
*/
public static function make($class, $columns = array())
{
static::$isSaving = false;

$instance = new static;

return $instance->fire($class, $columns);
Expand All @@ -138,37 +140,19 @@ protected static function setRootNamespace($class)
*/
public function fire($class, array $overrides = array())
{
$this->tableName = $this->parseTableName($class);
$this->class = $this->createModel($class);
$this->tableName = $this->class->getTable();

// First, we dynamically fetch the fields for the table
$columns = $this->getColumns($this->tableName);

// Then, we set dummy value on the model.
$this->setColumns($columns);

// Finally, if they specified any overrides, like
// Factory::make('Post', ['title' => null]),
// we'll make those take precedence.
$this->applyOverrides($overrides);
// Then, set column values.
$this->setColumns($columns, $overrides);

// And then return the new class
return $this->class;
}

/**
* Calulate the table name
*
* @param string $class
* @return string
*/
protected function parseTableName($class)
{
return $this->isNamespaced($class)
? snake_case(str_plural(substr(strrchr($class, '\\'), 1)))
: snake_case(str_plural($class));
}

/**
* Initialize the given model
*
Expand Down Expand Up @@ -197,21 +181,6 @@ protected function isNamespaced($class)
return str_contains($class, '\\');
}

/**
* If overrides are set, then
* override default values with them.
*
* @param array $overrides
* @return void
*/
protected function applyOverrides(array $overrides)
{
foreach ($overrides as $field => $value)
{
$this->class->$field = $value;
}
}

/**
* Fetch the table fields for the class.
*
Expand All @@ -223,45 +192,69 @@ public function getColumns($tableName)
// We only want to fetch the table details
// once. We'll store these fields with a
// $columns property for future fetching.
if (isset(static::$columns[$this->tableName]))
if (isset($this->columns[$this->tableName]))
{
return static::$columns[$this->tableName];
return $this->columns[$this->tableName];
}

// This will only run the first time the factory is created.
return static::$columns[$this->tableName] = $this->db->getDoctrineSchemaManager()->listTableDetails($tableName)->getColumns();
return $this->columns[$this->tableName] = $this->db->getDoctrineSchemaManager()->listTableDetails($tableName)->getColumns();
}

/**
* Set fields for object
*
* @param array $columns
*/
protected function setColumns(Array $columns)
protected function setColumns(Array $columns, Array $overrides)
{
$processed_keys = [];

//loop through table columns and fill with stub or override value
foreach($columns as $key => $col)
{
//create relationship
if ($relation = $this->hasForeignKey($key))
{
$this->class->$key = $this->createRelationship($relation);
$processed_keys[] = $key;
continue;
}
$this->class->$key = $this->setColumn($key, $col);
//fill with override
if (array_key_exists($key, $overrides))
{
$this->class->$key = $overrides[$key];
$processed_keys[] = $key;
continue;
}
//fill with stub
$this->class->$key = $this->setColumnWithStub($key, $col);
$processed_keys[] = $key;
}

//in case we don't have table columns (ie table wasnt migrated) at least ensure overrides are set
foreach ($overrides as $override_key => $override_value)
{
if (!in_array($override_key, $processed_keys))
{
$this->class->$override_key = $override_value;
}
}
}

/**
* Set single column
* Set single column with stub value
*
* @param string $name
* @param string $col
* @throws \Exception
*/
protected function setColumn($name, $col)
protected function setColumnWithStub($name, $col)
{
if ($name === 'id') return;

$method = $this->getFakeMethodName($name, $col);

if (method_exists($this->dataStore, $method))
{
return $this->dataStore->{$method}();
Expand Down
17 changes: 16 additions & 1 deletion src/Way/Tests/ModelHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ public function assertHasOne($relation, $class)
{
$this->assertRelationship($relation, $class, 'hasOne');
}

public function assertMorphOne($relation, $class)
{
$this->assertRelationship($relation, $class, 'morphOne');
}

public function assertMorphMany($relation, $class, $morphable)
public function assertMorphMany($relation, $class, $morphable = null)
{
$this->assertRelationship($relation, $class, 'morphMany');
}
Expand All @@ -50,6 +55,16 @@ public function assertMorphTo($relation, $class)
{
$this->assertRelationship($relation, $class, 'morphTo');
}

public function assertMorphToMany($relation, $class)
{
$this->assertRelationship($relation, $class, 'morphToMany');
}

public function assertMorphedByMany($relation, $class)
{
$this->assertRelationship($relation, $class, 'morphedByMany');
}

public function assertRespondsTo($method, $class, $message = null)
{
Expand Down