LoginSignup
0
0

More than 3 years have passed since last update.

Laravel,Graghql,phpunitでテストする。

Posted at

/tests/TestCase.phpに下記を追加

tests/TestCase.php
<?php

namespace Tests;

use Nuwave\Lighthouse\Testing\MakesGraphQLRequests; // 追加
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
    use MakesGraphQLRequests; // 追加
}

phpunit.xmlに下記を追加

phpunit.xml
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>

       <!--  下記の三行を追加 -->
        <testsuite name="Graghql">
            <directory suffix="Test.php">./tests/Graghql</directory>
        </testsuite>
    </testsuites>

上記でtests/Graghqlの中を見るようになります。

tests/Graghql/UserTest.php

    <?php

namespace Tests\Unit;

use Tests\TestCase;
use App\Models\User;

class UserTest extends TestCase
{
    /**
     * A basic unit test example.
     *
     * @return void
     */
    public function test_loginUser()
    {
        $res = $this->graphQL(/** @lang GraphQL */ '
            query(
                $user_name: String!
                $password: String!
                ){
                loginUser(
                    user_name: $user_name
                    password: $password
                ) {
                    result
                }
            }
          ', [
                'user_name' => 'テストユーザー',
                'password' => 'testpassword'
              ]
          );
        $res->assertStatus(200);
    }
}


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0