Commit 809c3a73 authored by Dmitry Borzyonok's avatar Dmitry Borzyonok

Add L5.5 support

parent c0cae3e1
......@@ -11,17 +11,18 @@
],
"license" : "MIT",
"require": {
"illuminate/support": "^5.1",
"illuminate/container": "^5.1",
"illuminate/database": "^5.1",
"illuminate/events": "^5.1",
"illuminate/support": "^5.5",
"illuminate/container": "^5.5",
"illuminate/database": "^5.5",
"illuminate/events": "^5.5",
"mongodb/mongodb": "^1.0.0"
},
"require-dev": {
"phpunit/phpunit": "^5.0|^6.0",
"phpunit/phpunit": "^6.0",
"orchestra/testbench": "^3.1",
"mockery/mockery": "^0.9",
"satooshi/php-coveralls": "^1.0"
"satooshi/php-coveralls": "^1.0",
"doctrine/dbal": "^2.6"
},
"autoload": {
"psr-0": {
......
......@@ -27,7 +27,7 @@ class DatabaseTokenRepository extends BaseDatabaseTokenRepository
$date = $token['created_at']->toDateTime();
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
$token['created_at'] = $date->format('Y-m-d H:i:s');
} elseif (is_array($token['created_at']) and isset($token['created_at']['date'])) {
} elseif (is_array($token['created_at']) && isset($token['created_at']['date'])) {
$date = new DateTime($token['created_at']['date'], new DateTimeZone(isset($token['created_at']['timezone']) ? $token['created_at']['timezone'] : 'UTC'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
$token['created_at'] = $date->format('Y-m-d H:i:s');
......
......@@ -3,6 +3,7 @@
namespace Jenssegers\Mongodb;
use Illuminate\Database\Connection as BaseConnection;
use Illuminate\Support\Arr;
use MongoDB\Client;
class Connection extends BaseConnection
......@@ -34,7 +35,7 @@ class Connection extends BaseConnection
$dsn = $this->getDsn($config);
// You can pass options directly to the MongoDB constructor
$options = array_get($config, 'options', []);
$options = Arr::get($config, 'options', []);
// Create the connection
$this->connection = $this->createConnection($dsn, $config, $options);
......
......@@ -162,10 +162,18 @@ class Builder extends EloquentBuilder
return $this->model->newFromBuilder((array) $results);
} // The result is a single object.
elseif (is_array($results) and array_key_exists('_id', $results)) {
elseif (is_array($results) && array_key_exists('_id', $results)) {
return $this->model->newFromBuilder((array) $results);
}
return $results;
}
/**
* @return \Illuminate\Database\ConnectionInterface
*/
public function getConnection()
{
return $this->query->getConnection();
}
}
......@@ -2,6 +2,7 @@
namespace Jenssegers\Mongodb\Eloquent;
use Illuminate\Support\Str;
use Jenssegers\Mongodb\Relations\EmbedsMany;
use Jenssegers\Mongodb\Relations\EmbedsOne;
......@@ -32,7 +33,7 @@ trait EmbedsRelations
}
if (is_null($foreignKey)) {
$foreignKey = snake_case(class_basename($this));
$foreignKey = Str::snake(class_basename($this));
}
$query = $this->newQuery();
......@@ -67,7 +68,7 @@ trait EmbedsRelations
}
if (is_null($foreignKey)) {
$foreignKey = snake_case(class_basename($this));
$foreignKey = Str::snake(class_basename($this));
}
$query = $this->newQuery();
......
......@@ -214,11 +214,20 @@ trait HybridRelations
* @param string $collection
* @param string $foreignKey
* @param string $otherKey
* @param string $parentKey
* @param string $relatedKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $relation = null)
{
public function belongsToMany(
$related,
$collection = null,
$foreignKey = null,
$otherKey = null,
$parentKey = null,
$relatedKey = null,
$relation = null
) {
// If no relationship name was passed, we will pull backtraces to get the
// name of the calling function. We will use that function name as the
// title of this relation since that is a great convention to apply.
......@@ -228,7 +237,15 @@ trait HybridRelations
// Check if it is a relation with an original model.
if (!is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation);
return parent::belongsToMany(
$related,
$collection,
$foreignKey,
$otherKey,
$parentKey,
$relatedKey,
$relation
);
}
// First, we'll need to determine the foreign key and "other key" for the
......@@ -252,7 +269,16 @@ trait HybridRelations
// appropriate query constraint and entirely manages the hydrations.
$query = $instance->newQuery();
return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation);
return new BelongsToMany(
$query,
$this,
$collection,
$foreignKey,
$otherKey,
$parentKey ?: $this->getKeyName(),
$relatedKey ?: $instance->getKeyName(),
$relation
);
}
/**
......
......@@ -6,6 +6,7 @@ use Carbon\Carbon;
use DateTime;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
use MongoDB\BSON\ObjectID;
......@@ -46,7 +47,7 @@ abstract class Model extends BaseModel
{
// If we don't have a value for 'id', we will use the Mongo '_id' value.
// This allows us to work with models in a more sql-like way.
if (!$value and array_key_exists('_id', $this->attributes)) {
if (!$value && array_key_exists('_id', $this->attributes)) {
$value = $this->attributes['_id'];
}
......@@ -131,12 +132,12 @@ abstract class Model extends BaseModel
}
// Dot notation support.
if (str_contains($key, '.') and array_has($this->attributes, $key)) {
if (Str::contains($key, '.') && Arr::has($this->attributes, $key)) {
return $this->getAttributeValue($key);
}
// This checks for embedded relation support.
if (method_exists($this, $key) and !method_exists(self::class, $key)) {
if (method_exists($this, $key) && !method_exists(self::class, $key)) {
return $this->getRelationValue($key);
}
......@@ -149,8 +150,8 @@ abstract class Model extends BaseModel
protected function getAttributeFromArray($key)
{
// Support keys in dot notation.
if (str_contains($key, '.')) {
return array_get($this->attributes, $key);
if (Str::contains($key, '.')) {
return Arr::get($this->attributes, $key);
}
return parent::getAttributeFromArray($key);
......@@ -162,17 +163,17 @@ abstract class Model extends BaseModel
public function setAttribute($key, $value)
{
// Convert _id to ObjectID.
if ($key == '_id' and is_string($value)) {
if ($key == '_id' && is_string($value)) {
$builder = $this->newBaseQueryBuilder();
$value = $builder->convertKey($value);
} // Support keys in dot notation.
elseif (str_contains($key, '.')) {
elseif (Str::contains($key, '.')) {
if (in_array($key, $this->getDates()) && $value) {
$value = $this->fromDateTime($value);
}
array_set($this->attributes, $key, $value);
Arr::set($this->attributes, $key, $value);
return;
}
......@@ -199,8 +200,8 @@ abstract class Model extends BaseModel
// Convert dot-notation dates.
foreach ($this->getDates() as $key) {
if (str_contains($key, '.') and array_has($attributes, $key)) {
array_set($attributes, $key, (string) $this->asDateTime(array_get($attributes, $key)));
if (Str::contains($key, '.') && Arr::has($attributes, $key)) {
Arr::set($attributes, $key, (string) $this->asDateTime(Arr::get($attributes, $key)));
}
}
......@@ -218,20 +219,36 @@ abstract class Model extends BaseModel
/**
* @inheritdoc
*/
protected function originalIsNumericallyEquivalent($key)
protected function originalIsEquivalent($key, $current)
{
$current = $this->attributes[$key];
$original = $this->original[$key];
if (!array_key_exists($key, $this->original)) {
return false;
}
$original = $this->getOriginal($key);
if ($current === $original) {
return true;
}
if (null === $current) {
return false;
}
// Date comparison.
if (in_array($key, $this->getDates())) {
if ($this->isDateAttribute($key)) {
$current = $current instanceof UTCDateTime ? $this->asDateTime($current) : $current;
$original = $original instanceof UTCDateTime ? $this->asDateTime($original) : $original;
return $current == $original;
}
return parent::originalIsNumericallyEquivalent($key);
if ($this->hasCast($key)) {
return $this->castAttribute($key, $current) ===
$this->castAttribute($key, $original);
}
return is_numeric($current) && is_numeric($original)
&& strcmp((string) $current, (string) $original) === 0;
}
/**
......@@ -242,9 +259,7 @@ abstract class Model extends BaseModel
*/
public function drop($columns)
{
if (!is_array($columns)) {
$columns = [$columns];
}
$columns = Arr::wrap($columns);
// Unset attributes
foreach ($columns as $column) {
......@@ -263,16 +278,14 @@ abstract class Model extends BaseModel
if ($parameters = func_get_args()) {
$unique = false;
if (count($parameters) == 3) {
if (count($parameters) === 3) {
list($column, $values, $unique) = $parameters;
} else {
list($column, $values) = $parameters;
}
// Do batch push by default.
if (!is_array($values)) {
$values = [$values];
}
$values = Arr::wrap($values);
$query = $this->setKeysForSaveQuery($this->newQuery());
......@@ -294,9 +307,7 @@ abstract class Model extends BaseModel
public function pull($column, $values)
{
// Do batch pull by default.
if (!is_array($values)) {
$values = [$values];
}
$values = Arr::wrap($values);
$query = $this->setKeysForSaveQuery($this->newQuery());
......@@ -318,11 +329,11 @@ abstract class Model extends BaseModel
foreach ($values as $value) {
// Don't add duplicate values when we only want unique values.
if ($unique and in_array($value, $current)) {
if ($unique && (!is_array($current) || in_array($value, $current))) {
continue;
}
array_push($current, $value);
$current[] = $value;
}
$this->attributes[$column] = $current;
......@@ -340,11 +351,13 @@ abstract class Model extends BaseModel
{
$current = $this->getAttributeFromArray($column) ?: [];
foreach ($values as $value) {
$keys = array_keys($current, $value);
if (is_array($current)) {
foreach ($values as $value) {
$keys = array_keys($current, $value);
foreach ($keys as $key) {
unset($current[$key]);
foreach ($keys as $key) {
unset($current[$key]);
}
}
}
......
......@@ -8,6 +8,7 @@ use Illuminate\Database\Query\Builder as BaseBuilder;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Jenssegers\Mongodb\Connection;
use MongoCollection;
use MongoDB\BSON\ObjectID;
......@@ -232,7 +233,7 @@ class Builder extends BaseBuilder
$wheres = $this->compileWheres();
// Use MongoDB's aggregation framework when using grouping or aggregation functions.
if ($this->groups or $this->aggregate or $this->paginating) {
if ($this->groups || $this->aggregate || $this->paginating) {
$group = [];
$unwinds = [];
......@@ -286,7 +287,7 @@ class Builder extends BaseBuilder
}
// The _id field is mandatory when using grouping.
if ($group and empty($group['_id'])) {
if ($group && empty($group['_id'])) {
$group['_id'] = null;
}
......@@ -563,7 +564,7 @@ class Builder extends BaseBuilder
public function update(array $values, array $options = [])
{
// Use $set as default operator.
if (!starts_with(key($values), '$')) {
if (!Str::startsWith(key($values), '$')) {
$values = ['$set' => $values];
}
......@@ -705,7 +706,7 @@ class Builder extends BaseBuilder
$operator = $unique ? '$addToSet' : '$push';
// Check if we are pushing multiple values.
$batch = (is_array($value) and array_keys($value) === range(0, count($value) - 1));
$batch = (is_array($value) && array_keys($value) === range(0, count($value) - 1));
if (is_array($column)) {
$query = [$operator => $column];
......@@ -728,7 +729,7 @@ class Builder extends BaseBuilder
public function pull($column, $value = null)
{
// Check if we passed an associative array.
$batch = (is_array($value) and array_keys($value) === range(0, count($value) - 1));
$batch = (is_array($value) && array_keys($value) === range(0, count($value) - 1));
// If we are pulling multiple values, we need to use $pullAll.
$operator = $batch ? '$pullAll' : '$pull';
......@@ -804,7 +805,7 @@ class Builder extends BaseBuilder
*/
public function convertKey($id)
{
if (is_string($id) and strlen($id) === 24 and ctype_xdigit($id)) {
if (is_string($id) && strlen($id) === 24 && ctype_xdigit($id)) {
return new ObjectID($id);
}
......@@ -822,7 +823,7 @@ class Builder extends BaseBuilder
if (func_num_args() == 3) {
$operator = &$params[1];
if (starts_with($operator, '$')) {
if (Str::startsWith($operator, '$')) {
$operator = substr($operator, 1);
}
}
......@@ -866,7 +867,7 @@ class Builder extends BaseBuilder
}
// Convert id's.
if (isset($where['column']) and ($where['column'] == '_id' or ends_with($where['column'], '._id'))) {
if (isset($where['column']) && ($where['column'] == '_id' || Str::endsWith($where['column'], '._id'))) {
// Multiple values.
if (isset($where['values'])) {
foreach ($where['values'] as &$value) {
......@@ -896,7 +897,7 @@ class Builder extends BaseBuilder
// The next item in a "chain" of wheres devices the boolean of the
// first item. So if we see that there are multiple wheres, we will
// use the operator of the next where.
if ($i == 0 and count($wheres) > 1 and $where['boolean'] == 'and') {
if ($i == 0 && count($wheres) > 1 && $where['boolean'] == 'and') {
$where['boolean'] = $wheres[$i + 1]['boolean'];
}
......@@ -938,10 +939,10 @@ class Builder extends BaseBuilder
$regex = preg_replace('#(^|[^\\\])%#', '$1.*', preg_quote($value));
// Convert like to regular expression.
if (!starts_with($value, '%')) {
if (!Str::startsWith($value, '%')) {
$regex = '^' . $regex;
}
if (!ends_with($value, '%')) {
if (!Str::endsWith($value, '%')) {
$regex = $regex . '$';
}
......@@ -958,12 +959,12 @@ class Builder extends BaseBuilder
// For inverse regexp operations, we can just use the $not operator
// and pass it a Regex instence.
if (starts_with($operator, 'not')) {
if (Str::startsWith($operator, 'not')) {
$operator = 'not';
}
}
if (!isset($operator) or $operator == '=') {
if (!isset($operator) || $operator == '=') {
$query = [$column => $value];
} elseif (array_key_exists($operator, $this->conversion)) {
$query = [$column => [$this->conversion[$operator] => $value]];
......
......@@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany;
use Illuminate\Support\Arr;
class BelongsToMany extends EloquentBelongsToMany
{
......@@ -93,7 +94,7 @@ class BelongsToMany extends EloquentBelongsToMany
/**
* @inheritdoc
*/
public function create(array $attributes, array $joining = [], $touch = true)
public function create(array $attributes = [], array $joining = [], $touch = true)
{
$instance = $this->related->newInstance($attributes);
......@@ -134,6 +135,8 @@ class BelongsToMany extends EloquentBelongsToMany
$records = $this->formatSyncList($ids);
$current = Arr::wrap($current);
$detach = array_diff($current, array_keys($records));
// We need to make sure we pass a clean array, so that it is not interpreted
......@@ -143,7 +146,7 @@ class BelongsToMany extends EloquentBelongsToMany
// Next, we will take the differences of the currents and given IDs and detach
// all of the entities that exist in the "current" array but are not in the
// the array of the IDs given to the method which will complete the sync.
if ($detaching and count($detach) > 0) {
if ($detaching && count($detach) > 0) {
$this->detach($detach);
$changes['detached'] = (array) array_map(function ($v) {
......@@ -184,7 +187,7 @@ class BelongsToMany extends EloquentBelongsToMany
$id = $model->getKey();
// Attach the new parent id to the related model.
$model->push($this->foreignKey, $this->parent->getKey(), true);
$model->push($this->foreignPivotKey, $this->parent->getKey(), true);
} else {
if ($id instanceof Collection) {
$id = $id->modelKeys();
......@@ -195,7 +198,7 @@ class BelongsToMany extends EloquentBelongsToMany
$query->whereIn($this->related->getKeyName(), (array) $id);
// Attach the new parent id to the related model.
$query->push($this->foreignKey, $this->parent->getKey(), true);
$query->push($this->foreignPivotKey, $this->parent->getKey(), true);
}
// Attach the new ids to the parent model.
......@@ -231,7 +234,7 @@ class BelongsToMany extends EloquentBelongsToMany
}
// Remove the relation to the parent.
$query->pull($this->foreignKey, $this->parent->getKey());
$query->pull($this->foreignPivotKey, $this->parent->getKey());
if ($touch) {
$this->touchIfTouching();
......@@ -245,7 +248,7 @@ class BelongsToMany extends EloquentBelongsToMany
*/
protected function buildDictionary(Collection $results)
{
$foreign = $this->foreignKey;
$foreign = $this->foreignPivotKey;
// First we will build a dictionary of child models keyed by the foreign key
// of the relation so that we will easily and quickly match them to their
......@@ -286,15 +289,23 @@ class BelongsToMany extends EloquentBelongsToMany
*/
public function getForeignKey()
{
return $this->foreignKey;
return $this->foreignPivotKey;
}
/**
* @inheritdoc
*/
public function getQualifiedForeignPivotKeyName()
{
return $this->foreignPivotKey;
}
/**
* @inheritdoc
*/
public function getQualifiedForeignKeyName()
public function getQualifiedRelatedPivotKeyName()
{
return $this->foreignKey;
return $this->relatedPivotKey;
}
/**
......@@ -324,6 +335,6 @@ class BelongsToMany extends EloquentBelongsToMany
*/
public function getRelatedKey()
{
return property_exists($this, 'relatedKey') ? $this->relatedKey : $this->otherKey;
return property_exists($this, 'relatedPivotKey') ? $this->relatedPivotKey : $this->relatedKey;
}
}
......@@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Arr;
use MongoDB\BSON\ObjectID;
class EmbedsMany extends EmbedsOneOrMany
......@@ -39,7 +40,7 @@ class EmbedsMany extends EmbedsOneOrMany
public function performInsert(Model $model)
{
// Generate a new key if needed.
if ($model->getKeyName() == '_id' and !$model->getKey()) {
if ($model->getKeyName() == '_id' && !$model->getKey()) {
$model->setAttribute('_id', new ObjectID);
}
......@@ -79,7 +80,7 @@ class EmbedsMany extends EmbedsOneOrMany
$foreignKey = $this->getForeignKeyValue($model);
// Use array dot notation for better update behavior.
$values = array_dot($model->getDirty(), $this->localKey . '.$.');
$values = Arr::dot($model->getDirty(), $this->localKey . '.$.');
// Update document in database.
$result = $this->getBaseQuery()->where($this->localKey . '.' . $model->getKeyName(), $foreignKey)
......
......@@ -3,6 +3,7 @@
namespace Jenssegers\Mongodb\Relations;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use MongoDB\BSON\ObjectID;
class EmbedsOne extends EmbedsOneOrMany
......@@ -36,7 +37,7 @@ class EmbedsOne extends EmbedsOneOrMany
public function performInsert(Model $model)
{
// Generate a new key if needed.
if ($model->getKeyName() == '_id' and !$model->getKey()) {
if ($model->getKeyName() == '_id' && !$model->getKey()) {
$model->setAttribute('_id', new ObjectID);
}
......@@ -71,7 +72,7 @@ class EmbedsOne extends EmbedsOneOrMany
}
// Use array dot notation for better update behavior.
$values = array_dot($model->getDirty(), $this->localKey . '.');
$values = Arr::dot($model->getDirty(), $this->localKey . '.');
$result = $this->getBaseQuery()->update($values);
......
......@@ -94,9 +94,11 @@ abstract class EmbedsOneOrMany extends Relation
/**
* Shorthand to get the results of the relationship.
*
* @param array $columns
*
* @return Collection
*/
public function get()
public function get($columns = ['*'])
{
return $this->getResults();
}
......@@ -280,7 +282,12 @@ abstract class EmbedsOneOrMany extends Relation
return;
}
$model = $this->related->newFromBuilder((array) $attributes);
$connection = $this->related->getConnection();
$model = $this->related->newFromBuilder(
(array) $attributes,
$connection ? $connection->getName() : null
);
$model->setParentRelation($this);
......
......@@ -156,7 +156,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
*/
public function geospatial($columns = null, $index = '2d', $options = [])
{
if ($index == '2d' or $index == '2dsphere') {
if ($index == '2d' || $index == '2dsphere') {
$columns = $this->fluent($columns);
$columns = array_flip($columns);
......
......@@ -20,14 +20,7 @@ class EmbeddedRelationsTest extends TestCase
$user = User::create(['name' => 'John Doe']);
$address = new Address(['city' => 'London']);
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
$events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true);
$events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($address), $address)->andReturn(true);
$events->shouldReceive('fire')->once()->with('eloquent.created: ' . get_class($address), $address);
$events->shouldReceive('fire')->once()->with('eloquent.saved: ' . get_class($address), $address);
$address = $user->addresses()->save($address);
$address->unsetEventDispatcher();
$this->assertNotNull($user->addresses);
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $user->addresses);
......@@ -45,15 +38,8 @@ class EmbeddedRelationsTest extends TestCase
$user = User::find($user->_id);
$this->assertEquals(['London', 'Paris'], $user->addresses->pluck('city')->all());
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
$events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true);
$events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($address), $address)->andReturn(true);
$events->shouldReceive('fire')->once()->with('eloquent.updated: ' . get_class($address), $address);
$events->shouldReceive('fire')->once()->with('eloquent.saved: ' . get_class($address), $address);
$address->city = 'New York';
$user->addresses()->save($address);
$address->unsetEventDispatcher();
$this->assertEquals(2, count($user->addresses));
$this->assertEquals(2, count($user->addresses()->get()));
......@@ -210,15 +196,9 @@ class EmbeddedRelationsTest extends TestCase
$address = $user->addresses->first();
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
$events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::type('Address'))->andReturn(true);
$events->shouldReceive('fire')->once()->with('eloquent.deleted: ' . get_class($address), Mockery::type('Address'));
$user->addresses()->destroy($address->_id);
$this->assertEquals(['Bristol', 'Bruxelles'], $user->addresses->pluck('city')->all());
$address->unsetEventDispatcher();
$address = $user->addresses->first();
$user->addresses()->destroy($address);
$this->assertEquals(['Bruxelles'], $user->addresses->pluck('city')->all());
......@@ -248,17 +228,11 @@ class EmbeddedRelationsTest extends TestCase
$address = $user->addresses->first();
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
$events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::type('Address'))->andReturn(true);
$events->shouldReceive('fire')->once()->with('eloquent.deleted: ' . get_class($address), Mockery::type('Address'));
$address->delete();
$this->assertEquals(2, $user->addresses()->count());
$this->assertEquals(2, $user->addresses->count());
$address->unsetEventDispatcher();
$address = $user->addresses->first();
$address->delete();
......@@ -296,12 +270,11 @@ class EmbeddedRelationsTest extends TestCase
$user = User::create(['name' => 'John Doe']);
$address = new Address(['city' => 'London']);
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
$events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true);
$events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($address), $address)->andReturn(false);
$address::creating(function () {
return false;
});
$this->assertFalse($user->addresses()->save($address));
$address->unsetEventDispatcher();
}
public function testEmbedsManySavingEventReturnsFalse()
......@@ -310,11 +283,11 @@ class EmbeddedRelationsTest extends TestCase
$address = new Address(['city' => 'Paris']);
$address->exists = true;
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
$events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(false);
$address::saving(function () {
return false;
});
$this->assertFalse($user->addresses()->save($address));
$address->unsetEventDispatcher();
}
public function testEmbedsManyUpdatingEventReturnsFalse()
......@@ -323,14 +296,13 @@ class EmbeddedRelationsTest extends TestCase
$address = new Address(['city' => 'New York']);
$user->addresses()->save($address);
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
$events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true);
$events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($address), $address)->andReturn(false);
$address::updating(function () {
return false;
});
$address->city = 'Warsaw';
$this->assertFalse($user->addresses()->save($address));
$address->unsetEventDispatcher();
}
public function testEmbedsManyDeletingEventReturnsFalse()
......@@ -340,13 +312,12 @@ class EmbeddedRelationsTest extends TestCase
$address = $user->addresses->first();
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
$events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::mustBe($address))->andReturn(false);
$address::deleting(function () {
return false;
});
$this->assertEquals(0, $user->addresses()->destroy($address));
$this->assertEquals(['New York'], $user->addresses->pluck('city')->all());
$address->unsetEventDispatcher();
}
public function testEmbedsManyFindOrContains()
......@@ -443,14 +414,7 @@ class EmbeddedRelationsTest extends TestCase
$user = User::create(['name' => 'John Doe']);
$father = new User(['name' => 'Mark Doe']);
$father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
$events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true);
$events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($father), $father)->andReturn(true);
$events->shouldReceive('fire')->once()->with('eloquent.created: ' . get_class($father), $father);
$events->shouldReceive('fire')->once()->with('eloquent.saved: ' . get_class($father), $father);
$father = $user->father()->save($father);
$father->unsetEventDispatcher();
$this->assertNotNull($user->father);
$this->assertEquals('Mark Doe', $user->father->name);
......@@ -462,29 +426,15 @@ class EmbeddedRelationsTest extends TestCase
$raw = $father->getAttributes();
$this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']);
$father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
$events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true);
$events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($father), $father)->andReturn(true);
$events->shouldReceive('fire')->once()->with('eloquent.updated: ' . get_class($father), $father);
$events->shouldReceive('fire')->once()->with('eloquent.saved: ' . get_class($father), $father);
$father->name = 'Tom Doe';
$user->father()->save($father);
$father->unsetEventDispatcher();
$this->assertNotNull($user->father);
$this->assertEquals('Tom Doe', $user->father->name);
$father = new User(['name' => 'Jim Doe']);
$father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
$events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true);
$events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($father), $father)->andReturn(true);
$events->shouldReceive('fire')->once()->with('eloquent.created: ' . get_class($father), $father);
$events->shouldReceive('fire')->once()->with('eloquent.saved: ' . get_class($father), $father);
$father = $user->father()->save($father);
$father->unsetEventDispatcher();
$this->assertNotNull($user->father);
$this->assertEquals('Jim Doe', $user->father->name);
......@@ -495,11 +445,7 @@ class EmbeddedRelationsTest extends TestCase
$user = User::create(['name' => 'John Doe']);
$father = new User(['name' => 'Mark Doe']);
$father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
$events->shouldReceive('until')->times(0)->with('eloquent.saving: ' . get_class($father), $father);
$father = $user->father()->associate($father);
$father->unsetEventDispatcher();
$this->assertNotNull($user->father);
$this->assertEquals('Mark Doe', $user->father->name);
......
......@@ -4,6 +4,7 @@ use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Address extends Eloquent
{
protected $connection = 'mongodb';
protected static $unguarded = true;
public function addresses()
......
......@@ -4,6 +4,7 @@ use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Book extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'books';
protected static $unguarded = true;
protected $primaryKey = 'title';
......
......@@ -4,6 +4,7 @@ use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Client extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'clients';
protected static $unguarded = true;
......
......@@ -4,11 +4,12 @@ use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Group extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'groups';
protected static $unguarded = true;
public function users()
{
return $this->belongsToMany('User', null, 'groups', 'users');
return $this->belongsToMany('User', 'users', 'groups', 'users', '_id', '_id', 'users');
}
}
......@@ -4,6 +4,7 @@ use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Item extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'items';
protected static $unguarded = true;
......
......@@ -4,6 +4,7 @@ use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Location extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'locations';
protected static $unguarded = true;
}
......@@ -4,6 +4,7 @@ use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Photo extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'photos';
protected static $unguarded = true;
......
......@@ -4,6 +4,7 @@ use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Role extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'roles';
protected static $unguarded = true;
......
......@@ -7,6 +7,7 @@ class Soft extends Eloquent
{
use SoftDeletes;
protected $connection = 'mongodb';
protected $collection = 'soft';
protected static $unguarded = true;
protected $dates = ['deleted_at'];
......
......@@ -10,6 +10,7 @@ class User extends Eloquent implements AuthenticatableContract, CanResetPassword
{
use Authenticatable, CanResetPassword;
protected $connection = 'mongodb';
protected $dates = ['birthday', 'entry.date'];
protected static $unguarded = true;
......@@ -45,7 +46,7 @@ class User extends Eloquent implements AuthenticatableContract, CanResetPassword
public function groups()
{
return $this->belongsToMany('Group', null, 'users', 'groups');
return $this->belongsToMany('Group', 'groups', 'users', 'groups', '_id', '_id', 'groups');
}
public function photos()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment