Author: Sebastian Bergmann (sb@sebastian-bergmann.de)
Ejemplo n.º 1
0
 /**
  * Assert function to determine if tips rendered to the page
  * have a corresponding page element.
  *
  * @param array $tips
  *   A list of tips which provide either a "data-id" or "data-class".
  *
  * @code
  * // Basic example.
  * $this->assertTourTips();
  *
  * // Advanced example. The following would be used for multipage or
  * // targeting a specific subset of tips.
  * $tips = array();
  * $tips[] = array('data-id' => 'foo');
  * $tips[] = array('data-id' => 'bar');
  * $tips[] = array('data-class' => 'baz');
  * $this->assertTourTips($tips);
  * @endcode
  */
 public function assertTourTips($tips = array())
 {
     // Get the rendered tips and their data-id and data-class attributes.
     if (empty($tips)) {
         // Tips are rendered as <li> elements inside <ol id="tour">.
         $rendered_tips = $this->xpath('//ol[@id = "tour"]//li[starts-with(@class, "tip")]');
         foreach ($rendered_tips as $rendered_tip) {
             $attributes = (array) $rendered_tip->attributes();
             $tips[] = $attributes['@attributes'];
         }
     }
     // If the tips are still empty we need to fail.
     if (empty($tips)) {
         $this->fail('Could not find tour tips on the current page.');
     } else {
         // Check for corresponding page elements.
         $total = 0;
         $modals = 0;
         foreach ($tips as $tip) {
             if (!empty($tip['data-id'])) {
                 $elements = \PHPUnit_Util_XML::cssSelect('#' . $tip['data-id'], TRUE, $this->content, TRUE);
                 $this->assertTrue(!empty($elements) && count($elements) === 1, format_string('Found corresponding page element for tour tip with id #%data-id', array('%data-id' => $tip['data-id'])));
             } elseif (!empty($tip['data-class'])) {
                 $elements = \PHPUnit_Util_XML::cssSelect('.' . $tip['data-class'], TRUE, $this->content, TRUE);
                 $this->assertFalse(empty($elements), format_string('Found corresponding page element for tour tip with class .%data-class', array('%data-class' => $tip['data-class'])));
             } else {
                 // It's a modal.
                 $modals++;
             }
             $total++;
         }
         $this->pass(format_string('Total %total Tips tested of which %modals modal(s).', array('%total' => $total, '%modals' => $modals)));
     }
 }
Ejemplo n.º 2
0
 /**
  * Note: we are overriding this method to remove the deprecated error
  * @see https://tracker.moodle.org/browse/MDL-47129
  *
  * @param  array   $matcher
  * @param  string  $actual
  * @param  string  $message
  * @param  boolean $ishtml
  *
  * @deprecated 3.0
  */
 public static function assertNotTag($matcher, $actual, $message = '', $ishtml = true)
 {
     $dom = PHPUnit_Util_XML::load($actual, $ishtml);
     $tags = self::findNodes($dom, $matcher, $ishtml);
     $matched = count($tags) > 0 && $tags[0] instanceof DOMNode;
     self::assertFalse($matched, $message);
 }
Ejemplo n.º 3
0
 private function findElementContent($html, $matcher)
 {
     $el = \PHPUnit_Util_XML::findNodes(dom_import_simplexml(simplexml_load_string($html))->ownerDocument, $matcher);
     if ($el === false) {
         return false;
     }
     return $el[0]->textContent;
 }
Ejemplo n.º 4
0
 /**
  * @dataProvider charProvider
  */
 public function testPrepareString($char)
 {
     $e = null;
     $escapedString = PHPUnit_Util_XML::prepareString($char);
     $xml = "<?xml version='1.0' encoding='UTF-8' ?><tag>{$escapedString}</tag>";
     $dom = new DomDocument('1.0', 'UTF-8');
     try {
         $dom->loadXML($xml);
     } catch (Exception $e) {
     }
     $this->assertNull($e, sprintf('PHPUnit_Util_XML::prepareString("\\x%02x") should not crash DomDocument', ord($char)));
 }
Ejemplo n.º 5
0
 public function testToOptionArray()
 {
     $this->dispatch('backend/admin/system_config/edit/section/admin');
     $dom = PHPUnit_Util_XML::load($this->getResponse()->getBody(), true);
     $select = $dom->getElementById('admin_startup_menu_item_id');
     $this->assertNotEmpty($select, 'Startup Page select missed');
     $options = $select->getElementsByTagName('option');
     $optionsCount = $options->length;
     $this->assertGreaterThan(0, $optionsCount, 'There must be present menu items at the admin backend');
     $this->assertEquals('Dashboard', $options->item(0)->nodeValue, 'First element is not Dashboard');
     $this->assertContains('Configuration', $options->item($optionsCount - 1)->nodeValue);
 }
Ejemplo n.º 6
0
 /**
  * @dataProvider getReporterProvider
  *
  * @param string[] $coverageFiles
  */
 public function testGenerateClover(array $coverageFiles)
 {
     $filename1 = $this->copyCoverageFile($coverageFiles[0], $this->targetDir);
     $filename2 = $this->copyCoverageFile($coverageFiles[1], $this->targetDir);
     $coverageMerger = new CoverageMerger();
     $coverageMerger->addCoverageFromFile($filename1);
     $coverageMerger->addCoverageFromFile($filename2);
     $target = $this->targetDir . '/coverage.xml';
     static::assertFileNotExists($target);
     $coverageMerger->getReporter()->clover($target);
     static::assertFileExists($target);
     $reportXml = \PHPUnit_Util_XML::loadFile($target);
     static::assertInstanceOf('DomDocument', $reportXml, 'Incorrect clover report xml was generated');
 }
Ejemplo n.º 7
0
 /**
  * @param  array       $options
  * @param  DOMDocument $actual
  * @return boolean
  * @since  Method available since Release 3.3.0
  * @author Mike Naberezny <*****@*****.**>
  * @author Derek DeVries <*****@*****.**>
  */
 protected static function doXmlTag(array $options, DOMDocument $actual)
 {
     $tags = PHPUnit_Util_XML::findNodes($actual, $options);
     return count($tags) > 0 && $tags[0] instanceof DOMNode;
 }
Ejemplo n.º 8
0
 /**
  * Skipped test.
  *
  * @param  PHPUnit_Framework_Test $test
  * @param  Exception              $e
  * @param  float                  $time
  * @since  Method available since Release 3.0.0
  */
 public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
 {
     if ($this->logIncompleteSkipped) {
         $error = $this->document->createElement('error');
         $error->setAttribute('type', get_class($e));
         $error->appendChild($this->document->createCDATASection(PHPUnit_Util_XML::convertToUtf8("Skipped Test\n" . PHPUnit_Util_Filter::getFilteredStacktrace($e, FALSE))));
         $this->currentTestCase->appendChild($error);
         $this->testSuiteErrors[$this->testSuiteLevel]++;
     } else {
         $this->attachCurrentTestCase = FALSE;
     }
 }
Ejemplo n.º 9
0
 /**
  * @param $filename
  * @param bool|false $isHtml
  * @param bool|false $xinclude
  * @param bool|false $strict
  *
  * @return \DOMDocument
  */
 public function loadFile($filename, $isHtml = false, $xinclude = false, $strict = false)
 {
     return \PHPUnit_Util_XML::loadFile($filename, $isHtml, $xinclude, $strict);
 }
Ejemplo n.º 10
0
 /**
  * @param  PHPUnit_Framework_TestResult $result
  */
 public function process(PHPUnit_Framework_TestResult $result)
 {
     $sutData = $result->getCodeCoverageInformation();
     $sutFiles = PHPUnit_Util_CodeCoverage::getSummary($sutData, TRUE);
     $allData = $result->getCodeCoverageInformation(FALSE);
     $allFiles = PHPUnit_Util_CodeCoverage::getSummary($allData, TRUE);
     $testFiles = array_diff(array_keys($allFiles), array_keys($sutFiles));
     foreach (array_keys($allFiles) as $key) {
         if (!@in_array($key, $testFiles)) {
             unset($allFiles[$key]);
         }
     }
     $allCommonPath = PHPUnit_Util_Filesystem::reducePaths($allFiles);
     $sutCommonPath = PHPUnit_Util_Filesystem::reducePaths($sutFiles);
     $testFiles = $allFiles;
     unset($allData);
     unset($allFiles);
     unset($sutData);
     $testToCoveredLinesMap = array();
     $time = time();
     foreach ($sutFiles as $filename => $data) {
         $fullPath = $sutCommonPath . DIRECTORY_SEPARATOR . $filename;
         if (file_exists($fullPath)) {
             $fullPath = realpath($fullPath);
             $document = new DOMDocument('1.0', 'UTF-8');
             $document->formatOutput = TRUE;
             $coveredFile = $document->createElement('coveredFile');
             $coveredFile->setAttribute('fullPath', $fullPath);
             $coveredFile->setAttribute('shortenedPath', $filename);
             $coveredFile->setAttribute('generated', $time);
             $coveredFile->setAttribute('phpunit', PHPUnit_Runner_Version::id());
             $document->appendChild($coveredFile);
             $lines = file($fullPath, FILE_IGNORE_NEW_LINES);
             $lineNum = 1;
             foreach ($lines as $line) {
                 if (isset($data[$lineNum])) {
                     if (is_array($data[$lineNum])) {
                         $count = count($data[$lineNum]);
                     } else {
                         $count = $data[$lineNum];
                     }
                 } else {
                     $count = -3;
                 }
                 $xmlLine = $coveredFile->appendChild($document->createElement('line'));
                 $xmlLine->setAttribute('lineNumber', $lineNum);
                 $xmlLine->setAttribute('executed', $count);
                 $xmlLine->appendChild($document->createElement('body', htmlspecialchars(PHPUnit_Util_XML::convertToUtf8($line), ENT_COMPAT, 'UTF-8')));
                 if (isset($data[$lineNum]) && is_array($data[$lineNum])) {
                     $xmlTests = $document->createElement('tests');
                     $xmlLine->appendChild($xmlTests);
                     foreach ($data[$lineNum] as $test) {
                         $xmlTest = $xmlTests->appendChild($document->createElement('test'));
                         if ($test instanceof PHPUnit_Framework_TestCase) {
                             $xmlTest->setAttribute('name', $test->getName());
                             $xmlTest->setAttribute('status', $test->getStatus());
                             if ($test->hasFailed()) {
                                 $xmlTest->appendChild($document->createElement('message', htmlspecialchars(PHPUnit_Util_XML::convertToUtf8($test->getStatusMessage()), ENT_COMPAT, 'UTF-8')));
                             }
                             $class = new ReflectionClass($test);
                             $testFullPath = $class->getFileName();
                             $testShortenedPath = str_replace($allCommonPath, '', $testFullPath);
                             $methodName = $test->getName(FALSE);
                             if ($class->hasMethod($methodName)) {
                                 $method = $class->getMethod($methodName);
                                 $startLine = $method->getStartLine();
                                 $xmlTest->setAttribute('class', $class->getName());
                                 $xmlTest->setAttribute('fullPath', $testFullPath);
                                 $xmlTest->setAttribute('shortenedPath', $testShortenedPath);
                                 $xmlTest->setAttribute('line', $startLine);
                                 if (!isset($testToCoveredLinesMap[$testFullPath][$startLine])) {
                                     $testToCoveredLinesMap[$testFullPath][$startLine] = array();
                                 }
                                 if (!isset($testToCoveredLinesMap[$testFullPath][$startLine][$fullPath])) {
                                     $testToCoveredLinesMap[$testFullPath][$startLine][$fullPath] = array('coveredLines' => array($lineNum), 'shortenedPath' => $filename);
                                 } else {
                                     $testToCoveredLinesMap[$testFullPath][$startLine][$fullPath]['coveredLines'][] = $lineNum;
                                 }
                             }
                         }
                     }
                 }
                 $lineNum++;
             }
             $document->save(sprintf('%s%s.xml', $this->directory, PHPUnit_Util_Filesystem::getSafeFilename(str_replace(DIRECTORY_SEPARATOR, '_', $filename))));
         }
     }
     foreach ($testFiles as $filename => $data) {
         $fullPath = $allCommonPath . DIRECTORY_SEPARATOR . $filename;
         if (file_exists($fullPath)) {
             $document = new DOMDocument('1.0', 'UTF-8');
             $document->formatOutput = TRUE;
             $testFile = $document->createElement('testFile');
             $testFile->setAttribute('fullPath', $fullPath);
             $testFile->setAttribute('shortenedPath', $filename);
             $testFile->setAttribute('generated', $time);
             $testFile->setAttribute('phpunit', PHPUnit_Runner_Version::id());
             $document->appendChild($testFile);
             $lines = file($fullPath, FILE_IGNORE_NEW_LINES);
             $lineNum = 1;
             foreach ($lines as $line) {
                 $xmlLine = $testFile->appendChild($document->createElement('line'));
                 $xmlLine->setAttribute('lineNumber', $lineNum);
                 $xmlLine->appendChild($document->createElement('body', htmlspecialchars(PHPUnit_Util_XML::convertToUtf8($line), ENT_COMPAT, 'UTF-8')));
                 if (isset($testToCoveredLinesMap[$fullPath][$lineNum])) {
                     $xmlCoveredFiles = $xmlLine->appendChild($document->createElement('coveredFiles'));
                     foreach ($testToCoveredLinesMap[$fullPath][$lineNum] as $coveredFileFullPath => $coveredFileData) {
                         $xmlCoveredFile = $xmlCoveredFiles->appendChild($document->createElement('coveredFile'));
                         $xmlCoveredFile->setAttribute('fullPath', $fullPath);
                         $xmlCoveredFile->setAttribute('shortenedPath', $coveredFileData['shortenedPath']);
                         foreach ($coveredFileData['coveredLines'] as $coveredLineNum) {
                             $xmlCoveredLine = $xmlCoveredFile->appendChild($document->createElement('coveredLine', $coveredLineNum));
                         }
                     }
                 }
                 $lineNum++;
             }
             $document->save(sprintf('%s%s.xml', $this->directory, PHPUnit_Util_Filesystem::getSafeFilename(str_replace(DIRECTORY_SEPARATOR, '_', $filename))));
         }
     }
 }
Ejemplo n.º 11
0
 private function __construct($testCase, $html)
 {
     $this->testCase = $testCase;
     $this->doc = PHPUnit_Util_XML::load($html, true);
     $this->domxpath = new DOMXPath($this->doc);
 }
