public function test_non_admin_cannot_remove_comment() { \Auth::shouldReceive('user')->once()->andReturn(DummyUser::where(['is_admin' => 0])->first()); $comment = Comment::where(['body' => 'My Comment 1'])->first(); $this->assertEquals($comment->body, 'My Comment 1'); $result = \Mural::remove($comment->id); $comment = Comment::where(['body' => 'My Comment 1'])->first(); $this->assertEquals($comment->body, 'My Comment 1'); $this->assertEquals($result, false); }
/** * Setup database untuk dummy data yang dibutuhkan untuk testing. * Method ini akan dijalankan setiap test case. * * File database.sqlite akan dinull kan sehingga test case akan memiliki fresh data setiap dijalankan. * * Penggunaan package Mural bergantung pada Model di Laravel apps seperti kewajiban implement interface di model atau * penggunaan trait. * Maka kita perlu membuat dummy model (DummyPost, DummyUser) agar dapat mensimulasikan situasi tersebut, beserta table-tablenya. * Disini dilakukan schema creation untuk dummy-dummy model tersebut. * * Package Mural membutuhkan table db tersendiri (table Comment), maka kita perlu memigratekannya juga. */ protected function setUpDatabase() { file_put_contents(__DIR__ . '/database.sqlite', null); $this->app['db']->connection()->getSchemaBuilder()->create('dummy_posts', function (Blueprint $table) { $table->increments('id'); $table->string('content'); $table->timestamps(); }); $this->app['db']->connection()->getSchemaBuilder()->create('dummy_users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->integer('is_admin'); $table->timestamps(); }); $this->artisan('migrate', ['--database' => 'sqlite', '--realpath' => realpath(__DIR__ . '/../database/migrations')]); $post = DummyPost::create(['content' => 'My first post']); $user = DummyUser::create(['name' => 'Heru', 'is_admin' => 0]); $userAdmin = DummyUser::create(['name' => 'Toni', 'is_admin' => 1]); for ($i = 1; $i <= 2; $i++) { Comment::create(['author_id' => $user->id, 'commentable_id' => $post->id, 'commentable_type' => DummyPost::class, 'body' => 'My Comment ' . $i, 'room' => 'test-room']); } }