Skip to content

[PHPUnit][Symfony] Make http assert provide more debug content #1680

@TomasVotruba

Description

@TomasVotruba
 $this->assertEquals(
     Response::HTTP_NO_CONTENT,
     $response->getHttpResponse()->getStatusCode(),
+    $response->getHttpResponse()->getContent()
 );

Dummy Prototype

<?php

use PhpParser\Node;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\RectorDefinition;
use Symfony\Component\HttpFoundation\Response;

final class AddErrorContentToAssertRequestRector extends AbstractRector
{
    /**
     * @return string[]
     */
    public function getNodeTypes(): array
    {
        return [Node\Expr\StaticCall::class];
    }

    /**
     * @param Node\Expr\StaticCall $node
     */
    public function refactor(Node $node): ?Node
    {
        if (!$this->isName($node->name, 'assertEquals')) {
            return null;
        }

        // already has "message" argument
        if (isset($node->args[2])) {
            return null;
        }

        // is http request status?
        if (!$node->args[0]->value instanceof Node\Expr\ClassConstFetch) {
            return null;
        }

        /** @var Node\Expr\ClassConstFetch $classConstFetch */
        $classConstFetch = $node->args[0]->value;
        if (!$this->isType($classConstFetch->class, Response::class)) {
            return null;
        }

        // we have a match!
        // @todo - get variable from arg[1]

        $methodCall = $node->args[1]->value;
        $variableName = 'response';
        if ($methodCall instanceof Node\Expr\MethodCall) {
            if ($methodCall->var instanceof Node\Expr\Variable) {
                $variableName = $this->getName($methodCall->var);
            } else {
                $variableName = $this->getName($methodCall->var->var);
            }
        }

        $getHttpResponseMethodCall = new Node\Expr\MethodCall(new Node\Expr\Variable($variableName), 'getHttpResponse');

        $getContentMethodCall = new Node\Expr\MethodCall($getHttpResponseMethodCall, 'getContent');

        $node->args[2] = new Node\Arg($getContentMethodCall);

        return $node;
    }

    public function getDefinition(): RectorDefinition
    {
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions