Skip to content

Commit 115ad67

Browse files
committed
Add assert false shortcut in column class
1 parent 41dec56 commit 115ad67

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

src/Database/Column.php

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function foreign($table, $column = 'id', $onUpdate = 'cascade', $onDelete
9191
}
9292

9393
$name = $this->getIndexName('foreign');
94-
$this->context->assertTrue($this->table->hasForeignKey($name), "The foreign key {$name} does not exist.");
94+
$this->assertTrue($this->table->hasForeignKey($name), "The foreign key {$name} does not exist.");
9595

9696
$key = $this->table->getForeignKey($name);
9797
$onUpdate && $this->context->assertEquals(strtoupper($onUpdate), $key->onUpdate());
@@ -154,9 +154,9 @@ public function exists()
154154
*/
155155
public function increments()
156156
{
157-
return $this->integer()
158-
->assertTrue($this->get('autoincrement'), "The column {$this->name} is not auto-incremented.")
159-
->primary();
157+
$message = "The column {$this->name} is not auto-incremented.";
158+
159+
return $this->integer()->assertTrue($this->get('autoincrement'), $message)->primary();
160160
}
161161

162162
/**
@@ -168,11 +168,11 @@ public function index()
168168
{
169169
$index = $this->getIndexName();
170170

171-
$this->context->assertTrue(
171+
$this->assertTrue(
172172
$this->table->hasIndex($index), "The {$this->name} column is not indexed."
173173
);
174174

175-
$this->context->assertTrue(
175+
$this->assertTrue(
176176
$this->table->getIndex($index)->isSimpleIndex(), "The {$this->name} column is not a simple index."
177177
);
178178
}
@@ -199,15 +199,11 @@ public function ofType($type)
199199
public function primary()
200200
{
201201
$key = $this->tableHasPrimary()->getPrimaryKey();
202+
$message = "The column {$this->name} is not a primary key.";
202203

203-
$this->context->assertTrue(
204-
in_array($this->name, $key->getColumns()), "The column {$this->name} is not a primary key."
205-
);
206-
207-
$this->context->assertTrue($key->isPrimary());
208-
$this->context->assertTrue($key->isUnique());
209-
210-
return $this;
204+
return $this->assertTrue(in_array($this->name, $key->getColumns()), $message)
205+
->assertTrue($key->isPrimary())
206+
->assertTrue($key->isUnique());
211207
}
212208

213209
/**
@@ -238,12 +234,10 @@ public function __call($method, $args)
238234
protected function assertNullable($negate = false)
239235
{
240236
if ($negate) {
241-
$this->context->assertTrue($this->get('notnull'), "The table column `{$this->name}` is nullable");
242-
} else {
243-
$this->context->assertFalse($this->get('notnull'), "The table column `{$this->name}` is not nullable");
237+
return $this->assertTrue($this->get('notnull'), "The table column `{$this->name}` is nullable");
244238
}
245239

246-
return $this;
240+
return $this->assertFalse($this->get('notnull'), "The table column `{$this->name}` is not nullable");
247241
}
248242

249243
/**
@@ -270,6 +264,20 @@ protected function assertColumn($method, $args)
270264
throw new \Exception("The database table column assertion {$method} does not exist.");
271265
}
272266

267+
/**
268+
* Assert a condition is false alias.
269+
*
270+
* @param bool $condition
271+
* @param string $message
272+
* @return $this
273+
*/
274+
protected function assertFalse($condition, $message)
275+
{
276+
$this->context->assertFalse($condition, $message);
277+
278+
return $this;
279+
}
280+
273281
/**
274282
* Assert a condition is true alias.
275283
*
@@ -294,7 +302,7 @@ protected function assertTrue($condition, $message)
294302
protected function assertUniqueIndex($key, $indexes)
295303
{
296304
$this->context->assertArrayHasKey($key, $indexes, "The {$this->name} column is not indexed.");
297-
$this->context->assertTrue($indexes[$key]->isUnique(), "The {$this->name} is not a unique index.");
305+
$this->assertTrue($indexes[$key]->isUnique(), "The {$this->name} is not a unique index.");
298306
}
299307

300308
/**
@@ -316,7 +324,7 @@ protected function getIndexName($suffix = 'index')
316324
*/
317325
protected function tableHasPrimary()
318326
{
319-
$this->context->assertTrue(
327+
$this->assertTrue(
320328
$this->table->hasPrimaryKey(), "The table {$this->table->getName()} does not have a primary key."
321329
);
322330

0 commit comments

Comments
 (0)