app->instance( PostServiceInterface::class, new PostService($this->app->get(PostFilter::class), $this->app->get(PostOrder::class)) ); } public function testApplicationWillReturnPostOnGet(): void { $post = Post::factory()->create(); $response = $this->get("/api/v1/post/" . $post->id); $response->assertJsonFragment( [ 'content' => $post->content, 'title' => $post->title, ] ); $response->assertStatus(200); } public function testApplicationWillAcceptFiltering(): void { Post::factory()->create(); $post = Post::factory()->create(); $response = $this->get("/api/v1/posts/1?title=" . $post->title); $response->assertJsonFragment( [ 'content' => $post->content, 'title' => $post->title, ] ); $this->assertEquals(1, count($response->decodeResponseJson()['items'])); } public function testApplicationWillNotReturnPostWithInvalidId(): void { $response = $this->get("/api/v1/post/-1"); $response->assertStatus(422); $response = $this->get("/api/v1/post/foo"); $response->assertStatus(422); $response = $this->get("/api/v1/post/0"); $response->assertStatus(404); } public function testApplicationWillCreatePostOnPost(): void { $category = Category::factory()->create(); $data = [ 'content' => 'This is a test post', 'title' => 'This is a test post', 'category_id' => $category->id, ]; $response = $this->post("/api/v1/posts", $data); $response->assertJsonFragment( [ 'content' => $data['content'], 'title' => $data['title'], ] ); $postDb = Post::find($response->getOriginalContent()->getAttributes()['id']); $this->assertEquals( [ 'content' => $data['content'], 'title' => $data['title'], 'category_id' => $data['category_id'], ], [ 'content' => $postDb->content, 'title' => $postDb->title, 'category_id' => $postDb->category_id, ] ); $this->assertEquals($category->id, $postDb->category_id); $response->assertStatus(201); } public function testApplicationWillUpdatePost(): void { $post = Post::factory()->create(); $data = [ 'content' => 'This is a test post', 'title' => 'This is a test post', ]; $response = $this->put("/api/v1/post/" . $post->id, $data); $response->assertJsonFragment( [ 'content' => $data['content'], 'title' => $data['title'], ] ); $postDb = Post::find($response->getOriginalContent()->getAttributes()['id']); $this->assertEquals( [ 'content' => $data['content'], 'title' => $data['title'], ], [ 'content' => $postDb->content, 'title' => $postDb->title, ] ); $response->assertStatus(200); } public function testApplicationWillDeletePost(): void { $post = Post::factory()->create(); $response = $this->delete("/api/v1/post/" . $post->id); $response->assertStatus(204); $postDb = Post::find($post->id); $this->assertNull($postDb); } }