Skip to content

Instantly share code, notes, and snippets.

@stefanzweifel
Last active July 11, 2018 18:43
Show Gist options
  • Save stefanzweifel/85f56fe462ea81d817bea8dcf8cfa4ac to your computer and use it in GitHub Desktop.
Save stefanzweifel/85f56fe462ea81d817bea8dcf8cfa4ac to your computer and use it in GitHub Desktop.
Custom Laravel Assertion when you often have to assert against timestamps. Just add it to your `Tests/Testcase.php`
<?php
public function assertHasNotNullRecordForColumn($table, $column, $data = [])
{
$result = \Illuminate\Support\Facades\DB::table($table)
->whereNotNull($column)
->where($data)
->count() > 0;
$message = sprintf(
"Failed asserting that any row in the table [%s] matches the attributes and the column [%s] is NOT NULL.\n\n%s",
$table, $column, json_encode($data, JSON_PRETTY_PRINT)
);
return $this->assertTrue($result, $message);
}
<?php
/** @test */
public function it_activates_the_user()
{
$response = $this->post('activate/user/1');
$this->assertDatabaseHas('users', [
'email' => 'winston@overwatch.org',
'activated_at' => now()
])
}
Failed asserting that a row in the table [users] matches the attributes {
"email": "winston@overwatch.org"
"activated_at": {
"date": "2018-07-11 13:30:27.023369", // <<<
"timezone_type": 3,
"timezone": "Europe\/Zurich"
}
}.
Found: [
{
"id": "1",
"email": "winston@overwatch.org",
"activated_at": "2018-07-11 13:30:28", // <<<
"updated_at": "2018-07-11 13:30:28",
}
].
<?php
/** @test */
public function it_activates_the_user()
{
$response = $this->post('activate/user/1');
$this->assertHasNotNullRecordForColumn('users', 'activated_at', [
'email' => 'winston@overwatch.org'
])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment