Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Sep 21, 2017
1 parent 071d9fa commit 91212df
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 49 deletions.
18 changes: 9 additions & 9 deletions src/Illuminate/Contracts/Database/ModelIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,33 @@ class ModelIdentifier
public $id;

/**
* The connection name of the model.
* The relationships loaded on the model.
*
* @var string|null
* @var array
*/
public $connection;
public $relations;

/**
* The relationships loaded on the model.
* The connection name of the model.
*
* @var array
* @var string|null
*/
public $relations;
public $connection;

/**
* Create a new model identifier.
*
* @param string $class
* @param mixed $id
* @param mixed $connection
* @param array $relations
* @param mixed $connection
* @return void
*/
public function __construct($class, $id, $connection, $relations)
public function __construct($class, $id, array $relations, $connection)
{
$this->id = $id;
$this->class = $class;
$this->connection = $connection;
$this->relations = $relations;
$this->connection = $connection;
}
}
12 changes: 6 additions & 6 deletions src/Illuminate/Contracts/Queue/QueueableCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ public function getQueueableClass();
public function getQueueableIds();

/**
* Get the connection of the entities being queued.
* Get the relationships of the entities being queued.
*
* @return string|null
* @return array
*/
public function getQueueableConnection();
public function getQueueableRelations();

/**
* Get the relationships of the entities being queued.
* Get the connection of the entities being queued.
*
* @return array
* @return string|null
*/
public function getQueueableRelations();
public function getQueueableConnection();
}
12 changes: 6 additions & 6 deletions src/Illuminate/Contracts/Queue/QueueableEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ interface QueueableEntity
public function getQueueableId();

/**
* Get the connection of the entity.
* Get the relationships for the entity.
*
* @return string|null
* @return array
*/
public function getQueueableConnection();
public function getQueueableRelations();

/**
* Get the relationships for the entity.
* Get the connection of the entity.
*
* @return array
* @return string|null
*/
public function getQueueableRelations();
public function getQueueableConnection();
}
20 changes: 10 additions & 10 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,16 @@ public function getQueueableIds()
return $this->modelKeys();
}

/**
* Get the relationships of the entities being queued.
*
* @return array
*/
public function getQueueableRelations()
{
return $this->isNotEmpty() ? $this->first()->getRelations() : [];
}

/**
* Get the connection of the entities being queued.
*
Expand All @@ -420,14 +430,4 @@ public function getQueueableConnection()

return $connection;
}

/**
* Get the relationships of the entities being queued.
*
* @return array
*/
public function getQueueableRelations()
{
return $this->isNotEmpty() ? $this->first()->getRelations() : [];
}
}
22 changes: 11 additions & 11 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1237,16 +1237,6 @@ public function getQueueableId()
return $this->getKey();
}

/**
* Get the queueable connection for the entity.
*
* @return mixed
*/
public function getQueueableConnection()
{
return $this->getConnectionName();
}

/**
* Get the queueable relationships for the entity.
*
Expand All @@ -1272,7 +1262,17 @@ public function getQueueableRelations()
}
}

return $relations;
return array_unique($relations);
}

/**
* Get the queueable connection for the entity.
*
* @return mixed
*/
public function getQueueableConnection()
{
return $this->getConnectionName();
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ protected function getSerializedPropertyValue($value)
return new ModelIdentifier(
$value->getQueueableClass(),
$value->getQueueableIds(),
$value->getQueueableConnection(),
$value->getQueueableRelations()
$value->getQueueableRelations(),
$value->getQueueableConnection()
);
}

if ($value instanceof QueueableEntity) {
return new ModelIdentifier(
get_class($value),
$value->getQueueableId(),
$value->getQueueableConnection(),
$value->getQueueableRelations()
$value->getQueueableRelations(),
$value->getQueueableConnection()
);
}

Expand Down Expand Up @@ -79,7 +79,8 @@ protected function restoreCollection($value)
*/
public function restoreModel($value)
{
$model = $this->getQueryForModelRestoration((new $value->class)->setConnection($value->connection))
$model = $this->getQueryForModelRestoration((new $value->class)
->setConnection($value->connection))
->useWritePdo()->findOrFail($value->id);

return $model->load($value->relations);
Expand Down
9 changes: 7 additions & 2 deletions tests/Integration/Queue/ModelSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function it_reloads_relationships()
Line::create(['order_id' => $order->id, 'product_id' => $product1->id]);
Line::create(['order_id' => $order->id, 'product_id' => $product2->id]);

$order->load('lines');
$order->load('line', 'lines');

$serialized = serialize(new ModelRelationSerializationTestClass($order));
$unSerialized = unserialize($serialized);
Expand All @@ -175,7 +175,7 @@ public function it_reloads_nested_relationships()
Line::create(['order_id' => $order->id, 'product_id' => $product1->id]);
Line::create(['order_id' => $order->id, 'product_id' => $product2->id]);

$order->load('lines', 'lines.product');
$order->load('line.product', 'lines', 'lines.product');

$nestedSerialized = serialize(new ModelRelationSerializationTestClass($order));
$nestedUnSerialized = unserialize($nestedSerialized);
Expand All @@ -196,6 +196,11 @@ class Order extends Model
public $guarded = ['id'];
public $timestamps = false;

public function line()
{
return $this->hasOne(Line::class);
}

public function lines()
{
return $this->hasMany(Line::class);
Expand Down

0 comments on commit 91212df

Please sign in to comment.