在官方示例中用于测试的命名空间对我不起作用

时间:2019-07-02 09:58:39

标签: php namespaces

我一直在尝试通过以下链接下载PHP的GraphQL版本:https://github.com/webonyx/graphql-php(我git被克隆,然后使用“ composer require webonyx / graphql-php”)

但是当我尝试执行“ test”文件夹中包含的代码时,我不断遇到找不到“ ...”类的错误

我首先尝试启动此示例(GraphQLTest):

<?php

declare(strict_types=1);

namespace GraphQL\Tests;

use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
use GraphQL\GraphQL;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use PHPUnit\Framework\TestCase;
use function sprintf;

class GraphQLTest extends TestCase
{
    public function testPromiseToExecute() : void
    {
        $promiseAdapter = new SyncPromiseAdapter();
        $schema         = new Schema(
            [
                'query' => new ObjectType(
                    [
                        'name' => 'Query',
                        'fields' => [
                            'sayHi' => [
                                'type' => Type::nonNull(Type::string()),
                                'args' => [
                                    'name' => [
                                        'type' => Type::nonNull(Type::string()),
                                    ],
                                ],
                                'resolve' => static function ($value, $args) use ($promiseAdapter) {
                                    return $promiseAdapter->createFulfilled(sprintf('Hi %s!', $args['name']));
                                },
                            ],
                        ],
                    ]
                ),
            ]
        );

        $promise = GraphQL::promiseToExecute($promiseAdapter, $schema, '{ sayHi(name: "John") }');
        $result  = $promiseAdapter->wait($promise);
        self::assertSame(['data' => ['sayHi' => 'Hi John!']], $result->toArray());
    }
}
$t = new GraphQLTest();
$t->testPromiseToExecute();

这只是测试,但我在最后添加了一行以启动该功能

但是我得到了错误:

Fatal error: Uncaught Error: Class 'PHPUnit\Framework\TestCase' not found in C:\Users\antoine\Desktop\boulot\Graphql\php\graphql-php\tests\GraphQLTest.php:15

该程序不知道PHPunit,因此我将其重新安装在父文件夹(当前文件夹)中,无济于事。

作为最后的手段,我修改了测试以将其转换为不使用phpunit的代码:

<?php

declare(strict_types=1);

namespace GraphQL\Tests;

use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
use GraphQL\GraphQL;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use function sprintf;

class GraphQLTest
{
    public function testPromiseToExecute() : void
    {
        $promiseAdapter = new SyncPromiseAdapter();
        $schema         = new Schema(
            [
                'query' => new ObjectType(
                    [
                        'name' => 'Query',
                        'fields' => [
                            'sayHi' => [
                                'type' => Type::nonNull(Type::string()),
                                'args' => [
                                    'name' => [
                                        'type' => Type::nonNull(Type::string()),
                                    ],
                                ],
                                'resolve' => static function ($value, $args) use ($promiseAdapter) {
                                    return $promiseAdapter->createFulfilled(sprintf('Hi %s!', $args['name']));
                                },
                            ],
                        ],
                    ]
                ),
            ]
        );

        $promise = GraphQL::promiseToExecute($promiseAdapter, $schema, '{ sayHi(name: "John") }');
        $result  = $promiseAdapter->wait($promise);
        echo $result->toArray();
    }
}
$t= new GraphQLTest();
$t->testPromiseToExecute();

但是我仍然收到错误消息:

Fatal error: Uncaught Error: Class 'GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter' not found in C:\Users\antoine\Desktop\boulot\Graphql\php\graphql-php\tests\GraphQLTest.php:18

这是一个经过测试的版本,名称空间不应引起问题。我没有更改这些文件夹中的任何内容,到底怎么不识别它们?昨天(尝试失败)安装GraphQLite时,我也遇到了类似的问题。

我猜我的设置不好,从而使命名空间的行为异常,但是我不知道如何检查。

有人有什么主意吗?

0 个答案:

没有答案