Skip to content

Commit

Permalink
Merge branch '5.6' of https://github.com/sikhlana/framework into sikh…
Browse files Browse the repository at this point in the history
…lana-5.6
  • Loading branch information
taylorotwell committed Jan 23, 2018
2 parents 98842da + fcbc5d6 commit 007d516
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class BelongsToMany extends Relation
*/
protected $pivotWhereIns = [];

/**
* Default values for the pivot columns.
*
* @var array
*/
protected $pivotValues = [];

/**
* Indicates if timestamps are available on the pivot table.
*
Expand Down Expand Up @@ -347,6 +354,32 @@ public function orWherePivot($column, $operator = null, $value = null)
return $this->wherePivot($column, $operator, $value, 'or');
}

/**
* Sets default value when querying or creating a new row in the pivot table.
*
* @param string $column
* @param mixed $value
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function withPivotValues($column, $value = null)
{
if (is_array($column)) {
foreach ($column as $name => $value) {
$this->withPivotValues($name, $value);
}

return $this;
}

if (is_null($value)) {
throw new \InvalidArgumentException('$value cannot be null.');
}

$this->pivotValues[] = func_get_args();

return $this->wherePivot($column, '=', $value);
}

/**
* Set an "or where in" clause for a pivot table column.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@ protected function baseAttachRecord($id, $timed)
$record = $this->addTimestampsToAttachment($record);
}

// Adding the default pivot values (if there is any) to the record.
foreach ($this->pivotValues as $arguments) {
list($name, $value) = $arguments;
$record[$name] = $value;
}

return $record;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Illuminate\Tests\Database;

use Mockery as m;
use PHPUnit\Framework\TestCase;

class DatabaseEloquentBelongsToManyWithDefaultAttributesTest extends TestCase
{
public function tearDown()
{
m::close();
}

public function testWithPivotValuesMethodSetsWhereConditionsForFetching()
{
$relation = $this->getMockBuilder('Illuminate\Database\Eloquent\Relations\BelongsToMany')->setMethods(['touchIfTouching'])->setConstructorArgs($this->getRelationArguments())->getMock();
$relation->withPivotValues(['is_admin' => 1]);
}

public function testWithPivotValuesMethodSetsDefaultArgumentsForInsertion()
{
$relation = $this->getMockBuilder('Illuminate\Database\Eloquent\Relations\BelongsToMany')->setMethods(['touchIfTouching'])->setConstructorArgs($this->getRelationArguments())->getMock();
$relation->withPivotValues(['is_admin' => 1]);

$query = m::mock('stdClass');
$query->shouldReceive('from')->once()->with('club_user')->andReturn($query);
$query->shouldReceive('insert')->once()->with([['club_id' => 1, 'user_id' => 1, 'is_admin' => 1]])->andReturn(true);
$relation->getQuery()->shouldReceive('getQuery')->andReturn($mockQueryBuilder = m::mock('stdClass'));
$mockQueryBuilder->shouldReceive('newQuery')->once()->andReturn($query);

$relation->attach(1);
}

public function getRelationArguments()
{
$parent = m::mock('Illuminate\Database\Eloquent\Model');
$parent->shouldReceive('getKey')->andReturn(1);
$parent->shouldReceive('getCreatedAtColumn')->andReturn('created_at');
$parent->shouldReceive('getUpdatedAtColumn')->andReturn('updated_at');
$parent->shouldReceive('getAttribute')->with('id')->andReturn(1);

$builder = m::mock('Illuminate\Database\Eloquent\Builder');
$related = m::mock('Illuminate\Database\Eloquent\Model');
$builder->shouldReceive('getModel')->andReturn($related);

$related->shouldReceive('getTable')->andReturn('users');
$related->shouldReceive('getKeyName')->andReturn('id');

$builder->shouldReceive('join')->once()->with('club_user', 'users.id', '=', 'club_user.user_id');
$builder->shouldReceive('where')->once()->with('club_user.club_id', '=', 1);
$builder->shouldReceive('where')->once()->with('club_user.is_admin', '=', 1, 'and');

return [$builder, $parent, 'club_user', 'club_id', 'user_id', 'id', 'id', null, false];
}
}

0 comments on commit 007d516

Please sign in to comment.