Ejemplo n.º 12
0
 protected function notEqualValues()
 {
     // cyclic dependencies
     $book1 = new Book();
     $book1->author = new Author('Terry Pratchett');
     $book1->author->books[] = $book1;
     $book2 = new Book();
     $book2->author = new Author('Terry Pratch');
     $book2->author->books[] = $book2;
     $book3 = new Book();
     $book3->author = 'Terry Pratchett';
     $book4 = new stdClass();
     $book4->author = 'Terry Pratchett';
     $object1 = new SampleClass(4, 8, 15);
     $object2 = new SampleClass(16, 23, 42);
     $object3 = new SampleClass(4, 8, 15);
     $storage1 = new SplObjectStorage();
     $storage1->attach($object1);
     $storage2 = new SplObjectStorage();
     $storage2->attach($object3);
     // same content, different object
     // cannot use $filesDirectory, because neither setUp() nor
     // setUpBeforeClass() are executed before the data providers
     $file = dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'foo.xml';
     return array(array('a', 'b'), array('a', 'A'), array('9E6666666', '9E7777777'), array(1, 2), array(2, 1), array(2.3, 4.2), array(2.3, 4.2, 0.5), array(array(2.3), array(4.2), 0.5), array(array(array(2.3)), array(array(4.2)), 0.5), array(new Struct(2.3), new Struct(4.2), 0.5), array(array(new Struct(2.3)), array(new Struct(4.2)), 0.5), array(NAN, NAN), array(array(), array(0 => 1)), array(array(0 => 1), array()), array(array(0 => NULL), array()), array(array(0 => 1, 1 => 2), array(0 => 1, 1 => 3)), array(array('a', 'b' => array(1, 2)), array('a', 'b' => array(2, 1))), array(new SampleClass(4, 8, 15), new SampleClass(16, 23, 42)), array($object1, $object2), array($book1, $book2), array($book3, $book4), array(fopen($file, 'r'), fopen($file, 'r')), array($storage1, $storage2), array(PHPUnit_Util_XML::load('<root></root>'), PHPUnit_Util_XML::load('<bar/>')), array(PHPUnit_Util_XML::load('<foo attr1="bar"/>'), PHPUnit_Util_XML::load('<foo attr1="foobar"/>')), array(PHPUnit_Util_XML::load('<foo> bar </foo>'), PHPUnit_Util_XML::load('<foo />')), array(PHPUnit_Util_XML::load('<foo xmlns="urn:myns:bar"/>'), PHPUnit_Util_XML::load('<foo xmlns="urn:notmyns:bar"/>')), array(PHPUnit_Util_XML::load('<foo> bar </foo>'), PHPUnit_Util_XML::load('<foo> bir </foo>')), array(new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 03:13:35', new DateTimeZone('America/New_York'))), array(new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 03:13:35', new DateTimeZone('America/New_York')), 3500), array(new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 05:13:35', new DateTimeZone('America/New_York')), 3500), array(new DateTime('2013-03-29', new DateTimeZone('America/New_York')), new DateTime('2013-03-30', new DateTimeZone('America/New_York'))), array(new DateTime('2013-03-29', new DateTimeZone('America/New_York')), new DateTime('2013-03-30', new DateTimeZone('America/New_York')), 43200), array(new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/Chicago'))), array(new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/Chicago')), 3500), array(new DateTime('2013-03-30', new DateTimeZone('America/New_York')), new DateTime('2013-03-30', new DateTimeZone('America/Chicago'))), array(new DateTime('2013-03-29T05:13:35-0600'), new DateTime('2013-03-29T04:13:35-0600')), array(new DateTime('2013-03-29T05:13:35-0600'), new DateTime('2013-03-29T05:13:35-0500')), array(new SampleClass(4, 8, 15), FALSE), array(FALSE, new SampleClass(4, 8, 15)), array(array(0 => 1, 1 => 2), FALSE), array(FALSE, array(0 => 1, 1 => 2)), array(array(), new stdClass()), array(new stdClass(), array()), array(0, 'Foobar'), array('Foobar', 0), array(3, acos(8)), array(acos(8), 3));
 }
Ejemplo n.º 13
0
 /**
  * @param  PHPUnit_Framework_TestResult $result
  */
 public function process(PHPUnit_Framework_TestResult $result, $minLines = 5, $minMatches = 70)
 {
     $codeCoverage = $result->getCodeCoverageInformation();
     $summary = PHPUnit_Util_CodeCoverage::getSummary($codeCoverage);
     $files = array_keys($summary);
     $metrics = new PHPUnit_Util_Metrics_Project($files, $summary, TRUE, $minLines, $minMatches);
     $document = new DOMDocument('1.0', 'UTF-8');
     $document->formatOutput = TRUE;
     $cpd = $document->createElement('pmd-cpd');
     $cpd->setAttribute('version', 'PHPUnit ' . PHPUnit_Runner_Version::id());
     $document->appendChild($cpd);
     foreach ($metrics->getDuplicates() as $duplicate) {
         $xmlDuplication = $cpd->appendChild($document->createElement('duplication'));
         $xmlDuplication->setAttribute('lines', $duplicate['numLines']);
         $xmlDuplication->setAttribute('tokens', $duplicate['numTokens']);
         $xmlFile = $xmlDuplication->appendChild($document->createElement('file'));
         $xmlFile->setAttribute('path', $duplicate['fileA']->getPath());
         $xmlFile->setAttribute('line', $duplicate['firstLineA']);
         $xmlFile = $xmlDuplication->appendChild($document->createElement('file'));
         $xmlFile->setAttribute('path', $duplicate['fileB']->getPath());
         $xmlFile->setAttribute('line', $duplicate['firstLineB']);
         $xmlDuplication->appendChild($document->createElement('codefragment', PHPUnit_Util_XML::prepareString(join('', array_slice($duplicate['fileA']->getLines(), $duplicate['firstLineA'] - 1, $duplicate['numLines'])))));
     }
     $this->write($document->saveXML());
     $this->flush();
 }
