Skip to content

Commit

Permalink
Allow running a callback with a temporary scope
Browse files Browse the repository at this point in the history
Closes #368
  • Loading branch information
JosephSilber committed Dec 2, 2018
1 parent 25fa4de commit ebba511
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Contracts/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,42 @@ public function applyToRelationQuery($query, $table);
*/
public function applyToRelation(BelongsToMany $relation);

/**
* Get the current scope value.
*
* @return mixed
*/
public function get();

/**
* Get the additional attributes for pivot table records.
*
* @param string|null $authority
* @return array
*/
public function getAttachAttributes($authority = null);

/**
* Run the given callback with the given temporary scope.
*
* @param mixed $scope
* @param callable $callback
* @return mixed
*/
public function onceTo($scope, callable $callback);

/**
* Remove the scope completely.
*
* @return $this
*/
public function remove();

/**
* Run the given callback without the scope applied.
*
* @param callable $callback
* @return mixed
*/
public function removeOnce(callable $callback);
}
51 changes: 51 additions & 0 deletions src/Database/Scope/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ protected function applyToQuery($query, $table)
});
}

/**
* Get the current scope value.
*
* @return mixed
*/
public function get()
{
return $this->scope;
}

/**
* Get the additional attributes for pivot table records.
*
Expand All @@ -187,6 +197,47 @@ public function getAttachAttributes($authority = null)
return ['scope' => $this->scope];
}

/**
* Run the given callback with the given temporary scope.
*
* @param mixed $scope
* @param callable $callback
* @return mixed
*/
public function onceTo($scope, callable $callback)
{
$mainScope = $this->scope;

$this->scope = $scope;

$result = $callback();

$this->scope = $mainScope;

return $result;
}

/**
* Remove the scope completely.
*
* @return $this
*/
public function remove()
{
$this->scope = null;
}

/**
* Run the given callback without the scope applied.
*
* @param callable $callback
* @return mixed
*/
public function removeOnce(callable $callback)
{
return $this->onceTo(null, $callback);
}

/**
* Determine whether the given class name is the role model.
*
Expand Down
85 changes: 85 additions & 0 deletions tests/MultiTenancyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,33 @@ function tearDown()
parent::tearDown();
}

/**
* @test
*/
function can_set_and_get_the_current_scope()
{
$bouncer = $this->bouncer();

$this->assertNull($bouncer->scope()->get());

$bouncer->scope()->to(1);
$this->assertEquals(1, $bouncer->scope()->get());
}

/**
* @test
*/
function can_remove_the_current_scope()
{
$bouncer = $this->bouncer();

$bouncer->scope()->to(1);
$this->assertEquals(1, $bouncer->scope()->get());

$bouncer->scope()->remove();
$this->assertNull($bouncer->scope()->get());
}

/**
* @test
* @dataProvider bouncerProvider
Expand Down Expand Up @@ -282,6 +309,44 @@ function can_set_custom_scope($provider)

$this->assertTrue($bouncer->can('delete', User::class));
}

/**
* @test
*/
function can_set_the_scope_temporarily()
{
$bouncer = $this->bouncer();

$this->assertNull($bouncer->scope()->get());

$result = $bouncer->scope()->onceTo(1, function () use ($bouncer) {
$this->assertEquals(1, $bouncer->scope()->get());

return 'result';
});

$this->assertEquals('result', $result);
$this->assertNull($bouncer->scope()->get());
}

/**
* @test
*/
function can_remove_the_scope_temporarily()
{
$bouncer = $this->bouncer();

$bouncer->scope()->to(1);

$result = $bouncer->scope()->removeOnce(function () use ($bouncer) {
$this->assertEquals(null, $bouncer->scope()->get());

return 'result';
});

$this->assertEquals('result', $result);
$this->assertEquals(1, $bouncer->scope()->get());
}
}


Expand Down Expand Up @@ -318,8 +383,28 @@ public function applyToRelation(BelongsToMany $relation)
return $relation;
}

public function get()
{
return null;
}

public function getAttachAttributes($authority = null)
{
return [];
}

public function onceTo($scope, callable $callback)
{
//
}

public function remove()
{
//
}

public function removeOnce(callable $callback)
{
//
}
}

0 comments on commit ebba511

Please sign in to comment.