Find this useful? Enter your email to receive occasional updates for securing PHP code.
Signing you up...
Thank you for signing up!
PHP Decode
<?php namespace Illuminate\Tests\Database; use Illuminate\Database\Capsule\Manager as DB..
Decoded Output download
<?php
namespace Illuminate\Tests\Database;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use PHPUnit\Framework\TestCase;
class DatabaseEloquentGlobalScopesTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
tap(new DB)->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
])->bootEloquent();
}
protected function tearDown(): void
{
parent::tearDown();
Model::unsetConnectionResolver();
}
public function testGlobalScopeIsApplied()
{
$model = new EloquentGlobalScopesTestModel;
$query = $model->newQuery();
$this->assertSame('select * from "table" where "active" = ?', $query->toSql());
$this->assertEquals([1], $query->getBindings());
}
public function testGlobalScopeCanBeRemoved()
{
$model = new EloquentGlobalScopesTestModel;
$query = $model->newQuery()->withoutGlobalScope(ActiveScope::class);
$this->assertSame('select * from "table"', $query->toSql());
$this->assertEquals([], $query->getBindings());
}
public function testClassNameGlobalScopeIsApplied()
{
$model = new EloquentClassNameGlobalScopesTestModel;
$query = $model->newQuery();
$this->assertSame('select * from "table" where "active" = ?', $query->toSql());
$this->assertEquals([1], $query->getBindings());
}
public function testGlobalScopeInAttributeIsApplied()
{
$model = new EloquentGlobalScopeInAttributeTestModel;
$query = $model->newQuery();
$this->assertSame('select * from "table" where "active" = ?', $query->toSql());
$this->assertEquals([1], $query->getBindings());
}
public function testClosureGlobalScopeIsApplied()
{
$model = new EloquentClosureGlobalScopesTestModel;
$query = $model->newQuery();
$this->assertSame('select * from "table" where "active" = ? order by "name" asc', $query->toSql());
$this->assertEquals([1], $query->getBindings());
}
public function testGlobalScopesCanBeRegisteredViaArray()
{
$model = new EloquentGlobalScopesArrayTestModel;
$query = $model->newQuery();
$this->assertSame('select * from "table" where "active" = ? order by "name" asc', $query->toSql());
$this->assertEquals([1], $query->getBindings());
}
public function testClosureGlobalScopeCanBeRemoved()
{
$model = new EloquentClosureGlobalScopesTestModel;
$query = $model->newQuery()->withoutGlobalScope('active_scope');
$this->assertSame('select * from "table" order by "name" asc', $query->toSql());
$this->assertEquals([], $query->getBindings());
}
public function testGlobalScopeCanBeRemovedAfterTheQueryIsExecuted()
{
$model = new EloquentClosureGlobalScopesTestModel;
$query = $model->newQuery();
$this->assertSame('select * from "table" where "active" = ? order by "name" asc', $query->toSql());
$this->assertEquals([1], $query->getBindings());
$query->withoutGlobalScope('active_scope');
$this->assertSame('select * from "table" order by "name" asc', $query->toSql());
$this->assertEquals([], $query->getBindings());
}
public function testAllGlobalScopesCanBeRemoved()
{
$model = new EloquentClosureGlobalScopesTestModel;
$query = $model->newQuery()->withoutGlobalScopes();
$this->assertSame('select * from "table"', $query->toSql());
$this->assertEquals([], $query->getBindings());
$query = EloquentClosureGlobalScopesTestModel::withoutGlobalScopes();
$this->assertSame('select * from "table"', $query->toSql());
$this->assertEquals([], $query->getBindings());
}
public function testGlobalScopesWithOrWhereConditionsAreNested()
{
$model = new EloquentClosureGlobalScopesWithOrTestModel;
$query = $model->newQuery();
$this->assertSame('select "email", "password" from "table" where ("email" = ? or "email" = ?) and "active" = ? order by "name" asc', $query->toSql());
$this->assertEquals(['[email protected]', '[email protected]', 1], $query->getBindings());
$query = $model->newQuery()->where('col1', 'val1')->orWhere('col2', 'val2');
$this->assertSame('select "email", "password" from "table" where ("col1" = ? or "col2" = ?) and ("email" = ? or "email" = ?) and "active" = ? order by "name" asc', $query->toSql());
$this->assertEquals(['val1', 'val2', '[email protected]', '[email protected]', 1], $query->getBindings());
}
public function testRegularScopesWithOrWhereConditionsAreNested()
{
$query = EloquentClosureGlobalScopesTestModel::withoutGlobalScopes()->where('foo', 'foo')->orWhere('bar', 'bar')->approved();
$this->assertSame('select * from "table" where ("foo" = ? or "bar" = ?) and ("approved" = ? or "should_approve" = ?)', $query->toSql());
$this->assertEquals(['foo', 'bar', 1, 0], $query->getBindings());
}
public function testScopesStartingWithOrBooleanArePreserved()
{
$query = EloquentClosureGlobalScopesTestModel::withoutGlobalScopes()->where('foo', 'foo')->orWhere('bar', 'bar')->orApproved();
$this->assertSame('select * from "table" where ("foo" = ? or "bar" = ?) or ("approved" = ? or "should_approve" = ?)', $query->toSql());
$this->assertEquals(['foo', 'bar', 1, 0], $query->getBindings());
}
public function testHasQueryWhereBothModelsHaveGlobalScopes()
{
$query = EloquentGlobalScopesWithRelationModel::has('related')->where('bar', 'baz');
$subQuery = 'select * from "table" where "table2"."id" = "table"."related_id" and "foo" = ? and "active" = ?';
$mainQuery = 'select * from "table2" where exists ('.$subQuery.') and "bar" = ? and "active" = ? order by "name" asc';
$this->assertEquals($mainQuery, $query->toSql());
$this->assertEquals(['bar', 1, 'baz', 1], $query->getBindings());
}
}
class EloquentClosureGlobalScopesTestModel extends Model
{
protected $table = 'table';
public static function boot()
{
static::addGlobalScope(function ($query) {
$query->orderBy('name');
});
static::addGlobalScope('active_scope', function ($query) {
$query->where('active', 1);
});
parent::boot();
}
public function scopeApproved($query)
{
return $query->where('approved', 1)->orWhere('should_approve', 0);
}
public function scopeOrApproved($query)
{
return $query->orWhere('approved', 1)->orWhere('should_approve', 0);
}
}
class EloquentGlobalScopesWithRelationModel extends EloquentClosureGlobalScopesTestModel
{
protected $table = 'table2';
public function related()
{
return $this->hasMany(EloquentGlobalScopesTestModel::class, 'related_id')->where('foo', 'bar');
}
}
class EloquentClosureGlobalScopesWithOrTestModel extends EloquentClosureGlobalScopesTestModel
{
public static function boot()
{
static::addGlobalScope('or_scope', function ($query) {
$query->where('email', '[email protected]')->orWhere('email', '[email protected]');
});
static::addGlobalScope(function ($query) {
$query->select('email', 'password');
});
parent::boot();
}
}
class EloquentGlobalScopesTestModel extends Model
{
protected $table = 'table';
public static function boot()
{
static::addGlobalScope(new ActiveScope);
parent::boot();
}
}
class EloquentClassNameGlobalScopesTestModel extends Model
{
protected $table = 'table';
public static function boot()
{
static::addGlobalScope(ActiveScope::class);
parent::boot();
}
}
class EloquentGlobalScopesArrayTestModel extends Model
{
protected $table = 'table';
public static function boot()
{
static::addGlobalScopes([
'active_scope' => new ActiveScope,
fn ($query) => $query->orderBy('name'),
]);
parent::boot();
}
}
#[ScopedBy(ActiveScope::class)]
class EloquentGlobalScopeInAttributeTestModel extends Model
{
protected $table = 'table';
}
class ActiveScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
return $builder->where('active', 1);
}
}
?>
Did this file decode correctly?
Original Code
<?php
namespace Illuminate\Tests\Database;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use PHPUnit\Framework\TestCase;
class DatabaseEloquentGlobalScopesTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
tap(new DB)->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
])->bootEloquent();
}
protected function tearDown(): void
{
parent::tearDown();
Model::unsetConnectionResolver();
}
public function testGlobalScopeIsApplied()
{
$model = new EloquentGlobalScopesTestModel;
$query = $model->newQuery();
$this->assertSame('select * from "table" where "active" = ?', $query->toSql());
$this->assertEquals([1], $query->getBindings());
}
public function testGlobalScopeCanBeRemoved()
{
$model = new EloquentGlobalScopesTestModel;
$query = $model->newQuery()->withoutGlobalScope(ActiveScope::class);
$this->assertSame('select * from "table"', $query->toSql());
$this->assertEquals([], $query->getBindings());
}
public function testClassNameGlobalScopeIsApplied()
{
$model = new EloquentClassNameGlobalScopesTestModel;
$query = $model->newQuery();
$this->assertSame('select * from "table" where "active" = ?', $query->toSql());
$this->assertEquals([1], $query->getBindings());
}
public function testGlobalScopeInAttributeIsApplied()
{
$model = new EloquentGlobalScopeInAttributeTestModel;
$query = $model->newQuery();
$this->assertSame('select * from "table" where "active" = ?', $query->toSql());
$this->assertEquals([1], $query->getBindings());
}
public function testClosureGlobalScopeIsApplied()
{
$model = new EloquentClosureGlobalScopesTestModel;
$query = $model->newQuery();
$this->assertSame('select * from "table" where "active" = ? order by "name" asc', $query->toSql());
$this->assertEquals([1], $query->getBindings());
}
public function testGlobalScopesCanBeRegisteredViaArray()
{
$model = new EloquentGlobalScopesArrayTestModel;
$query = $model->newQuery();
$this->assertSame('select * from "table" where "active" = ? order by "name" asc', $query->toSql());
$this->assertEquals([1], $query->getBindings());
}
public function testClosureGlobalScopeCanBeRemoved()
{
$model = new EloquentClosureGlobalScopesTestModel;
$query = $model->newQuery()->withoutGlobalScope('active_scope');
$this->assertSame('select * from "table" order by "name" asc', $query->toSql());
$this->assertEquals([], $query->getBindings());
}
public function testGlobalScopeCanBeRemovedAfterTheQueryIsExecuted()
{
$model = new EloquentClosureGlobalScopesTestModel;
$query = $model->newQuery();
$this->assertSame('select * from "table" where "active" = ? order by "name" asc', $query->toSql());
$this->assertEquals([1], $query->getBindings());
$query->withoutGlobalScope('active_scope');
$this->assertSame('select * from "table" order by "name" asc', $query->toSql());
$this->assertEquals([], $query->getBindings());
}
public function testAllGlobalScopesCanBeRemoved()
{
$model = new EloquentClosureGlobalScopesTestModel;
$query = $model->newQuery()->withoutGlobalScopes();
$this->assertSame('select * from "table"', $query->toSql());
$this->assertEquals([], $query->getBindings());
$query = EloquentClosureGlobalScopesTestModel::withoutGlobalScopes();
$this->assertSame('select * from "table"', $query->toSql());
$this->assertEquals([], $query->getBindings());
}
public function testGlobalScopesWithOrWhereConditionsAreNested()
{
$model = new EloquentClosureGlobalScopesWithOrTestModel;
$query = $model->newQuery();
$this->assertSame('select "email", "password" from "table" where ("email" = ? or "email" = ?) and "active" = ? order by "name" asc', $query->toSql());
$this->assertEquals(['[email protected]', '[email protected]', 1], $query->getBindings());
$query = $model->newQuery()->where('col1', 'val1')->orWhere('col2', 'val2');
$this->assertSame('select "email", "password" from "table" where ("col1" = ? or "col2" = ?) and ("email" = ? or "email" = ?) and "active" = ? order by "name" asc', $query->toSql());
$this->assertEquals(['val1', 'val2', '[email protected]', '[email protected]', 1], $query->getBindings());
}
public function testRegularScopesWithOrWhereConditionsAreNested()
{
$query = EloquentClosureGlobalScopesTestModel::withoutGlobalScopes()->where('foo', 'foo')->orWhere('bar', 'bar')->approved();
$this->assertSame('select * from "table" where ("foo" = ? or "bar" = ?) and ("approved" = ? or "should_approve" = ?)', $query->toSql());
$this->assertEquals(['foo', 'bar', 1, 0], $query->getBindings());
}
public function testScopesStartingWithOrBooleanArePreserved()
{
$query = EloquentClosureGlobalScopesTestModel::withoutGlobalScopes()->where('foo', 'foo')->orWhere('bar', 'bar')->orApproved();
$this->assertSame('select * from "table" where ("foo" = ? or "bar" = ?) or ("approved" = ? or "should_approve" = ?)', $query->toSql());
$this->assertEquals(['foo', 'bar', 1, 0], $query->getBindings());
}
public function testHasQueryWhereBothModelsHaveGlobalScopes()
{
$query = EloquentGlobalScopesWithRelationModel::has('related')->where('bar', 'baz');
$subQuery = 'select * from "table" where "table2"."id" = "table"."related_id" and "foo" = ? and "active" = ?';
$mainQuery = 'select * from "table2" where exists ('.$subQuery.') and "bar" = ? and "active" = ? order by "name" asc';
$this->assertEquals($mainQuery, $query->toSql());
$this->assertEquals(['bar', 1, 'baz', 1], $query->getBindings());
}
}
class EloquentClosureGlobalScopesTestModel extends Model
{
protected $table = 'table';
public static function boot()
{
static::addGlobalScope(function ($query) {
$query->orderBy('name');
});
static::addGlobalScope('active_scope', function ($query) {
$query->where('active', 1);
});
parent::boot();
}
public function scopeApproved($query)
{
return $query->where('approved', 1)->orWhere('should_approve', 0);
}
public function scopeOrApproved($query)
{
return $query->orWhere('approved', 1)->orWhere('should_approve', 0);
}
}
class EloquentGlobalScopesWithRelationModel extends EloquentClosureGlobalScopesTestModel
{
protected $table = 'table2';
public function related()
{
return $this->hasMany(EloquentGlobalScopesTestModel::class, 'related_id')->where('foo', 'bar');
}
}
class EloquentClosureGlobalScopesWithOrTestModel extends EloquentClosureGlobalScopesTestModel
{
public static function boot()
{
static::addGlobalScope('or_scope', function ($query) {
$query->where('email', '[email protected]')->orWhere('email', '[email protected]');
});
static::addGlobalScope(function ($query) {
$query->select('email', 'password');
});
parent::boot();
}
}
class EloquentGlobalScopesTestModel extends Model
{
protected $table = 'table';
public static function boot()
{
static::addGlobalScope(new ActiveScope);
parent::boot();
}
}
class EloquentClassNameGlobalScopesTestModel extends Model
{
protected $table = 'table';
public static function boot()
{
static::addGlobalScope(ActiveScope::class);
parent::boot();
}
}
class EloquentGlobalScopesArrayTestModel extends Model
{
protected $table = 'table';
public static function boot()
{
static::addGlobalScopes([
'active_scope' => new ActiveScope,
fn ($query) => $query->orderBy('name'),
]);
parent::boot();
}
}
#[ScopedBy(ActiveScope::class)]
class EloquentGlobalScopeInAttributeTestModel extends Model
{
protected $table = 'table';
}
class ActiveScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
return $builder->where('active', 1);
}
}
Function Calls
| None |
Stats
| MD5 | c8c9b7c73592d5338070817cfde51340 |
| Eval Count | 0 |
| Decode Time | 96 ms |