Skip to content

Commit

Permalink
add strict option to hasColumns function
Browse files Browse the repository at this point in the history
  • Loading branch information
ariaieboy committed Feb 29, 2024
1 parent 698c74b commit d02efd6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Illuminate/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,15 @@ public function hasColumn($table, $column)
*
* @param string $table
* @param array $columns
* @param bool $strict
* @return bool
*/
public function hasColumns($table, array $columns)
public function hasColumns($table, array $columns, bool $strict = false)
{
if ($strict){
return $this->getColumnListing($table) === $columns;
}

$tableColumns = array_map('strtolower', $this->getColumnListing($table));

foreach ($columns as $column) {
Expand Down
7 changes: 6 additions & 1 deletion tests/Database/DatabaseSchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,15 @@ public function testTableHasColumns()
$grammar = m::mock(stdClass::class);
$connection->shouldReceive('getSchemaGrammar')->andReturn($grammar);
$builder = m::mock(Builder::class.'[getColumnListing]', [$connection]);
$builder->shouldReceive('getColumnListing')->with('users')->twice()->andReturn(['id', 'firstname']);
$builder->shouldReceive('getColumnListing')->with('users')->times(6)->andReturn(['id', 'firstname']);

$this->assertTrue($builder->hasColumns('users', ['id', 'firstname']));
$this->assertFalse($builder->hasColumns('users', ['id', 'address']));

$this->assertTrue($builder->hasColumns('users',['id','firstname'],true));
$this->assertFalse($builder->hasColumns('users',['id'],true));
$this->assertFalse($builder->hasColumns('users',['firstname','id'],true));
$this->assertFalse($builder->hasColumns('users',['id','FirstName'],true));
}

public function testGetColumnTypeAddsPrefix()
Expand Down

0 comments on commit d02efd6

Please sign in to comment.