Ejemplo n.º 14
0
 /**
  * @dataProvider provideTranslationFiles
  */
 public function testTranslationFileIsValid($filePath)
 {
     \PHPUnit_Util_XML::loadfile($filePath, false, false, true);
 }
Ejemplo n.º 15
0
 /**
  * @expectedException PHPUnit_Framework_Exception
  * @expectedExceptionMessage Could not load XML from boolean
  */
 public function testLoadBoolean()
 {
     PHPUnit_Util_XML::load(false);
 }
Ejemplo n.º 16
0
 /**
  * Loads a PHPUnit configuration file.
  *
  * @param  string $filename
  */
 public function __construct($filename)
 {
     $this->document = PHPUnit_Util_XML::load($filename);
     $this->xpath = new DOMXPath($this->document);
 }
Ejemplo n.º 17
0
 protected function equalValues()
 {
     // cyclic dependencies
     $book1 = new Book();
     $book1->author = new Author('Terry Pratchett');
     $book1->author->books[] = $book1;
     $book2 = new Book();
     $book2->author = new Author('Terry Pratchett');
     $book2->author->books[] = $book2;
     $object1 = new SampleClass(4, 8, 15);
     $object2 = new SampleClass(4, 8, 15);
     $storage1 = new SplObjectStorage();
     $storage1->attach($object1);
     $storage2 = new SplObjectStorage();
     $storage2->attach($object1);
     return array(array('a', 'A', 0, false, true), array(array('a' => 1, 'b' => 2), array('b' => 2, 'a' => 1)), array(array(1), array('1')), array(array(3, 2, 1), array(2, 3, 1), 0, true), array(2.3, 2.5, 0.5), array(array(2.3), array(2.5), 0.5), array(array(array(2.3)), array(array(2.5)), 0.5), array(new Struct(2.3), new Struct(2.5), 0.5), array(array(new Struct(2.3)), array(new Struct(2.5)), 0.5), array(1, 2, 1), array($object1, $object2), array($book1, $book2), array($storage1, $storage2), array(PHPUnit_Util_XML::load('<root></root>'), PHPUnit_Util_XML::load('<root/>')), array(PHPUnit_Util_XML::load('<root attr="bar"></root>'), PHPUnit_Util_XML::load('<root attr="bar"/>')), array(PHPUnit_Util_XML::load('<root><foo attr="bar"></foo></root>'), PHPUnit_Util_XML::load('<root><foo attr="bar"/></root>')), array(PHPUnit_Util_XML::load("<root>\n  <child/>\n</root>"), PHPUnit_Util_XML::load('<root><child/></root>')), array(new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York'))), array(new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 04:13:25', new DateTimeZone('America/New_York')), 10), array(new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 04:14:40', new DateTimeZone('America/New_York')), 65), array(new DateTime('2013-03-29', new DateTimeZone('America/New_York')), new DateTime('2013-03-29', new DateTimeZone('America/New_York'))), array(new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 03:13:35', new DateTimeZone('America/Chicago'))), array(new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 03:13:49', new DateTimeZone('America/Chicago')), 15), array(new DateTime('2013-03-30', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 23:00:00', new DateTimeZone('America/Chicago'))), array(new DateTime('2013-03-30', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 23:01:30', new DateTimeZone('America/Chicago')), 100), array(new DateTime('@1364616000'), new DateTime('2013-03-29 23:00:00', new DateTimeZone('America/Chicago'))), array(new DateTime('2013-03-29T05:13:35-0500'), new DateTime('2013-03-29T04:13:35-0600')), array(0, '0'), array('0', 0), array(2.3, '2.3'), array('2.3', 2.3), array((string) (1 / 3), 1 - 2 / 3), array(1 / 3, (string) (1 - 2 / 3)), array('string representation', new ClassWithToString()), array(new ClassWithToString(), 'string representation'));
 }
Ejemplo n.º 18
0
 protected function equalValues()
 {
     // cyclic dependencies
     $book1 = new Book();
     $book1->author = new Author('Terry Pratchett');
     $book1->author->books[] = $book1;
     $book2 = new Book();
     $book2->author = new Author('Terry Pratchett');
     $book2->author->books[] = $book2;
     $object1 = new SampleClass(4, 8, 15);
     $object2 = new SampleClass(4, 8, 15);
     $storage1 = new SplObjectStorage();
     $storage1->attach($object1);
     $storage2 = new SplObjectStorage();
     $storage2->attach($object1);
     return [['a', 'A', 0, false, true], [['a' => 1, 'b' => 2], ['b' => 2, 'a' => 1]], [[1], ['1']], [[3, 2, 1], [2, 3, 1], 0, true], [2.3, 2.5, 0.5], [[2.3], [2.5], 0.5], [[[2.3]], [[2.5]], 0.5], [new Struct(2.3), new Struct(2.5), 0.5], [[new Struct(2.3)], [new Struct(2.5)], 0.5], [1, 2, 1], [$object1, $object2], [$book1, $book2], [$storage1, $storage2], [PHPUnit_Util_XML::load('<root></root>'), PHPUnit_Util_XML::load('<root/>')], [PHPUnit_Util_XML::load('<root attr="bar"></root>'), PHPUnit_Util_XML::load('<root attr="bar"/>')], [PHPUnit_Util_XML::load('<root><foo attr="bar"></foo></root>'), PHPUnit_Util_XML::load('<root><foo attr="bar"/></root>')], [PHPUnit_Util_XML::load("<root>\n  <child/>\n</root>"), PHPUnit_Util_XML::load('<root><child/></root>')], [new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York'))], [new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 04:13:25', new DateTimeZone('America/New_York')), 10], [new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 04:14:40', new DateTimeZone('America/New_York')), 65], [new DateTime('2013-03-29', new DateTimeZone('America/New_York')), new DateTime('2013-03-29', new DateTimeZone('America/New_York'))], [new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 03:13:35', new DateTimeZone('America/Chicago'))], [new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 03:13:49', new DateTimeZone('America/Chicago')), 15], [new DateTime('2013-03-30', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 23:00:00', new DateTimeZone('America/Chicago'))], [new DateTime('2013-03-30', new DateTimeZone('America/New_York')), new DateTime('2013-03-29 23:01:30', new DateTimeZone('America/Chicago')), 100], [new DateTime('@1364616000'), new DateTime('2013-03-29 23:00:00', new DateTimeZone('America/Chicago'))], [new DateTime('2013-03-29T05:13:35-0500'), new DateTime('2013-03-29T04:13:35-0600')], [0, '0'], ['0', 0], [2.3, '2.3'], ['2.3', 2.3], [(string) (1 / 3), 1 - 2 / 3], [1 / 3, (string) (1 - 2 / 3)], ['string representation', new ClassWithToString()], [new ClassWithToString(), 'string representation']];
 }
