You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.4 KiB

7 years ago
7 years ago
7 years ago
  1. <?php
  2. class UtilsTest extends PHPUnit_Framework_TestCase {
  3. public function testRandomString() {
  4. $str1 = p3k\random_string(20);
  5. $this->assertEquals(20, strlen($str1));
  6. $str2 = p3k\random_string(20);
  7. $this->assertEquals(20, strlen($str2));
  8. $this->assertNotEquals($str1, $str2);
  9. }
  10. public function testEndsWith() {
  11. $this->assertFalse(p3k\str_ends_with('abcdefg', ''));
  12. $this->assertFalse(p3k\str_ends_with('', 'abcdefg'));
  13. $this->assertTrue(p3k\str_ends_with('abcdefg', 'efg'));
  14. $this->assertTrue(p3k\str_ends_with('abcdefg', 'abcdefg'));
  15. $this->assertTrue(p3k\str_ends_with('abcdefg', 'g'));
  16. $this->assertFalse(p3k\str_ends_with('abcdefg', 'abc'));
  17. }
  18. /*
  19. * These are failing in php nightly with the error:
  20. * session_name(): Cannot change session name when headers already sent
  21. * despite not trying to change the session name, just read it.
  22. public function testSessionSetupNoCreate() {
  23. // no session already, so this should not create one
  24. p3k\session_setup();
  25. $this->assertFalse(isset($_SESSION));
  26. }
  27. public function testSessionSetupCreateFromCookie() {
  28. // there is a session cookie, so this should initialize the session
  29. $_COOKIE[session_name()] = '12345';
  30. p3k\session_setup();
  31. $this->assertTrue(isset($_SESSION));
  32. }
  33. */
  34. public function testSessionAccess() {
  35. $_SESSION = [];
  36. $this->assertNull(p3k\session('foo'));
  37. $_SESSION = [];
  38. $this->assertEquals('default', p3k\session('foo', 'default'));
  39. $_SESSION = ['foo'=>'bar'];
  40. $this->assertEquals('bar', p3k\session('foo'));
  41. }
  42. public function testFlash() {
  43. $_SESSION = [];
  44. $this->assertNull(p3k\flash('foo'));
  45. $_SESSION = ['foo'=>'bar'];
  46. $this->assertEquals('bar', p3k\flash('foo'));
  47. $this->assertNull(p3k\flash('foo'));
  48. }
  49. public function testE() {
  50. $html = p3k\e('<b>test</b>');
  51. $this->assertEquals('&lt;b&gt;test&lt;/b&gt;', $html);
  52. }
  53. public function testK() {
  54. $this->assertEquals('b', p3k\k(['a'=>'b'], 'a'));
  55. $this->assertEquals('default', p3k\k(['a'=>'b'], 'z', 'default'));
  56. $obj = new StdClass;
  57. $obj->a = 'b';
  58. $this->assertEquals('b', p3k\k($obj, 'a'));
  59. $this->assertEquals('default', p3k\k($obj, 'z', 'default'));
  60. $keys = ['a','b','c'];
  61. $values = ['a'=>true, 'b'=>true, 'c'=>true];
  62. $this->assertTrue(p3k\k($values, $keys));
  63. $keys = ['a','b','c'];
  64. $values = ['a'=>true, 'c'=>true];
  65. $this->assertFalse(p3k\k($values, $keys));
  66. }
  67. public function testHTTPHeaderCase() {
  68. $name = p3k\http_header_case('header-name');
  69. $this->assertEquals('Header-Name', $name);
  70. $name = p3k\http_header_case('HEADER-NAME');
  71. $this->assertEquals('Header-Name', $name);
  72. $name = p3k\http_header_case('hEaDeR-nAmE');
  73. $this->assertEquals('Header-Name', $name);
  74. $name = p3k\http_header_case('host');
  75. $this->assertEquals('Host', $name);
  76. $name = p3k\http_header_case('x-header-name');
  77. $this->assertEquals('X-Header-Name', $name);
  78. }
  79. public function testHTMLToDomDocument() {
  80. $doc = p3k\html_to_dom_document('<html><head><title>Title</title></head><body>Hello World</body></html>');
  81. $this->assertEquals('DOMDocument', get_class($doc));
  82. $this->assertEmpty(libxml_get_errors());
  83. $doc = p3k\html_to_dom_document("\0this is not HTML");
  84. $this->assertEquals('DOMDocument', get_class($doc));
  85. $this->assertEmpty(libxml_get_errors());
  86. }
  87. public function testXMLToDomDocument() {
  88. $doc = p3k\xml_to_dom_document('<html><head><title>Title</title></head><body>Hello World</body></html>');
  89. $this->assertEquals('DOMDocument', get_class($doc));
  90. $this->assertEmpty(libxml_get_errors());
  91. $doc = p3k\xml_to_dom_document('<html><title>Title</title></head><body>Hello World</html>');
  92. $this->assertEquals('DOMDocument', get_class($doc));
  93. $this->assertEmpty(libxml_get_errors());
  94. $doc = p3k\xml_to_dom_document("\0this is not XML");
  95. $this->assertEquals('DOMDocument', get_class($doc));
  96. $this->assertEmpty(libxml_get_errors());
  97. }
  98. public function testBase60() {
  99. $this->assertEquals('BBBB', p3k\b10to60(p3k\b60to10('BBBB')));
  100. $this->assertEquals('ABCD_efg', p3k\b10to60(p3k\b60to10('ABCD_efg')));
  101. $this->assertEquals('Z111000', p3k\b10to60(p3k\b60to10('ZIl1O0O')));
  102. $this->assertEquals('0', p3k\b10to60(p3k\b60to10(',<.')));
  103. }
  104. public function testCreatesRedis() {
  105. p3k\redis();
  106. $this->assertEquals('Predis\Client', get_class(p3k\redis()));
  107. }
  108. }