/tests/unit/CssCrush/UtilTest.php

https://github.com/peteboere/css-crush · PHP · 135 lines · 111 code · 22 blank · 2 comment · 0 complexity · 60c1c642e674f936ea66373620926b07 MD5 · raw file

  1. <?php
  2. namespace CssCrush\UnitTest;
  3. use CssCrush\Util;
  4. use CssCrush\Tokens;
  5. use CssCrush\Url;
  6. class UtilTest extends \PHPUnit\Framework\TestCase
  7. {
  8. public function setUp(): void
  9. {
  10. $this->process = bootstrap_process(['minify' => false]);
  11. $this->tokens = $this->process->tokens;
  12. }
  13. public function testNormalizePath()
  14. {
  15. $this->assertEquals('/Some/crazy/Path', Util::normalizePath('C:\\Some\crazy/Path\\', true));
  16. $this->assertEquals('/Some/crazy/Path', Util::normalizePath('/\Some//./crazy\\\/Path/'));
  17. $this->assertEquals('sane/path', Util::normalizePath('./sane/path/'));
  18. }
  19. public function testHtmlAttributes()
  20. {
  21. $attributes = [
  22. 'rel' => 'stylesheet',
  23. 'id' => 'foo',
  24. 'media' => 'screen',
  25. ];
  26. $this->assertEquals(
  27. ' rel="stylesheet" id="foo" media="screen"',
  28. Util::htmlAttributes($attributes));
  29. $this->assertEquals(
  30. ' id="foo" media="screen" rel="stylesheet"',
  31. Util::htmlAttributes($attributes, ['id', 'media', 'rel']));
  32. }
  33. public function testSimplifyPath()
  34. {
  35. $this->assertEquals('bar', Util::simplifyPath('foo/../bar'));
  36. $this->assertEquals('./../', Util::simplifyPath('./foo/../bar/../../'));
  37. }
  38. public function testVlqEncode()
  39. {
  40. $this->assertEquals('A', Util::vlqEncode(0));
  41. $this->assertEquals('C', Util::vlqEncode(1));
  42. $this->assertEquals('gB', Util::vlqEncode(16));
  43. $this->assertEquals('6H', Util::vlqEncode(125));
  44. $this->assertEquals('qmC', Util::vlqEncode(1125));
  45. }
  46. public function testStripCommentTokens()
  47. {
  48. $this->assertEquals('', Util::stripCommentTokens('?ca??cb?'));
  49. }
  50. public function testResolveUserPath()
  51. {
  52. $this->assertEquals(__FILE__, Util::resolveUserPath(__FILE__));
  53. $this->assertFalse(Util::resolveUserPath(__FILE__ . 'nothing'));
  54. // Relative path resolution.
  55. $original_path = getcwd();
  56. chdir(__DIR__);
  57. $this_filename = basename(__FILE__);
  58. // Case-insensitive file systems may normalize case.
  59. $this->assertEquals(strtolower(__FILE__), strtolower(Util::resolveUserPath($this_filename)));
  60. chdir($original_path);
  61. }
  62. public function testNormalizeWhiteSpace()
  63. {
  64. $this->assertEquals(
  65. '.foo[class]{rgb(0,0,0);}',
  66. Util::normalizeWhiteSpace(".foo[ class ] { \t rgb( \t0\n , 0, 0\r\n ) ; } "));
  67. }
  68. public function testSplitDelimList()
  69. {
  70. $this->assertEquals(['foo(1,2)','3','4'], Util::splitDelimList("foo(1,2), 3,4"));
  71. $this->assertEquals([], Util::splitDelimList(" ; ; ", ['delim' => ';']));
  72. $this->assertEquals(['', ''], Util::splitDelimList(" , ", ['allow_empty_strings' => true]));
  73. }
  74. public function testGetLinkBetweenPaths()
  75. {
  76. $path1 = __DIR__;
  77. $path2 = realpath(__DIR__ . '/../../');
  78. $this->assertEquals('../../', Util::getLinkBetweenPaths($path1, $path2));
  79. $this->assertEqualsIgnoringCase('Unit/CssCrush/', Util::getLinkBetweenPaths($path2, $path1));
  80. }
  81. public function testFilePutContents()
  82. {
  83. $test_file = sys_get_temp_dir() . '/' . str_replace('\\', '_', __CLASS__);
  84. $this->assertTrue(Util::filePutContents($test_file, 'Hello Mum'));
  85. }
  86. public function testRawValue()
  87. {
  88. $url1 = $this->tokens->add(new Url('foo.jpg'));
  89. $url2 = $this->tokens->add(new Url('foo.jpg'));
  90. $this->assertNotEquals($url1, $url2);
  91. $this->assertEquals(Util::rawValue($url1), Util::rawValue($url2));
  92. $this->assertEquals(Util::rawValue($url1), 'foo.jpg');
  93. $string1 = $this->tokens->add('"bar"', 's');
  94. $string2 = $this->tokens->add('"bar"', 's');
  95. $this->assertNotEquals($string1, $string2);
  96. $this->assertEquals(Util::rawValue($string1), Util::rawValue($string2));
  97. $this->assertEquals(Util::rawValue($string1), '"bar"');
  98. $this->assertEquals(Util::rawValue('foobar'), 'foobar');
  99. $this->assertNotEquals(Util::rawValue('foobar'), 'notFoobar');
  100. }
  101. public function testReadConfigFile()
  102. {
  103. $contents = <<<'NOW_DOC'
  104. <?php
  105. $plugins = ['svg', 'px2em'];
  106. $boilerplate = true;
  107. $unrecognised_option = true;
  108. NOW_DOC;
  109. $options = Util::readConfigFile(temp_file($contents));
  110. $this->assertArrayHasKey('plugins', $options);
  111. $this->assertArrayNotHasKey('unrecognised_option', $options);
  112. }
  113. }