Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in /src/Codeception/Step.php in getArguments() method #30

Closed
tiger-seo opened this issue Jul 12, 2012 · 2 comments
Closed

Bug in /src/Codeception/Step.php in getArguments() method #30

tiger-seo opened this issue Jul 12, 2012 · 2 comments

Comments

@tiger-seo
Copy link
Member

Привет.
Хотел заюзать метод seeResponseContainsJson в REST модуле, но столкнулся с багом. Условие задаю как в доке:

    $I = new ApiGuy($scenario);
    $I->wantTo('fail to create a new user by API');
    $params = [
        'email'       => 'some@email.com',
        'password'    => 'qwertyui',
        'language_id' => '1'
    ];
    $I->sendPOST('/1/user/create', $params);
    $I->seeResponseIsJson();
    $I->seeResponseCodeIs(412);
    $I->seeResponseContainsJson(['error' => 'true']);
    $I->seeResponseContainsJson(['code' => 412]);

Но результат:

There was 1 error:                                                         

Couldn't fail to create a new user by api (QwertyCept.php)



  [ErrorException]                                                         
  8 Array to string conversion                                             

Длительная и глубокая отладка привела меня к:

    public function beforeStep(\Codeception\Event\Step $e) {
        $this->logger->info($e->getStep()->getHumanizedAction());
    }

в файле /pear/pear/Codeception/src/Codeception/Subscriber/Logger.php
а затем к getArguments в /pear/pear/Codeception/src/Codeception/Step.php

    /**
     * Returns this step's arguments.
     *
     * @param  boolean $asString
     * @return array|string
     */
    public function getArguments($asString = FALSE)
    {
        if (!$asString) {
            return $this->arguments;
        } else {
            $arguments = $this->arguments;

            foreach ($arguments as $k => $argument) {
                if (!is_string($argument) and is_callable($argument, true)) {
                    $arguments[$k] = 'lambda function';
                    continue;
                }
                if (is_object($argument)) {
                    if (method_exists($argument, '__toString')) {
                        $arguments[$k] = $argument->__toString();
                    } elseif (isset($argument->__mocked)) {
                        $arguments[$k] = $this->formatClassName($argument->__mocked);
                    } else {
                        $arguments[$k] = $this->formatClassName(get_class($argument));
                    }
                    continue;
                }
                // if (settype($argument, 'string') === false) throw new \InvalidArgumentException('Argument can\'t be converted to string or serialized');
            }

            switch (count($arguments)) {
                case 0:
                    return '';
                case 1:
                    return '"' . $arguments[0] . '"';
                default:

                    return stripcslashes(json_encode($arguments));

            }
        }
    }

Возможное решение (мне помогло) - такой код:

            switch (count($arguments)) {
                case 0:
                    return '';
                case 1:
                    if (is_array($arguments[0])) {
                        $arguments[0] = stripcslashes(json_encode($arguments[0]));
                    }
                    return '"' . $arguments[0] . '"';
                default:
@tiger-seo
Copy link
Member Author

Также есть проблема с методом arrayHasArray() в REST модуле. array_intersect не поддерживает проверку вложенных массивов и пытается их тайпкастнуть к стрингу, что в итоге ведет к:

Exception thrown ErrorException:
8 Array to string conversion
Stack trace:
#1 Codeception\Subscriber{closure} :
#2 array_intersect D:\server\pear\pear\Codeception\src\Codeception\Module\REST.php:207
#3 arrayHasArray D:\server\pear\pear\Codeception\src\Codeception\Module\REST.php:202

If it's a Codeception bug, please report it to GitHub

Мне помогло: заменить array_intersect() на array_intersect_assoc()

@DavertMik
Copy link
Member

Ок. Спасибо. По REST модулю у меня сейчас нет готового проекта для тестирования. Я пока его только юнит-тестами покрыл. Потому пока нет возможности узнать о реальных багах и реальном использовании.

Так что любые патчи приветствуются.
Если ещё что-то будет, стучись в скайп: davert.ua
Попробуем разобраться и по возможности быстро исправить

Naktibalda added a commit to Naktibalda/Codeception that referenced this issue Dec 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants