<commit_before><?php
// @codingStandardsIgnoreFile

namespace Tests\Feature;

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;

class HandlesFriendshipsTest extends TestCase
{
    use DatabaseMigrations;

    public function setup()
    {
        parent::setup();

        $this->authenticate();
    }

    /** @test */
    function a_user_can_fetch_their_friendships()
    {
        $sender = create('Knot\Models\User');
        $sender->befriend(auth()->user());

        $response = $this->getJson('api/friendships')->assertStatus(200);
        $this->assertCount(1, $response->getOriginalContent()['requests']);
    }

    /** @test */
    function a_user_can_add_a_new_friend()
    {
        $recipient = create('Knot\Models\User');

        $response = $this->postJson('api/friendships/add/'.$recipient->id)->assertStatus(200);
        $this->assertTrue($recipient->hasFriendRequestFrom(auth()->user()));
        $this->assertCount(1, $recipient->notifications);
    }

    /** @test */
    function a_user_can_accept_a_friend_request()
    {
        $sender = create('Knot\Models\User');
        $sender->befriend(auth()->user());

        $this->postJson('api/friendships/accept/'.$sender->id)->assertStatus(200);
        $this->assertTrue(auth()->user()->isFriendWith($sender));
    }

    /** @test */
    function a_user_can_deny_a_friend_request()
    {
        $sender = create('Knot\Models\User');
        $sender->befriend(auth()->user());

        $this->postJson('api/friendships/deny/'.$sender->id)->assertStatus(200);
        $this->assertFalse(auth()->user()->isFriendWith($sender));
    }

    /** @test */
    function a_user_can_unfriend_a_friend()
    {
        $sender = create('Knot\Models\User');
        $this->createFriendship(auth()->user(), $sender);

        $this->assertTrue(auth()->user()->isFriendWith($sender));
        $this->postJson('api/friendships/unfriend/'.$sender->id)->assertStatus(200);
        $this->assertFalse(auth()->user()->isFriendWith($sender));
    }
}<commit_msg>Adds better notification tests by using a Notification fake<commit_after><?php
// @codingStandardsIgnoreFile

namespace Tests\Feature;

use Tests\TestCase;
use Knot\Notifications\AddedAsFriend;
use Knot\Notifications\FriendRequestAccepted;
use Illuminate\Support\Facades\Notification;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class HandlesFriendshipsTest extends TestCase
{
    use DatabaseMigrations;

    public function setup()
    {
        parent::setup();
        Notification::fake();
        $this->authenticate();
    }

    /** @test */
    function a_user_can_fetch_their_friendships()
    {
        $sender = create('Knot\Models\User');
        $sender->befriend(auth()->user());

        $response = $this->getJson('api/friendships')->assertStatus(200);
        $this->assertCount(1, $response->getOriginalContent()['requests']);
    }

    /** @test */
    function a_user_can_add_a_new_friend()
    {
        $recipient = create('Knot\Models\User');

        $response = $this->postJson('api/friendships/add/' . $recipient->id)->assertStatus(200);
        $this->assertTrue($recipient->hasFriendRequestFrom(auth()->user()));
        Notification::assertSentTo($recipient, AddedAsFriend::class);
    }

    /** @test */
    function a_user_can_accept_a_friend_request()
    {
        $sender = create('Knot\Models\User');
        $sender->befriend(auth()->user());

        $this->postJson('api/friendships/accept/' . $sender->id)->assertStatus(200);
        $this->assertTrue(auth()->user()->isFriendWith($sender));
        Notification::assertSentTo($sender, FriendRequestAccepted::class);
    }

    /** @test */
    function a_user_can_deny_a_friend_request()
    {
        $sender = create('Knot\Models\User');
        $sender->befriend(auth()->user());

        $this->postJson('api/friendships/deny/' . $sender->id)->assertStatus(200);
        $this->assertFalse(auth()->user()->isFriendWith($sender));
    }

    /** @test */
    function a_user_can_unfriend_a_friend()
    {
        $sender = create('Knot\Models\User');
        $this->createFriendship(auth()->user(), $sender);

        $this->assertTrue(auth()->user()->isFriendWith($sender));
        $this->postJson('api/friendships/unfriend/' . $sender->id)->assertStatus(200);
        $this->assertFalse(auth()->user()->isFriendWith($sender));
    }
}
{'fraction_non_alphanumeric': 0.1797676008202324, 'fraction_numerical': 0.007518796992481203, 'mean_word_length': 3.39, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2126573', 'n_tokens_mistral': 1367, 'n_tokens_neox': 1321, 'n_words': 151}