Ejemplo n.º 19
0
 public function testConvertAssertRange()
 {
     $selector = '#foo';
     $content = array('greater_than' => 5, 'less_than' => 10);
     $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
     $tag = array('id' => 'foo');
     $this->assertEquals($tag, $converted);
 }
Ejemplo n.º 20
0
 /**
  * Method which generalizes addError() and addFailure()
  *
  * @param PHPUnit_Framework_Test $test
  * @param Exception              $e
  * @param float                  $time
  * @param string                 $type
  */
 private function doAddFault(PHPUnit_Framework_Test $test, Exception $e, $time, $type)
 {
     if ($this->currentTestCase === null) {
         return;
     }
     if ($test instanceof PHPUnit_Framework_SelfDescribing) {
         $buffer = $test->toString() . "\n";
     } else {
         $buffer = '';
     }
     $buffer .= PHPUnit_Framework_TestFailure::exceptionToString($e) . "\n" . PHPUnit_Util_Filter::getFilteredStacktrace($e);
     $fault = $this->document->createElement($type, PHPUnit_Util_XML::prepareString($buffer));
     $fault->setAttribute('type', get_class($e));
     $this->currentTestCase->appendChild($fault);
 }
Ejemplo n.º 21
0
 /**
  * Asserts that a hierarchy of DOMElements matches.
  *
  * @param DOMElement $expectedElement
  * @param DOMElement $actualElement
  * @param bool       $checkAttributes
  * @param string     $message
  *
  * @since  Method available since Release 3.3.0
  */
 public static function assertEqualXMLStructure(DOMElement $expectedElement, DOMElement $actualElement, $checkAttributes = false, $message = '')
 {
     $tmp = new DOMDocument();
     $expectedElement = $tmp->importNode($expectedElement, true);
     $tmp = new DOMDocument();
     $actualElement = $tmp->importNode($actualElement, true);
     unset($tmp);
     self::assertEquals($expectedElement->tagName, $actualElement->tagName, $message);
     if ($checkAttributes) {
         self::assertEquals($expectedElement->attributes->length, $actualElement->attributes->length, sprintf('%s%sNumber of attributes on node "%s" does not match', $message, !empty($message) ? "\n" : '', $expectedElement->tagName));
         for ($i = 0; $i < $expectedElement->attributes->length; $i++) {
             $expectedAttribute = $expectedElement->attributes->item($i);
             $actualAttribute = $actualElement->attributes->getNamedItem($expectedAttribute->name);
             if (!$actualAttribute) {
                 self::fail(sprintf('%s%sCould not find attribute "%s" on node "%s"', $message, !empty($message) ? "\n" : '', $expectedAttribute->name, $expectedElement->tagName));
             }
         }
     }
     PHPUnit_Util_XML::removeCharacterDataNodes($expectedElement);
     PHPUnit_Util_XML::removeCharacterDataNodes($actualElement);
     self::assertEquals($expectedElement->childNodes->length, $actualElement->childNodes->length, sprintf('%s%sNumber of child nodes of "%s" differs', $message, !empty($message) ? "\n" : '', $expectedElement->tagName));
     for ($i = 0; $i < $expectedElement->childNodes->length; $i++) {
         self::assertEqualXMLStructure($expectedElement->childNodes->item($i), $actualElement->childNodes->item($i), $checkAttributes, $message);
     }
 }
