Last active
July 11, 2018 18:43
-
-
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`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** @test */ | |
| public function it_activates_the_user() | |
| { | |
| $response = $this->post('activate/user/1'); | |
| $this->assertDatabaseHas('users', [ | |
| 'email' => '[email protected]', | |
| 'activated_at' => now() | |
| ]) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Failed asserting that a row in the table [users] matches the attributes { | |
| "email": "[email protected]" | |
| "activated_at": { | |
| "date": "2018-07-11 13:30:27.023369", // <<< | |
| "timezone_type": 3, | |
| "timezone": "Europe\/Zurich" | |
| } | |
| }. | |
| Found: [ | |
| { | |
| "id": "1", | |
| "email": "[email protected]", | |
| "activated_at": "2018-07-11 13:30:28", // <<< | |
| "updated_at": "2018-07-11 13:30:28", | |
| } | |
| ]. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** @test */ | |
| public function it_activates_the_user() | |
| { | |
| $response = $this->post('activate/user/1'); | |
| $this->assertHasNotNullRecordForColumn('users', 'activated_at', [ | |
| 'email' => '[email protected]' | |
| ]) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment