/**
  * Validates the specified write directory
  *
  * This will attempt to create the write directory
  * if it does not already exist.
  *
  * @param   string $dir Write directory
  * @return  bool
  * @throws  MockMakerException
  */
 public static function validateWriteDir($dir)
 {
     if (!is_dir($dir)) {
         if (!mkdir($dir, 0777, true)) {
             throw new MockMakerException(MockMakerErrors::generateMessage(MockMakerErrors::WRITE_DIR_CANNOT_CREATE, array('dir' => $dir)));
         }
     }
     if (!is_writeable($dir)) {
         throw new MockMakerException(MockMakerErrors::generateMessage(MockMakerErrors::WRITE_DIR_INVALID_PERMISSIONS, array('dir' => "'{$dir}'")));
     }
     return true;
 }
 /**
  * Saves code to a file
  *
  * Does not save if file exists and overwrites are disallowed.
  *
  * @param   string $filePath Fully qualified path to new file
  * @param   string $code     Code to write in file
  * @return  bool
  * @throws  MockMakerException
  */
 protected function writeFile($filePath, $code)
 {
     // ensure that the directory we're attempting to write to exists
     // important for recursive writes!
     $writeDir = PathWorker::getPathUpToName($filePath);
     DirectoryWorker::validateWriteDir($writeDir);
     if (!file_put_contents($filePath, $code)) {
         throw new MockMakerException(MockMakerErrors::generateMessage(MockMakerErrors::DATA_POINT_WORKER_WRITE_ERR, array('file' => $filePath)));
     }
     return true;
 }
 public function test_generateMessage_returnsValidMessage()
 {
     $expected = "Unknown error while attempting to generate mock for class 'TestClass'.";
     $actual = MockMakerErrors::generateMessage(MockMakerErrors::CLASS_CANNOT_BE_MOCKED, array('class' => 'TestClass'));
     $this->assertEquals($expected, $actual);
 }
示例#4
0
 /**
  * Returns files from an array that match a regex pattern
  *
  * @param   array  $files Files to filter
  * @param   string $regex Regex to use on file names
  * @return  array
  * @throws  MockMakerException
  */
 private function getFilesThatMatchRegex($files, $regex)
 {
     $matches = [];
     try {
         if (!empty($regex)) {
             $matches = $this->getMatchingFiles($files, $regex);
         }
     } catch (\Exception $e) {
         throw new MockMakerException(MockMakerErrors::generateMessage(MockMakerErrors::INVALID_REGEX, array('regex' => $regex)));
     }
     return $matches;
 }
 /**
  * Determines a class's namespace by iterating over a filepath=>namespace conversion
  *
  * @param   string $filePath Fully qualified target file path
  * @param   string $rootPath Project root path
  * @return  string
  * @throws  MockMakerException
  */
 private function determineClassNamespace($filePath, $rootPath)
 {
     $className = PathWorker::getClassNameFromFilePath($filePath);
     if ($result = $this->checkClassUsingValidNamespacesArray($className)) {
         return $result;
     }
     // not already in our array, so we have to find it the hard way
     $classPath = PathWorker::convertFilePathToClassPath($filePath, $rootPath);
     if (!($result = $this->getClassNamespaceFromFilePath($classPath))) {
         $args = array('class' => $className);
         throw new MockMakerException(MockMakerErrors::generateMessage(MockMakerErrors::INVALID_CLASS_TYPE, $args));
     }
     $this->addValidNamespaces($result);
     return $result;
 }