Ejemplo n.º 22
0
 /**
  * This assertion is the exact opposite of assertTag().
  *
  * Rather than asserting that $matcher results in a match, it asserts that
  * $matcher does not match.
  *
  * @param array  $matcher
  * @param string $actual
  * @param string $message
  * @param bool   $isHtml
  * @since  Method available since Release 3.3.0
  * @author Mike Naberezny <*****@*****.**>
  * @author Derek DeVries <*****@*****.**>
  * @deprecated
  */
 public static function assertNotTag($matcher, $actual, $message = '', $isHtml = true)
 {
     trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
     $dom = PHPUnit_Util_XML::load($actual, $isHtml);
     $tags = PHPUnit_Util_XML::findNodes($dom, $matcher, $isHtml);
     $matched = count($tags) > 0 && $tags[0] instanceof DOMNode;
     self::assertFalse($matched, $message);
 }
Ejemplo n.º 23
0
 /**
  * Skipped test.
  *
  * @param PHPUnit_Framework_Test $test
  * @param Exception              $e
  * @param float                  $time
  * @since  Method available since Release 3.0.0
  */
 public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
 {
     if ($this->logIncompleteSkipped && $this->currentTestCase !== null) {
         $error = $this->document->createElement('error', PHPUnit_Util_XML::prepareString("Skipped Test\n" . PHPUnit_Util_Filter::getFilteredStacktrace($e)));
         $error->setAttribute('type', get_class($e));
         $this->currentTestCase->appendChild($error);
         $this->testSuiteErrors[$this->testSuiteLevel]++;
     } else {
         $this->attachCurrentTestCase = false;
     }
 }
Ejemplo n.º 24
0
 /**
  * @param string $fileName
  *
  * @return string
  *
  * @throws \Exception
  */
 private function getStubbedXMLConf($fileName)
 {
     $filePath = realpath($fileName);
     if (!file_exists($filePath)) {
         throw new \Exception('Stub XML config file missing: ' . $fileName);
     }
     return \PHPUnit_Util_XML::loadFile($filePath, false, true, true);
 }
Ejemplo n.º 25
0
 /**
  * @param array $matcher
  * @param string $actual
  * @param bool $isHtml
  *
  * @return bool
  */
 private static function tagMatch($matcher, $actual, $isHtml = true)
 {
     $dom = PHPUnit_Util_XML::load($actual, $isHtml);
     $tags = PHPUnit_Util_XML::findNodes($dom, $matcher, $isHtml);
     return count($tags) > 0 && $tags[0] instanceof DOMNode;
 }
Ejemplo n.º 26
0
 /**
  * Runs a test from a Selenese (HTML) specification.
  *
  * @param string $filename
  * @access public
  */
 public function runSelenese($filename)
 {
     $document = PHPUnit_Util_XML::load($filename, TRUE);
     $xpath = new DOMXPath($document);
     $rows = $xpath->query('body/table/tbody/tr');
     foreach ($rows as $row) {
         $action = NULL;
         $arguments = array();
         $columns = $xpath->query('td', $row);
         foreach ($columns as $column) {
             if ($action === NULL) {
                 $action = $column->nodeValue;
             } else {
                 $arguments[] = $column->nodeValue;
             }
         }
         $this->__call($action, $arguments);
     }
 }
Ejemplo n.º 27
0
 /**
  * Runs a test from a Selenese (HTML) specification.
  *
  * @param string $filename
  */
 public function runSelenese($filename)
 {
     $document = PHPUnit_Util_XML::loadFile($filename, TRUE);
     $xpath = new DOMXPath($document);
     $rows = $xpath->query('body/table/tbody/tr');
     foreach ($rows as $row) {
         $action = NULL;
         $arguments = array();
         $columns = $xpath->query('td', $row);
         foreach ($columns as $column) {
             if ($action === NULL) {
                 $action = PHPUnit_Util_XML::nodeToText($column);
             } else {
                 $arguments[] = PHPUnit_Util_XML::nodeToText($column);
             }
         }
         if (method_exists($this, $action)) {
             call_user_func_array(array($this, $action), $arguments);
         } else {
             $this->__call($action, $arguments);
         }
     }
 }
