/** * @param string $filename * @param FileReport $report * @return bool|string */ private function getSchemaValidationSource($filename, $report) { if (preg_match('/^(http|https|ftp):/i', $filename) === 0) { if (file_exists($filename) === false) { $filename = $report->getFile()->getPath() . '/' . $filename; } if (!is_readable($filename)) { $report->reportProblem('unable to validate, schema file is not readable: ' . $filename); return false; } } if (isset($this->cache[$filename])) { return $this->cache[$filename]; } $validationSource = @file_get_contents($filename); if ($validationSource === false) { $report->reportProblem('unable to load schema file from: ' . $filename); return false; } if (empty($validationSource)) { $report->reportProblem(sprintf('xsd validation file is empty ("%s").', $filename)); return false; } return $this->cache[$filename] = $validationSource; }
public function testReportProblemCreatesValidationProblem() { $report = FileReport::create('test_file.xml'); $report->reportProblem('my_message'); $result = $report->getProblems(); $problem = reset($result); $this->assertInstanceOf(ValidationProblem::class, $problem); $this->assertEquals('my_message', $problem->getMessage()); }
/** * @dataProvider getMockReturnValues * @param bool $return1 * @param bool $return2 * @param bool $expected */ public function testCollectionCorrectReturnValue($return1, $return2, $expected) { $mock1 = $this->getMockBuilder(ValidationInterface::class)->getMock(); $mock2 = $this->getMockBuilder(ValidationInterface::class)->getMock(); $mock1->method('validateFile')->willReturn($return1); $mock2->method('validateFile')->willReturn($return2); $collection = new ValidationCollection(); /** @var ValidationInterface $mock1 */ /** @var ValidationInterface $mock2 */ $collection->addValidation($mock1)->addValidation($mock2); $this->assertEquals($expected, $collection->validateFile(FileReport::create('some_file.xml'))); }
/** * @inheritdoc */ public function validateFile(FileReport $report) { $realPath = $report->getFile()->getRealPath(); if (is_file($realPath) === false) { $report->reportProblem('file not found: ' . $realPath); return false; } if (is_readable($realPath) === false) { $report->reportProblem('file not readable: ' . $realPath); return false; } libxml_clear_errors(); $domDoc = new \DOMDocument(); $domDoc->load($realPath, LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_PEDANTIC); $errors = libxml_get_errors(); foreach ($this->formatter->formatErrors($errors) as $problem) { $report->reportProblem($problem); } return !$report->hasProblems(); }
/** * lint a file, pass errors to the queue * @param \SplFileInfo $file * @return bool */ private function lintFile(\SplFileInfo $file) { if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { $this->output->write('lint file ' . $file . ' ... '); } $report = FileReport::create($file); $this->reports[] = $report; $status = $this->validator->validateFile($report); if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { if ($status === false) { $this->output->writeln('<error>errors</error>'); } else { $this->output->writeln('<info>passed.</info>'); } } else { if ($status === false) { $this->output->write('<error>F</error>'); } else { $this->output->write('.'); } } return $status; }