NewBornManyCommentTest.php 1.94 KB
<?php

namespace FootyRoom\Tests;

use Illuminate\Database\Connection;

class NewBornManyCommentTest extends TestCase
{
    use SetsUpFixtures;

    public function setup()
    {
        parent::setup();
        $this->faker = \Faker\Factory::create();
        $this->mysql = $this->app->make(Connection::class);

        for ($i = 1; $i <= 5; $i++) {
            $comments[] = [
                'user_id' => 2,
                'content' => $this->faker->sentence(),
                'datetime' => $this->faker->date('Y-m-d H:i:s', '-1 week'),
            ];
        }

        $this->mysql->table('comments')->insert($comments);
    }

    public function testNewBornCanCreateCommentAfterTooManyCommentBeyond24Hours()
    {
        $data = [
            'postId' => 1,
            'content' => $this->faker->paragraph(),
        ];

        $user = factory('FootyRoom\User\User')->make(['user_id' => 2]);
        $response = $this->actingAs($user)->call('POST', '/forum/comments', $data);
        $this->assertEquals(200, $response->status());
    }

    public function testNewBornCanNotCreateCommentAfterTooManyCommentWithin24Hours()
    {
        for ($i = 1; $i <= 5; $i++) {
            $comments[] = [
                'discussion_id' => 'post:1',
                'user_id' => 2,
                'content' => $this->faker->sentence(),
                'datetime' => date('Y-m-d H:i:s'),
            ];
        }

        $this->mysql->table('comments')->insert($comments);

        $data = [
            'postId' => 1,
            'content' => $this->faker->paragraph(),
        ];

        $user = factory('FootyRoom\User\User')->make(['user_id' => 2]);
        $response = $this->actingAs($user)->call('POST', '/forum/comments', $data);
        $this->assertEquals(500, $response->status());
        $this->assertContains('New members can comment/post 5 times per day. You have already made 5 comments/posts today. Please wait until tomorrow.', $response->getContent());
    }
}