Задать вопрос

Как настроить юнит тесты на оплату библиотеки?

Использую в связке с yii2. Я подключил библиотеку "yoomoney/yookassa-sdk-php": "^3.7". Внедрил тесты, и не пойму как правильно написать
Сам тест
$curlClientStub = $this->getMockBuilder(CurlClient::class)
            ->setMethods(['sendRequest'])
            ->getMock();

        $curlClientStub
            ->expects($this->once())
            ->method('sendRequest')
            ->willReturn([
                ['Header-Name' => 'HeaderValue'],
                file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'createInvoiceFixtures.json'),
                ['http_code' => 200],
            ])
        ;

        $client = $this->make(Client::class);
        $client->setApiClient($curlClientStub)->setAuth(Yii::$app->yookassa->login, Yii::$app->yookassa->password);

        $yooKassa = $this->make(YooKassa::class, [
            '_client' => $client,
            'login' => Yii::$app->yookassa->login,
            'password' => Yii::$app->yookassa->password,
            'authorization' => function() use($client) {
                $this->_client = new $client;
            }
        ]);

        $course = $this->make(Course::class, [
           'id' => 1,
           'type' => 1,
           'subject' => 1,
           'exam' => 1,
        ]);

        $invoice = $this->make(Invoice::class, [
            'id' => 1,
            'sum' => 1000,
            'course' => '["'. $course->id . '"]',
            'getCourseInvoice' => function() use($course) { return [$course]; }
        ]);

        $order = $this->make(Order::class, [
            'amount' => 1000,
            'id' => 1,
            'email' => 'test@test.ru',
            'invoice_id' => $invoice->id,
            'getInvoice' => function () use($invoice) { return $invoice; }
        ]);

        var_dump($yooKassa->createPayment($order, 'http://b.99ballov.local/test/test')); die();


И сам метод createPayment в классе YiiKassa
private YooKassa\Client $_client;

public function authorization()
    {
        $this->_client = new YooKassa\Client();
        $this->_client->setAuth($this->login, $this->password);
    }

public function createPayment(Order $order, string $redirect): array
    {
        try {
            $this->authorization();
            $builder = CreatePaymentRequest::builder();
            $builder->setAmount($order->amount)
                ->setCurrency(CurrencyCode::RUB)
                ->setCapture(true)
                ->setDescription($order->getDescription())
                ->setMetadata([
                    'order_id' => $order->id,
                    'language' => 'ru',
                ]);

            // Устанавливаем страницу для редиректа после оплаты
            $builder->setConfirmation([
                'type'      => ConfirmationType::REDIRECT,
                'returnUrl' => $redirect,
            ]);

            $builder->setReceiptEmail($order->email);
//            $builder->setReceiptPhone($order->user->phone);

            $arrBasket = $this->basket($order);

            foreach ($arrBasket['items'] as $item) {
                // Добавим товар
                $builder->addReceiptItem(
                    mb_substr($item['name'], 0, 128),
                    $item['sum'],
                    1.0,
                    2,
                    'full_payment',
                    'service'
                );
            }

            // Создаем объект запроса
            $request = $builder->build();

            $response = $this->_client->createPayment($request, uniqid('', true));

            $url = $response->getConfirmation()->getConfirmationUrl();
            $parts = parse_url($url);
            parse_str($parts['query'], $query);

            return ['url' => $url, 'orderId' => $query['orderId']];
        } catch (\Exception $e) {
            throw new \Exception($e);
        }
    }


И при запуске теста у меня возникает вот такая ошибка
[Exception] yii\base\ErrorException: Undefined array key "url" in /var/www/99ballov/vendor/yoomoney/yookassa-sdk-php/lib/Client/CurlClient.php:378

Не пойму как обложить метод тестами
  • Вопрос задан
  • 24 просмотра
Подписаться 1 Простой Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы