|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Api; |
| 4 | + |
| 5 | +use Tests\TestCase; |
| 6 | +use Illuminate\Foundation\Testing\DatabaseMigrations; |
| 7 | + |
| 8 | +class ArticleFilterTest extends TestCase |
| 9 | +{ |
| 10 | + use DatabaseMigrations; |
| 11 | + |
| 12 | + /** @test */ |
| 13 | + public function it_returns_an_empty_array_of_articles_when_no_articles_exist_with_the_tag_or_an_invalid_tag() |
| 14 | + { |
| 15 | + $response = $this->getJson('/api/articles?tag=test'); |
| 16 | + |
| 17 | + $response->assertStatus(200) |
| 18 | + ->assertJson([ |
| 19 | + 'articles' => [], |
| 20 | + 'articlesCount' => 0 |
| 21 | + ]); |
| 22 | + |
| 23 | + $response = $this->getJson('/api/articles?tag=somerandomtag'); |
| 24 | + |
| 25 | + $response->assertStatus(200) |
| 26 | + ->assertJson([ |
| 27 | + 'articles' => [], |
| 28 | + 'articlesCount' => 0 |
| 29 | + ]); |
| 30 | + } |
| 31 | + |
| 32 | + /** @test */ |
| 33 | + public function it_returns_the_articles_with_the_tag_along_with_correct_total_article_count() |
| 34 | + { |
| 35 | + $tags = factory(\App\Tag::class)->times(2)->create(); |
| 36 | + |
| 37 | + $articles = $this->user->articles() |
| 38 | + ->saveMany(factory(\App\Article::class)->times(3)->make()) |
| 39 | + ->each(function ($article) use ($tags) { |
| 40 | + $article->tags()->attach($tags); |
| 41 | + }); |
| 42 | + |
| 43 | + $this->user->articles()->saveMany(factory(\App\Article::class)->times(5)->make()); |
| 44 | + |
| 45 | + $response = $this->getJson("/api/articles?tag={$tags[0]->name}"); |
| 46 | + |
| 47 | + $response->assertStatus(200) |
| 48 | + ->assertJson([ |
| 49 | + 'articles' => [ |
| 50 | + [ |
| 51 | + 'slug' => $articles[0]->slug, |
| 52 | + 'title' => $articles[0]->title, |
| 53 | + 'tagList' => $articles[0]->tagList, |
| 54 | + ], |
| 55 | + [ |
| 56 | + 'slug' => $articles[1]->slug, |
| 57 | + 'title' => $articles[1]->title, |
| 58 | + 'tagList' => $articles[1]->tagList, |
| 59 | + ], |
| 60 | + [ |
| 61 | + 'slug' => $articles[2]->slug, |
| 62 | + 'title' => $articles[2]->title, |
| 63 | + 'tagList' => $articles[2]->tagList, |
| 64 | + ], |
| 65 | + ], |
| 66 | + 'articlesCount' => 3 |
| 67 | + ]); |
| 68 | + } |
| 69 | + |
| 70 | + /** @test */ |
| 71 | + public function it_returns_an_empty_array_of_articles_when_no_articles_exist_by_the_author_or_invalid_author() |
| 72 | + { |
| 73 | + $response = $this->getJson('/api/articles?author=test'); |
| 74 | + |
| 75 | + $response->assertStatus(200) |
| 76 | + ->assertJson([ |
| 77 | + 'articles' => [], |
| 78 | + 'articlesCount' => 0 |
| 79 | + ]); |
| 80 | + |
| 81 | + $response = $this->getJson('/api/articles?author=somerandomtag'); |
| 82 | + |
| 83 | + $response->assertStatus(200) |
| 84 | + ->assertJson([ |
| 85 | + 'articles' => [], |
| 86 | + 'articlesCount' => 0 |
| 87 | + ]); |
| 88 | + } |
| 89 | + |
| 90 | + /** @test */ |
| 91 | + public function it_returns_the_articles_by_the_author_along_with_correct_total_article_count() |
| 92 | + { |
| 93 | + $articles = $this->user->articles()->saveMany(factory(\App\Article::class)->times(3)->make()); |
| 94 | + $this->loggedInUser->articles()->saveMany(factory(\App\Article::class)->times(5)->make()); |
| 95 | + |
| 96 | + $response = $this->getJson("/api/articles?author={$this->user->username}"); |
| 97 | + |
| 98 | + $response->assertStatus(200) |
| 99 | + ->assertJson([ |
| 100 | + 'articles' => [ |
| 101 | + [ |
| 102 | + 'slug' => $articles[0]->slug, |
| 103 | + 'title' => $articles[0]->title, |
| 104 | + 'author' => [ |
| 105 | + 'username' => $this->user->username |
| 106 | + ] |
| 107 | + ], |
| 108 | + [ |
| 109 | + 'slug' => $articles[1]->slug, |
| 110 | + 'title' => $articles[1]->title, |
| 111 | + 'author' => [ |
| 112 | + 'username' => $this->user->username |
| 113 | + ] |
| 114 | + ], |
| 115 | + [ |
| 116 | + 'slug' => $articles[2]->slug, |
| 117 | + 'title' => $articles[2]->title, |
| 118 | + 'author' => [ |
| 119 | + 'username' => $this->user->username |
| 120 | + ] |
| 121 | + ], |
| 122 | + ], |
| 123 | + 'articlesCount' => 3 |
| 124 | + ]); |
| 125 | + } |
| 126 | + |
| 127 | + /** @test */ |
| 128 | + public function it_returns_an_empty_array_of_articles_when_no_favorited_articles_exist_for_a_user_or_invalid_user() |
| 129 | + { |
| 130 | + $response = $this->getJson("/api/articles?favorited={$this->user->username}"); |
| 131 | + |
| 132 | + $response->assertStatus(200) |
| 133 | + ->assertJson([ |
| 134 | + 'articles' => [], |
| 135 | + 'articlesCount' => 0 |
| 136 | + ]); |
| 137 | + |
| 138 | + $response = $this->getJson('/api/articles?favorited=somerandomuser'); |
| 139 | + |
| 140 | + $response->assertStatus(200) |
| 141 | + ->assertJson([ |
| 142 | + 'articles' => [], |
| 143 | + 'articlesCount' => 0 |
| 144 | + ]); |
| 145 | + } |
| 146 | + |
| 147 | + /** @test */ |
| 148 | + public function it_returns_the_articles_favorited_by_the_user_along_with_correct_total_article_count() |
| 149 | + { |
| 150 | + $articles = $this->loggedInUser->articles()->saveMany(factory(\App\Article::class)->times(5)->make()); |
| 151 | + $this->user->favorite($articles[0]); |
| 152 | + $this->user->favorite($articles[2]); |
| 153 | + $this->user->favorite($articles[4]); |
| 154 | + |
| 155 | + $response = $this->getJson("/api/articles?favorited={$this->user->username}"); |
| 156 | + |
| 157 | + $response->assertStatus(200) |
| 158 | + ->assertJson([ |
| 159 | + 'articles' => [ |
| 160 | + [ |
| 161 | + 'slug' => $articles[0]->slug, |
| 162 | + 'title' => $articles[0]->title, |
| 163 | + 'author' => [ |
| 164 | + 'username' => $this->loggedInUser->username |
| 165 | + ] |
| 166 | + ], |
| 167 | + [ |
| 168 | + 'slug' => $articles[2]->slug, |
| 169 | + 'title' => $articles[2]->title, |
| 170 | + 'author' => [ |
| 171 | + 'username' => $this->loggedInUser->username |
| 172 | + ] |
| 173 | + ], |
| 174 | + [ |
| 175 | + 'slug' => $articles[4]->slug, |
| 176 | + 'title' => $articles[4]->title, |
| 177 | + 'author' => [ |
| 178 | + 'username' => $this->loggedInUser->username |
| 179 | + ] |
| 180 | + ], |
| 181 | + ], |
| 182 | + 'articlesCount' => 3 |
| 183 | + ]); |
| 184 | + } |
| 185 | +} |
0 commit comments