Ejemplo n.º 28
0
 public function testPrepareStringEscapesChars()
 {
     $this->assertEquals('&#x1b;', PHPUnit_Util_XML::prepareString(""));
 }
Ejemplo n.º 29
0
    /**
     * Returns the configuration for listeners.
     *
     * @return array
     * @since  Method available since Release 3.4.0
     */
    public function getListenerConfiguration()
    {
        $result = array();

        foreach ($this->xpath->query('listeners/listener') as $listener) {
            $class     = (string)$listener->getAttribute('class');
            $file      = '';
            $arguments = array();

            if ($listener->hasAttribute('file')) {
                $file = $this->toAbsolutePath((string)$listener->getAttribute('file'));
            }

            if ($listener->childNodes->item(1) instanceof DOMElement &&
                $listener->childNodes->item(1)->tagName == 'arguments') {
                foreach ($listener->childNodes->item(1)->childNodes as $argument) {
                    if ($argument instanceof DOMElement) {
                        if($argument->tagName == 'file' || $argument->tagName == 'directory') {
                            $arguments[] = $this->toAbsolutePath((string)$argument->nodeValue);
                        } else {
                            $arguments[] = PHPUnit_Util_XML::xmlToVariable($argument);
                        }
                    }
                }
            }

            $result[] = array(
              'class'     => $class,
              'file'      => $file,
              'arguments' => $arguments
            );
        }

        return $result;
    }
Ejemplo n.º 30
0
 /**
  * CSS-style selector-based assertion that makes [[assertTag()]] look quite cumbersome.
  * The first argument is a string that is essentially a standard CSS selectors used to
  * match the element we want:
  *
  *  - `div`             : an element of type `div`
  *  - `div.class_nm`    : an element of type `div` whose class is `warning`
  *  - `div#myid`        : an element of type `div` whose ID equal to `myid`
  *  - `div[foo="bar"]`  : an element of type `div` whose `foo` attribute value is exactly
  *                        equal to `bar`
  *  - `div[foo~="bar"]` : an element of type `div` whose `foo` attribute value is a list
  *                        of space-separated values, one of which is exactly equal
  *                        to `bar`
  *  - `div[foo*="bar"]` : an element of type `div` whose `foo` attribute value contains
  *                        the substring `bar`
  *  - `div span`        : an span element descendant of a `div` element
  *  - `div > span`      : a span element which is a direct child of a `div` element
  *
  * We can also do combinations to any degree:
  *
  *  - `div#folder.open a[href="http://foo"][title="bar"].selected.big > span`
  *
  * The second argument determines what we're matching in the content or number of tags.
  * It can be one 4 options:
  *
  *  - `content`    : match the content of the tag
  *  - `true/false` : match if the tag exists/doesn't exist
  *  - `number`     : match a specific number of elements
  *  - `range`      : to match a range of elements, we can use an array with the options
  *                         `>` and `<`.
  *
  * {{code: php
  *     ...
  *     
  *     // There is an element with the id "binder_1" with the content "Test Foo"
  *     $this->assertSelect("#binder_1", "Test Foo");
  *     
  *     // There are 10 div elements with the class folder:
  *     $this->assertSelect("div.folder", 10);
  *     
  *     // There are more than 2, less than 10 li elements
  *     $this->assertSelect("ul > li", array('>' => 2, '<' => 10));
  *     
  *     // There are more than or exactly 2, less than or exactly 10 li elements
  *     $this->assertSelect("ul > li", array('>=' => 2, '<=' => 10));
  *     
  *     // The "#binder_foo" id exists
  *     $this->assertSelect('#binder_foo");
  *     $this->assertSelect('#binder_foo", true);
  *     
  *     // The "#binder_foo" id DOES NOT exist
  *     $this->assertSelect('#binder_foo", false);
  *     
  *     ...
  * }}
  *
  * @param   string  $selector
  * @param   mixed   $content
  * @param   boolean $exists
  * @param   string  $msg
  * @param   boolean $isHtml
  * @throws  Mad_Test_Exception
  */
 public function assertSelect($selector, $content = true, $exists = true, $msg = null, $isHtml = true)
 {
     if (!method_exists($this, 'assertSelectEquals')) {
         throw new Mad_Test_Exception('PHPUnit selector assertion support required');
     }
     // only parse response into dom once for better performance
     if ($this->_responseDom === null) {
         $body = $this->response->getBody();
         $this->_responseDom = PHPUnit_Util_XML::load($body, $isHtml);
     }
     if (is_string($content)) {
         if (preg_match('!^/.*/.?$!', $content)) {
             $this->assertSelectRegexp($selector, $content, $exists, $this->_responseDom, $msg, $isHtml);
         } else {
             $this->assertSelectEquals($selector, $content, $exists, $this->_responseDom, $msg, $isHtml);
         }
     } else {
         $this->assertSelectCount($selector, $content, $this->_responseDom, $msg, $isHtml);
     }
 }