Frederic G. MARAND 7 лет назад
Родитель
Сommit
dd00f9f963
4 измененных файлов с 65 добавлено и 2 удалено
  1. 18 1
      database/factories/ModelFactory.php
  2. 1 0
      phpunit.xml
  3. 13 0
      tests/Feature/ExampleTest.php
  4. 33 1
      tests/Unit/ExampleTest.php

+ 18 - 1
database/factories/ModelFactory.php

@@ -9,10 +9,17 @@
 | you a convenient way to create models for testing and seeding your
 | database. Just tell the factory how a default model should look.
 |
+| "A blueprint for an Eloquent model".
+|
 */
 
 /** @var \Illuminate\Database\Eloquent\Factory $factory */
-$factory->define(App\User::class, function (Faker\Generator $faker) {
+
+use App\Post;
+use App\User;
+use Faker\Generator;
+
+$factory->define(User::class, function (Generator $faker) {
     static $password;
 
     return [
@@ -22,3 +29,13 @@ $factory->define(App\User::class, function (Faker\Generator $faker) {
         'remember_token' => str_random(10),
     ];
 });
+
+$factory->define(Post::class, function (Generator $faker) {
+   return [
+       'user_id' => function () {
+            return factory(User::class)->create()->id;
+       },
+       'title' => $faker->sentence,
+       'body' => $faker->paragraph,
+   ];
+});

+ 1 - 0
phpunit.xml

@@ -25,6 +25,7 @@
     <php>
         <env name="APP_ENV" value="testing"/>
         <env name="CACHE_DRIVER" value="array"/>
+        <env name="DB_DATABASE" value="laravel_blog_testing" />
         <env name="SESSION_DRIVER" value="array"/>
         <env name="QUEUE_DRIVER" value="sync"/>
     </php>

+ 13 - 0
tests/Feature/ExampleTest.php

@@ -19,5 +19,18 @@ class ExampleTest extends TestCase
         $response = $this->get('/');
 
         $response->assertStatus(200);
+        $response->assertSee('The Bootstrap Blog');
+    }
+
+    /**
+     * A basic sad test example.
+     *
+     * @return void
+     */
+    public function testBasicSadTest()
+    {
+        $response = $this->get('/not-there');
+
+        $response->assertStatus(404);
     }
 }

+ 33 - 1
tests/Unit/ExampleTest.php

@@ -2,12 +2,16 @@
 
 namespace Tests\Unit;
 
+use App\Post;
+use Carbon\Carbon;
 use Tests\TestCase;
 use Illuminate\Foundation\Testing\DatabaseMigrations;
 use Illuminate\Foundation\Testing\DatabaseTransactions;
 
 class ExampleTest extends TestCase
 {
+    use DatabaseTransactions;
+
     /**
      * A basic test example.
      *
@@ -15,6 +19,34 @@ class ExampleTest extends TestCase
      */
     public function testBasicTest()
     {
-        $this->assertTrue(true);
+        // $this->assertTrue(true);
+
+        // Given
+        // - I have two post records in the database
+        // - They are posted a month apart
+        $firstPost = factory(Post::class)->create();
+        $secondPost = factory(Post::class)->create([
+            'created_at' => Carbon::now()->subMonth(),
+        ]);
+
+        // When I fetch the archives...
+        $posts = Post::archives();
+
+        // Then the response should be in the proper format.
+        $this->assertCount(2, $posts);
+
+        $this->assertSame([
+            [
+                'year' => (int) $firstPost->created_at->format('Y'),
+                'month' => $firstPost->created_at->format('F'),
+                'published' => 1,
+            ],
+            [
+                'year' => (int) $secondPost->created_at->format('Y'),
+                'month' => $secondPost->created_at->format('F'),
+                'published' => 1,
+            ],
+        ], $posts);
     }
+
 }