/** * @dataProvider codeProvider * * @param string $code * @param Patch $patch * @param string $expectedResult */ public function testPatcher($code, Patch $patch, $expectedResult) { $patcher = new CodePatcher((new ParserFactory())->create(ParserFactory::PREFER_PHP7), new Standard()); $exercise = $this->getMock(PatchableExercise::class); $exercise->expects($this->once())->method('getPatch')->will($this->returnValue($patch)); $result = $patcher->patch($exercise, $code); $this->assertEquals($expectedResult, $result); }
public function testRevertAfterPatch() { file_put_contents($this->file, 'ORIGINAL CONTENT'); $fileName = $this->file; $exercise = $this->getMock(ExerciseInterface::class); $this->codePatcher->expects($this->once())->method('patch')->with($exercise, 'ORIGINAL CONTENT')->will($this->returnValue('MODIFIED CONTENT')); $listener = new CodePatchListener($this->codePatcher); $event = new Event('event', compact('exercise', 'fileName')); $listener->patch($event); $listener->revert($event); $this->assertStringEqualsFile($this->file, 'ORIGINAL CONTENT'); }
public function testRevertAfterPatch() { file_put_contents($this->file, 'ORIGINAL CONTENT'); $input = new Input('app', ['program' => $this->file]); $exercise = $this->createMock(ExerciseInterface::class); $this->codePatcher->expects($this->once())->method('patch')->with($exercise, 'ORIGINAL CONTENT')->will($this->returnValue('MODIFIED CONTENT')); $listener = new CodePatchListener($this->codePatcher); $event = new ExerciseRunnerEvent('event', $exercise, $input); $listener->patch($event); $listener->revert($event); $this->assertStringEqualsFile($this->file, 'ORIGINAL CONTENT'); }
/** * @param Event $event */ public function patch(Event $event) { $fileName = $event->getParameter('fileName'); $this->originalCode = file_get_contents($fileName); file_put_contents($fileName, $this->codePatcher->patch($event->getParameter('exercise'), $this->originalCode)); }
/** * @param ExerciseRunnerEvent $event */ public function patch(ExerciseRunnerEvent $event) { $fileName = $event->getInput()->getArgument('program'); $this->originalCode = file_get_contents($fileName); file_put_contents($fileName, $this->codePatcher->patch($event->getExercise(), $this->originalCode)); }