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.

119 lines
4.2 KiB

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. public function testSessionSetupNoCreate() {
  19. // no session already, so this should not create one
  20. p3k\session_setup();
  21. $this->assertFalse(isset($_SESSION));
  22. }
  23. public function testSessionSetupCreateFromCookie() {
  24. // there is a session cookie, so this should initialize the session
  25. $_COOKIE[session_name()] = '12345';
  26. p3k\session_setup();
  27. $this->assertTrue(isset($_SESSION));
  28. }
  29. public function testSessionAccess() {
  30. $_SESSION = [];
  31. $this->assertNull(p3k\session('foo'));
  32. $_SESSION = [];
  33. $this->assertEquals('default', p3k\session('foo', 'default'));
  34. $_SESSION = ['foo'=>'bar'];
  35. $this->assertEquals('bar', p3k\session('foo'));
  36. }
  37. public function testFlash() {
  38. $_SESSION = [];
  39. $this->assertNull(p3k\flash('foo'));
  40. $_SESSION = ['foo'=>'bar'];
  41. $this->assertEquals('bar', p3k\flash('foo'));
  42. $this->assertNull(p3k\flash('foo'));
  43. }
  44. public function testE() {
  45. $html = p3k\e('<b>test</b>');
  46. $this->assertEquals('&lt;b&gt;test&lt;/b&gt;', $html);
  47. }
  48. public function testK() {
  49. $this->assertEquals('b', p3k\k(['a'=>'b'], 'a'));
  50. $this->assertEquals('default', p3k\k(['a'=>'b'], 'z', 'default'));
  51. $obj = new StdClass;
  52. $obj->a = 'b';
  53. $this->assertEquals('b', p3k\k($obj, 'a'));
  54. $this->assertEquals('default', p3k\k($obj, 'z', 'default'));
  55. $keys = ['a','b','c'];
  56. $values = ['a'=>true, 'b'=>true, 'c'=>true];
  57. $this->assertTrue(p3k\k($values, $keys));
  58. $keys = ['a','b','c'];
  59. $values = ['a'=>true, 'c'=>true];
  60. $this->assertFalse(p3k\k($values, $keys));
  61. }
  62. public function testHTTPHeaderCase() {
  63. $name = p3k\http_header_case('header-name');
  64. $this->assertEquals('Header-Name', $name);
  65. $name = p3k\http_header_case('HEADER-NAME');
  66. $this->assertEquals('Header-Name', $name);
  67. $name = p3k\http_header_case('hEaDeR-nAmE');
  68. $this->assertEquals('Header-Name', $name);
  69. $name = p3k\http_header_case('host');
  70. $this->assertEquals('Host', $name);
  71. $name = p3k\http_header_case('x-header-name');
  72. $this->assertEquals('X-Header-Name', $name);
  73. }
  74. public function testHTMLToDomDocument() {
  75. $doc = p3k\html_to_dom_document('<html><head><title>Title</title></head><body>Hello World</body></html>');
  76. $this->assertEquals('DOMDocument', get_class($doc));
  77. $this->assertEmpty(libxml_get_errors());
  78. $doc = p3k\html_to_dom_document("\0this is not HTML");
  79. $this->assertEquals('DOMDocument', get_class($doc));
  80. $this->assertEmpty(libxml_get_errors());
  81. }
  82. public function testXMLToDomDocument() {
  83. $doc = p3k\xml_to_dom_document('<html><head><title>Title</title></head><body>Hello World</body></html>');
  84. $this->assertEquals('DOMDocument', get_class($doc));
  85. $this->assertEmpty(libxml_get_errors());
  86. $doc = p3k\xml_to_dom_document('<html><title>Title</title></head><body>Hello World</html>');
  87. $this->assertEquals('DOMDocument', get_class($doc));
  88. $this->assertEmpty(libxml_get_errors());
  89. $doc = p3k\xml_to_dom_document("\0this is not XML");
  90. $this->assertEquals('DOMDocument', get_class($doc));
  91. $this->assertEmpty(libxml_get_errors());
  92. }
  93. public function testBase60() {
  94. $this->assertEquals('BBBB', p3k\b10to60(p3k\b60to10('BBBB')));
  95. $this->assertEquals('ABCD_efg', p3k\b10to60(p3k\b60to10('ABCD_efg')));
  96. $this->assertEquals('Z111000', p3k\b10to60(p3k\b60to10('ZIl1O0O')));
  97. $this->assertEquals('0', p3k\b10to60(p3k\b60to10(',<.')));
  98. }
  99. public function testCreatesRedis() {
  100. p3k\redis();
  101. $this->assertEquals('Predis\Client', get_class(p3k\redis()));
  102. }
  103. }