Skip to content

Commit 140c5e4

Browse files
committed
Add test for strict types with before insertion
1 parent 62ab5b5 commit 140c5e4

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

test/CodePatcherTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,33 @@ public function testTransformerWhichAddsStrictTypesDoesNotResultInDoubleStrictTy
216216
$patcher->patch($exercise, $code)
217217
);
218218
}
219+
220+
public function testAddingStrictTypesDeclareDoesNotBreakBeforeInsertion(): void
221+
{
222+
$code = '<?php $original = true;';
223+
$patch = (new Patch())
224+
->withTransformer(function (array $statements) {
225+
return array_merge([new \PhpParser\Node\Stmt\Declare_([
226+
new DeclareDeclare(
227+
new \PhpParser\Node\Identifier('strict_types'),
228+
new LNumber(1)
229+
)
230+
])], $statements);
231+
})
232+
->withInsertion(new Insertion(Insertion::TYPE_BEFORE, '$before = "here";'));
233+
234+
$patcher = new CodePatcher((new ParserFactory())->create(ParserFactory::PREFER_PHP7), new Standard());
235+
236+
$exercise = $this->createMock(PatchableExercise::class);
237+
238+
$exercise
239+
->expects($this->once())
240+
->method('getPatch')
241+
->willReturn($patch);
242+
243+
$this->assertEquals(
244+
"<?php\n\ndeclare (strict_types=1);\n\$before = \"here\";\n\$original = true;",
245+
$patcher->patch($exercise, $code)
246+
);
247+
}
219248
}

0 commit comments

Comments
 (0)