<?php



namespace HkApps\Tests\Http\Controllers\Admin;

use HkApps\App\Models\Admin;

/**
 * Class PagesControllerTest.
 *
 * @package HkApps\Tests\Http\Controllers\Admin
 */
class PagesControllerTest extends TestCase
{
    public function testIndexOnLogined()
    {
        $this
            ->visitWithLogined('/admin')
            ->see('Admin Page')
            ->see('Tools')
            ->see('/admin/admin-user')
            ->see('/admin/shorturl')
            ->see('/admin/console')
            ->see('/admin/error-log')
            ->see('/admin/info')
            ->see('/admin/twitter-user')
            ->dontSee('/admin/login')
            ->assertResponseOk();
    }

    public function testIndexOnNoLogined()
    {
        $this
            ->visitWithNoLogined('/admin')
            ->see('Admin Page')
            ->see('ログイン')
            ->assertResponseOk();
    }

    public function testLoginOnLogined()
    {
        $this
            ->getWithLogined('/admin/login')
            ->onNoLogined();
    }

    /**
     * @dataProvider allAdminDataProvider
     *
     * @param Admin $admin
     */
    public function testLoginOnNoLogined(Admin $admin)
    {
        $this
            ->visitWithNoLogined('/admin/login')
            ->see('ログイン')
            ->see('User')
            ->see('Password')
            ->see('キャンセル')
            ->type($admin->user, 'user')
            ->type($admin->password, 'password')
            ->assertResponseOk();
        $this
            ->press('ログイン')
            ->seePageIs('/admin')
            ->see('Admin Page')
            ->see('Tools')
            ->see('/admin/admin-user')
            ->see('/admin/shorturl')
            ->assertResponseOk();
    }

    public function testLoginFailOnNoLogined()
    {
        $this
            ->visitWithNoLogined('/admin/login')
            ->type('', 'user')
            ->type('', 'password')
            ->press('ログイン')
            ->seePageIs('/admin/login')
            ->see('ユーザーネームかパスワードが不正です。');
    }

    public function testLogoutOnLogined()
    {
        $this
            ->visitWithLogined('/admin/logout')
            ->see('ログアウト')
            ->see('ログアウトしますか?')
            ->see('キャンセル')
            ->assertResponseOk();
        $this
            ->press('ログアウト')
            ->seePageIs('/admin')
            ->see('ログアウトしました。')
            ->assertResponseOk();
    }

    public function testLogoutOnNoLogined()
    {
        $this
            ->getWithNoLogined('/admin/logout')
            ->onNoLogined();
    }

    public function testHelpOnLogined()
    {
        $this
            ->visitWithLogined('/admin/help')
            ->see('Help')
            ->see('About HkApps')
            ->see('https://github.com/Hiroto-K/HkApps')
            ->see('Laravel')
            ->see(app()->version())
            ->see('HkApps')
            ->see(config('app.app_version'))
            ->see('PHP')
            ->see(PHP_VERSION)
            ->assertResponseOk();
    }

    public function testHelpOnNoLogined()
    {
        $this
            ->getWithNoLogined('/admin/help')
            ->onNoLogined();
    }

    public function testErrorLogOnLogined()
    {
        $this
            ->visitWithLogined('/admin/error-log')
            ->see('Log')
            ->see(storage_path('logs/laravel.log'))
            ->assertResponseOk();
    }

    public function testErrorLogOnNoLogined()
    {
        $this
            ->getWithNoLogined('/admin/error-log')
            ->onNoLogined();
    }

    public function testInfoOnLogined()
    {
        $this
            ->visitWithLogined('/admin/info')
            ->see('Info')
            ->assertResponseOk();
        $c = function ($item, $value) {
            $this->see($item);
            if (is_string($value)) {
                $this->see($value);
            } elseif (is_array($value)) {
                $this->see(implode(',', $value));
            } else {
                $this->see($value);
            }
        };
        foreach ($_SERVER as $item => $value) {
            $c($item, $value);
        }
        foreach ($_GET as $item => $value) {
            $c($item, $value);
        }
        foreach ($_POST as $item => $value) {
            $c($item, $value);
        }
    }

    public function testInfoOnNoLogined()
    {
        $this
            ->getWithNoLogined('/admin/info')
            ->onNoLogined();
    }
}
