Exemple #1
0
 public function onGetClassFixClass(GetClassEvent $event)
 {
     $fileLocation = $event->getFileLocation();
     $command = new FixCommand();
     $arguments = ['path' => $fileLocation];
     $arguments = array_merge($arguments, $this->arguments);
     $output = isset($this->output) ? $this->output : new BufferedOutput();
     $output->writeln(sprintf('<info>Running PSR Fixer on %s</info>', $fileLocation));
     $input = new ArrayInput($arguments);
     $command->run($input, $output);
 }
 public function testConstantNameConversion()
 {
     $parser = new Parser(new Lexer());
     $class = file_get_contents(__DIR__ . '/mock/constants/class_with_constants.php');
     $statement = $parser->parse($class);
     $event = new GetClassEvent($statement[0], '/tmp/');
     $event->setCode('<?php echo "some code";');
     $event->setUses('use Some\\Namespace;');
     $event->setNamespace('namespace Current\\Namespace;');
     $plugin = new ConstantNamesToUpper();
     $plugin->onSetClassUpdateConstants($event);
     foreach ($event->getStatements()->stmts as $constant) {
         $this->assertEquals($constant->consts[0]->value->value, $constant->consts[0]->name);
     }
 }
 public function testSavesFile()
 {
     $mockAdapter = $this->getMockBuilder('\\League\\Flysystem\\Adapter\\Local')->disableOriginalConstructor()->setMethods(['has', 'createDir', 'write'])->getMock();
     // plugin calls this method 3 times, internally write calls this method each time checking if
     // the file exist. write has must always return false otherwise it will throw an exception.
     $mockAdapter->expects($this->exactly(2))->method('has')->will($this->onConsecutiveCalls(false, false));
     $mockAdapter->expects($this->once())->method('createDir')->willReturn(true);
     $mockAdapter->expects($this->once())->method('write')->willReturn(true);
     $statement = new Class_('some_name');
     $event = new GetClassEvent($statement);
     $event->setFileLocation('/tmp/');
     $event->setCode('<?php echo "some code";');
     $plugin = new GenerateClassFile($mockAdapter);
     $plugin->onGetClassGenerateFile($event);
 }
Exemple #4
0
 public function testOnGetClassFixClass()
 {
     $pathToFile = __DIR__ . '/mock/fixer/original/GetSomeResponse.php';
     $pathToFileFixed = __DIR__ . '/mock/fixer/fixed/GetSomeResponse.php';
     $code = file_get_contents($pathToFile);
     $statement = new Class_($code);
     $event = new GetClassEvent($statement, $pathToFile);
     $event->setCode($code);
     $event->setUses('use DateTime;');
     $event->setNamespace('namespace SomeNameSpace\\Category;');
     $plugin = new PhpCsFixer();
     $plugin->onGetClassFixClass($event);
     $code = file_get_contents($pathToFile);
     $fixedCode = file_get_contents($pathToFileFixed);
     $this->assertEquals($fixedCode, $code, 'Files are not equal and they should be.');
 }
 /**
  * Use flysystem to save the file in the desired location.
  *
  * @param \Onema\ClassyFile\Event\GetClassEvent $event
  */
 public function onGetClassGenerateFile(GetClassEvent $event)
 {
     $statement = $event->getStatements();
     $fileLocation = $event->getFileLocation();
     $code = $event->getCode();
     $name = $statement->name;
     if (!$this->filesystem->has($fileLocation)) {
         // dir doesn't exist, make it
         $this->filesystem->createDir($fileLocation);
     }
     $location = sprintf('%s/%s.php', $fileLocation, $name);
     $this->filesystem->put($location, $code);
     $adapter = $this->filesystem->getAdapter();
     if ($adapter instanceof AbstractAdapter) {
         $prefix = $adapter->getPathPrefix();
         $location = Util::normalizePath($location);
         $event->setFileLocation(sprintf('%s%s', $prefix, $location));
     }
 }