<commit_before><?php
namespace Tests\Services;

use Tests\TestCase;
use Domains\Library\Discourse\Api\DiscourseUserApi;
use Domains\Library\Discourse\Api\DiscourseAdminApi;
use Domains\Modules\Accounts\Models\Account;
use Domains\Services\Login\LogoutService;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Contracts\Auth\Guard as Auth;
use Illuminate\Log\Logger;

class LogoutService_Test extends TestCase
{
    use DatabaseMigrations, DatabaseTransactions;

    private $loggerStub;
    private $discourseUserApiMock;
    private $discourseAdminApiMock;

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

        $this->loggerStub = $this->getMockBuilder(Logger::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->discourseUserApiMock = $this->getMockBuilder(DiscourseUserApi::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->discourseAdminApiMock = $this->getMockBuilder(DiscourseAdminApi::class)
            ->disableOriginalConstructor()
            ->getMock();
    }

    /**
     * Creates a new Account and logins
     * as that account
     *
     * @param Auth $auth
     * @return void
     */
    private function loginAsUser(Auth $auth)
    {
        $account = factory(Account::class)->make();
        $auth->setUser($account);

        $this->assertTrue($auth->check());
    }

    public function testLogoutOfPcb_canLogout()
    {
        // given...
        $auth = resolve(Auth::class);
        $service = new LogoutService($this->discourseUserApiMock, 
                                     $this->discourseAdminApiMock, 
                                     $auth, 
                                     $this->loggerStub);
        $this->loginAsUser($auth);

        // when...
        $service->logoutOfPCB();

        // expect...
        $this->assertFalse($auth->check());
    }
    
    public function testLogoutOfPcb_notLoggedIn_doesNothing()
    {
        // given...
        $auth = resolve(Auth::class);
        $service = new LogoutService($this->discourseUserApiMock, 
                                     $this->discourseAdminApiMock, 
                                     $auth, 
                                     $this->loggerStub);

        $this->assertFalse($auth->check());

        // when...
        $result = $service->logoutOfPCB();

        // expect...
        $this->assertFalse($result);
    }

    // public function testLogoutOfBoth_canLogoutOfPcb()
    // {
    //     // given...
    //     $this->discourseUserApiMock
    //         ->expects($this->once())
    //         ->method('fetchUserByPcbId')
    //         ->willReturn([
    //             'user' => 15,
    //         ]);

    //     $auth = resolve(Auth::class);
    //     $service = new LogoutService($this->discourseUserApiMock, 
    //                                 $this->discourseAdminApiMock, 
    //                                 $auth, 
    //                                 $this->loggerStub);
    //     $this->loginAsUser($auth);

    //     // when...
    //     $service->logoutOfDiscourseAndPcb();

    //     // expect...
    //     $this->assertFalse($auth->check());
    // }
}<commit_msg>Update logout service test<commit_after><?php
namespace Tests\Services;

use Tests\TestCase;
use Domains\Library\Discourse\Api\DiscourseUserApi;
use Domains\Library\Discourse\Api\DiscourseAdminApi;
use Domains\Modules\Accounts\Models\Account;
use Domains\Services\Login\LogoutService;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Contracts\Auth\Guard as Auth;
use Illuminate\Log\Logger;

class LogoutService_Test extends TestCase
{
    use DatabaseMigrations, DatabaseTransactions;

    private $loggerStub;
    private $discourseUserApiMock;
    private $discourseAdminApiMock;

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

        $this->loggerStub = $this->getMockBuilder(Logger::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->discourseUserApiMock = $this->getMockBuilder(DiscourseUserApi::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->discourseAdminApiMock = $this->getMockBuilder(DiscourseAdminApi::class)
            ->disableOriginalConstructor()
            ->getMock();
    }

    /**
     * Creates a new Account and logins
     * as that account
     *
     * @param Auth $auth
     * @return void
     */
    private function loginAsUser(Auth $auth)
    {
        $account = factory(Account::class)->create();
        $auth->setUser($account);

        $this->assertTrue($auth->check());
    }

    public function testLogoutOfPcb_canLogout()
    {
        // given...
        $auth = resolve(Auth::class);
        $service = new LogoutService($this->discourseUserApiMock, 
                                     $this->discourseAdminApiMock, 
                                     $auth, 
                                     $this->loggerStub);
        $this->loginAsUser($auth);

        // when...
        $service->logoutOfPCB();

        // expect...
        $this->assertFalse($auth->check());
    }
    
    public function testLogoutOfPcb_notLoggedIn_doesNothing()
    {
        // given...
        $auth = resolve(Auth::class);
        $service = new LogoutService($this->discourseUserApiMock, 
                                     $this->discourseAdminApiMock, 
                                     $auth, 
                                     $this->loggerStub);

        $this->assertFalse($auth->check());

        // when...
        $result = $service->logoutOfPCB();

        // expect...
        $this->assertFalse($result);
    }

    public function testLogoutOfBoth_canLogoutOfPcb()
    {
        // given...
        $this->discourseUserApiMock
            ->expects($this->once())
            ->method('fetchUserByPcbId')
            ->willReturn([
                'user' => [
                    'id' => 15,
                    'username' => 'test_user',
                ],
            ]);

        $auth = resolve(Auth::class);
        $service = new LogoutService($this->discourseUserApiMock, 
                                    $this->discourseAdminApiMock, 
                                    $auth, 
                                    $this->loggerStub);
        $this->loginAsUser($auth);

        // when...
        $service->logoutOfDiscourseAndPcb();

        // expect...
        $this->assertFalse($auth->check());
    }

    public function testLogoutOfBoth_usesCorrectDiscourseId()
    {
        // given...
        $discourseId = 111;

        $this->discourseUserApiMock
            ->expects($this->once())
            ->method('fetchUserByPcbId')
            ->willReturn([
                'user' => [
                    'id' => $discourseId,
                    'username' => 'test_user',
                ],
            ]);

        $auth = resolve(Auth::class);
        $service = new LogoutService($this->discourseUserApiMock, 
                                    $this->discourseAdminApiMock, 
                                    $auth, 
                                    $this->loggerStub);
        $this->loginAsUser($auth);

        // expect...
        $this->discourseAdminApiMock
            ->expects($this->once())
            ->method('requestLogout')
            ->with($this->equalTo($discourseId));

        // when...
        $service->logoutOfDiscourseAndPcb();
    }
}
{'fraction_non_alphanumeric': 0.13088524590163936, 'fraction_numerical': 0.0009180327868852459, 'mean_word_length': 1.6525217391304348, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 110, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7319612', 'n_tokens_mistral': 2087, 'n_tokens_neox': 2021, 'n_words': 301}