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); }
/** * 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)); } }