Skip to content

Commit

Permalink
Refactor hasColumns method and test
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandrb committed Jun 10, 2024
1 parent 680433c commit ca0285f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
11 changes: 5 additions & 6 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ public function hasColumn($table, $column): bool
*/
public function hasColumns($table, array $columns): bool
{
foreach ($columns as $column) {
if (!$this->hasColumn($table, $column)) {
return false;
}
}
return true;
$collection = $this->connection->table($table);

return $collection->whereAll($columns, 'exists', true)
->project(['_id' => 1])
->exists();
}

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,10 @@ public function testHasColumn(): void

public function testHasColumns(): void
{
DB::connection()->collection('newcollection')->insert(['column1' => 'value']);
DB::connection()->collection('newcollection')->insert(['column2' => 'value']);
// Insert documents with both column1 and column2
DB::connection()->collection('newcollection')->insert([
['column1' => 'value1', 'column2' => 'value2']
]);

$this->assertTrue(Schema::hasColumns('newcollection', ['column1', 'column2']));
$this->assertFalse(Schema::hasColumns('newcollection', ['column1', 'column3']));
Expand Down

0 comments on commit ca0285f

Please sign in to comment.