Problem/Motivation

We're working towards replacing assertIdentical with assertSame. assertSame requires the arguments to be ($expected, $actual, ...), whereas assertIdentical requires ($actual, $expected,...).

Proposed resolution

In preparation for the final cleanup, change all assertIdentical to have ($expected, $actual, ...) like assertSame so that in a follow up we can just search/replace the method name.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3192221

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mondrake created an issue. See original summary.

mondrake’s picture

Assigned: Unassigned » mondrake

mondrake’s picture

Assigned: mondrake » Unassigned
Status: Active » Needs review
mondrake’s picture

Issue tags: +Deprecated assertions
longwave’s picture

Technically this means we are calling assertIdentical() incorrectly after this is committed but before the followup is.

I wonder if instead we should do the swap and rename in one go (this could be scripted), and perhaps break out the assertIdentical(..., NULL) conversion to assertNull() and any other similar changes to their own issue first?

mondrake’s picture

Thanks for looking into this @longwave.

The situation is a bit grim here. In HEAD right now part of the assertIdentical calls DO abide to its signature, but a lot of them (most?) are actually already set to ($expected, $actual,...) as it should be for assertSame.

So, I used the script below to swap all instances, and later I only git added those that require swapping. Manually.

In other words - in this issue we are just swapping those cases where the swap is needed, NOT ALL the instances.


use PhpParser\Error;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

function convertMatch($parser, $printer, $part) {
  try {
    $ast = $parser->parse('<?php ' . $part);
  }
  catch (Error $error) {
    throw new \RuntimeException("Parse error: {$error->getMessage()}");
  }
  $conv = $ast[0]->expr;
//  $conv->name->name = 'assertSame';
  $tmp = clone $conv->args[1];
  $conv->args[1] = $conv->args[0];
  $conv->args[0] = $tmp;

  return [
    $printer->prettyPrintExpr($conv) . ";",
  ];
}

// Initialize the autoloader.
require_once 'autoload.php';

$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$prettyPrinter = new Standard;

$fs = new Filesystem();
$finder = new Finder();
$finder->files()->in('core/tests')->in('core/modules')->name('*.php');

foreach ($finder as $file) {

  $str = file_get_contents((string) $file);

  // Find all the instances of '$this->assertIdentical(' in the file.
  preg_match_all('/^(.*)(\$this->assertIdentical\()/m', $str, $matches, PREG_OFFSET_CAPTURE);

  if ($matches && $matches[2]) {
    echo 'Processing ' . $file . '... ' . "\n";

    // Process and convert each instance.
    $convs = []; $i = 0;
    foreach ($matches[2] as $m) {
      $start_pos = $m[1];
      $end_pos = strpos($str, ");\n", $start_pos) + 3;
      $part = substr($str, $start_pos, $end_pos - $start_pos);
      $conv = convertMatch($parser, $prettyPrinter, $part);
//dump($conv);die;
      $convs[] = [
        'start' => $start_pos,
        'end' => $end_pos,
        'prefix' => $matches[1][$i][0],
        'conv' => $conv,
      ];
      $i++;
    }
    dump($convs);

    // Recombine the file and output.
    $out = ''; $offset = 0;
    foreach ($convs as $conv) {
      $out .= substr($str, $offset, $conv['start'] - $offset);
      $out .= $conv['conv'][0] . "\n";
      $offset = $conv['end'];
    }
    $out .= substr($str, $offset);
//    dump($out);
    file_put_contents((string) $file, $out);

  }

}
longwave’s picture

Urgh, I didn't realise that. In that case I guess it is best to do the swap first and then the rename is simple.

However do we still want to break out the ~35 changes to assertNull to a separate issue? That would be easier to get in and make the patch here more straightforward.

mondrake’s picture

Status: Needs review » Postponed

OK so let's pull out the conversions to assertNull to a separate issue.

mondrake’s picture

mondrake’s picture

Status: Postponed » Needs review

Unpostponing, related issue was committed. Rerolled.

daffie’s picture

Status: Needs review » Needs work

I could only one thing wrong with the patch.

mondrake’s picture

Status: Needs work » Needs review
daffie’s picture

Status: Needs review » Reviewed & tested by the community

All code changes look good to me.
We do this issue to make #2867959: Replace usages of deprecated AssertLegacyTrait::assertIdentical more easy.
For me it is RTBC.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Needs a rebase.

Normally we'd not leave HEAD in an interim state, but given the wrong order is already prevalent across core, and the general momentum on phpunit clean-up is very good, I think it makes sense to do this in two reviewable chunks.

mondrake’s picture

Issue tags: -Needs reroll

Rerolled.

mondrake’s picture

Status: Needs work » Reviewed & tested by the community
daffie’s picture

+1 for RTBC. In the patch chunks from the rebase are all the changes to assertIdentical() what they should be.

  • catch committed d722e9d on 9.2.x
    Issue #3192221 by mondrake, daffie, longwave: Swap assertIdentical...
catch’s picture

Version: 9.2.x-dev » 9.1.x-dev
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Committed d722e9d and pushed to 9.2.x. Thanks!

Needs a rebase for 9.1.x

mondrake’s picture

Status: Needs work » Postponed

Let's reroll #3192427: [backport] Replace usages of deprecated AssertLegacyTrait::assertNotEqual first, to keep the sequence with what went in 9.2.x

catch’s picture

Title: Swap assertIdentical arguments in preparation to replace with assertSame » [backport] Swap assertIdentical arguments in preparation to replace with assertSame
mondrake’s picture

Status: Postponed » Needs work
mondrake’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new293.32 KB

Rerolled for 9.1.x

daffie’s picture

Status: Needs review » Reviewed & tested by the community

The backport patch for 9.1 is for me RTBC.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Needs a reroll already.

ayushmishra206’s picture

Assigned: Unassigned » ayushmishra206

Will be working on this.

ayushmishra206’s picture

Assigned: ayushmishra206 » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new293.91 KB

Rerolled the patch for 9.1.x

Status: Needs review » Needs work

The last submitted patch, 28: 3192221-28.patch, failed testing. View results

mondrake’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new293.91 KB

Reroll of #24.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed e9b83ef and pushed to 9.1.x. Thanks!

  • catch committed e9b83ef on 9.1.x
    Issue #3192221 by mondrake, ayushmishra206, daffie, longwave: Swap...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.