HighlightUtilitiesTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /* Copyright (c) 2019 Geert Bergman (geert@scrivo.nl), highlight.php
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright notice,
  10. * this list of conditions and the following disclaimer in the documentation
  11. * and/or other materials provided with the distribution.
  12. * 3. Neither the name of "highlight.js", "highlight.php", nor the names of its
  13. * contributors may be used to endorse or promote products derived from this
  14. * software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. class HighlightUtilitiesTest extends PHPUnit_Framework_TestCase
  29. {
  30. /** @var \Highlight\Highlighter */
  31. private $hl;
  32. protected function setUp()
  33. {
  34. $this->hl = new \Highlight\Highlighter();
  35. }
  36. public function testGetAvailableStyleSheets_NamesOnly()
  37. {
  38. $results = \HighlightUtilities\getAvailableStyleSheets();
  39. $this->assertNotEmpty($results);
  40. foreach ($results as $result) {
  41. $this->assertNotContains(DIRECTORY_SEPARATOR, $result);
  42. $this->assertNotContains(".css", $result);
  43. }
  44. }
  45. public function testGetAvailableStyleSheets_FilePaths()
  46. {
  47. $results = \HighlightUtilities\getAvailableStyleSheets(true);
  48. $this->assertNotEmpty($results);
  49. foreach ($results as $result) {
  50. $this->assertContains(DIRECTORY_SEPARATOR, $result);
  51. $this->assertContains(".css", $result);
  52. $this->assertFileExists($result);
  53. }
  54. }
  55. public function testGetAvailableStyleSheets_SameCount()
  56. {
  57. $namesOnly = \HighlightUtilities\getAvailableStyleSheets();
  58. $filePaths = \HighlightUtilities\getAvailableStyleSheets(true);
  59. $this->assertCount(count($namesOnly), $filePaths);
  60. }
  61. public function testGetStyleSheet_Exists()
  62. {
  63. $yesExt = \HighlightUtilities\getStyleSheet("a11y-dark.css");
  64. $noExt = \HighlightUtilities\getStyleSheet("a11y-dark");
  65. $this->assertNotEmpty($yesExt);
  66. $this->assertEquals($yesExt, $noExt);
  67. }
  68. public function testGetStyleSheet_NotExists()
  69. {
  70. $this->setExpectedException('\DomainException');
  71. \HighlightUtilities\getStyleSheet("strawberry.png");
  72. }
  73. public function testSplitCodeIntoArray_MultilineComment()
  74. {
  75. $raw = <<<PHP
  76. /**
  77. * Hello World
  78. *
  79. * @api
  80. * @since 1.0.0
  81. * @param string \$str Some string parameter
  82. */
  83. PHP;
  84. $highlighted = $this->hl->highlight('php', $raw);
  85. $cleanSplit = \HighlightUtilities\splitCodeIntoArray($highlighted->value);
  86. $dumbSplit = preg_split('/\R/', $highlighted->value);
  87. $this->assertEquals(1, substr_count($highlighted->value, 'hljs-comment'));
  88. $this->assertEquals(count($cleanSplit), substr_count(implode(PHP_EOL, $cleanSplit), 'hljs-comment'));
  89. $this->assertTrue(is_array($cleanSplit));
  90. $this->assertCount(count($dumbSplit), $cleanSplit);
  91. $this->assertNotEquals($cleanSplit, $dumbSplit);
  92. foreach ($cleanSplit as $line) {
  93. $this->assertStringStartsWith('<span class="hljs-comment">', trim($line));
  94. $this->assertStringEndsWith('</span>', trim($line));
  95. }
  96. }
  97. public function testGetThemeBackgroundColorSingleColor()
  98. {
  99. $theme = 'atom-one-dark';
  100. $this->assertEquals(array('r' => 40, 'g' => 44, 'b' => 52), \HighlightUtilities\getThemeBackgroundColor($theme));
  101. }
  102. public function testGetThemeBackgroundColorColorWithBgImage()
  103. {
  104. $theme = 'brown-paper';
  105. $this->assertEquals(array('r' => 183, 'g' => 166, 'b' => 142), \HighlightUtilities\getThemeBackgroundColor($theme));
  106. }
  107. }