From 783d5c263a41f425c3d306052f60b34a863173dc Mon Sep 17 00:00:00 2001
From: Brian Henry <BrianHenryIE@gmail.com>
Date: Sat, 1 May 2021 02:04:48 -0700
Subject: [PATCH] Namespaces with escaped backslashes in strings are not
 replaced

@roberts91
https://github.com/coenjacobs/mozart/issues/129
---
 tests/Issues/MozartIssue129Test.php | 112 ++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)
 create mode 100644 tests/Issues/MozartIssue129Test.php

diff --git a/tests/Issues/MozartIssue129Test.php b/tests/Issues/MozartIssue129Test.php
new file mode 100644
index 00000000..505a4b6c
--- /dev/null
+++ b/tests/Issues/MozartIssue129Test.php
@@ -0,0 +1,112 @@
+<?php
+/**
+ * Namespaces with escaped backslashes in strings are not replaced.
+ *
+ * @see https://github.com/coenjacobs/mozart/issues/129
+ *
+ * Also affects mpdf: Tag.php:170
+ *
+ * $className = 'Mpdf\Tag\\';
+ *
+ * @author BrianHenryIE
+ */
+
+namespace BrianHenryIE\Strauss\Tests\Issues;
+
+use BrianHenryIE\Strauss\ChangeEnumerator;
+use BrianHenryIE\Strauss\Composer\Extra\StraussConfig;
+use BrianHenryIE\Strauss\Prefixer;
+use PHPUnit\Framework\TestCase;
+
+/**
+ * Class MozartIssue129Test
+ * @package BrianHenryIE\Strauss\Tests\Issues
+ * @coversNothing
+ */
+class MozartIssue129Test extends TestCase
+{
+
+    /**
+     * @author BrianHenryIE
+     *
+     * @dataProvider pairTestDataProvider
+     */
+    public function test_test($phpString, $expected)
+    {
+
+        $config = $this->createMock(StraussConfig::class);
+
+        $original = 'Example\Sdk\Endpoints';
+        $replacement = 'Strauss\Example\Sdk\Endpoints';
+
+        $replacer = new Prefixer($config, __DIR__);
+
+        $result = $replacer->replaceNamespace($phpString, $original, $replacement);
+
+        $this->assertEquals($expected, $result);
+    }
+
+    public function pairTestDataProvider()
+    {
+
+        $fromTo = [];
+
+        $contents = <<<'EOD'
+$baseNamespace = "\Example\Sdk\Endpoints";
+EOD;
+        $expected = <<<'EOD'
+$baseNamespace = "\Strauss\Example\Sdk\Endpoints";
+EOD;
+        $fromTo[] = [ $contents, $expected];
+
+        $contents = <<<'EOD'
+$baseNamespace = "Example\\Sdk\\Endpoints";
+EOD;
+        $expected = <<<'EOD'
+$baseNamespace = "Strauss\\Example\\Sdk\\Endpoints";
+EOD;
+        $fromTo[] = [ $contents, $expected];
+
+        $contents = <<<'EOD'
+$baseNamespace = "Example\Sdk\Endpoints";
+EOD;
+        $expected = <<<'EOD'
+$baseNamespace = "Strauss\Example\Sdk\Endpoints";
+EOD;
+        $fromTo[] = [ $contents, $expected];
+
+        $contents = <<<'EOD'
+$baseNamespace = '\\Example\\Sdk\\Endpoints';
+EOD;
+        $expected = <<<'EOD'
+$baseNamespace = '\\Strauss\\Example\\Sdk\\Endpoints';
+EOD;
+        $fromTo[] = [ $contents, $expected];
+
+        $contents = <<<'EOD'
+$baseNamespace = '\Example\Sdk\Endpoints';
+EOD;
+        $expected = <<<'EOD'
+$baseNamespace = '\Strauss\Example\Sdk\Endpoints';
+EOD;
+        $fromTo[] = [ $contents, $expected];
+
+        $contents = <<<'EOD'
+$baseNamespace = 'Example\\Sdk\\Endpoints';
+EOD;
+        $expected = <<<'EOD'
+$baseNamespace = 'Strauss\\Example\\Sdk\\Endpoints';
+EOD;
+        $fromTo[] = [ $contents, $expected];
+
+        $contents = <<<'EOD'
+$baseNamespace = 'Example\Sdk\Endpoints';
+EOD;
+        $expected = <<<'EOD'
+$baseNamespace = 'Strauss\Example\Sdk\Endpoints';
+EOD;
+        $fromTo[] = [ $contents, $expected];
+
+        return $fromTo;
+    }
+}