PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/plussed/library/metabox copy/tests/cmb-tests-base.php

https://gitlab.com/plusplusminus/compassion
PHP | 108 lines | 78 code | 18 blank | 12 comment | 7 complexity | 5c2f473a6663e3626f5bf99a2d9da558 MD5 | raw file
  1. <?php
  2. abstract class CMB2_Test extends WP_UnitTestCase {
  3. /**
  4. * Set up the test fixture
  5. */
  6. public function setUp() {
  7. parent::setUp();
  8. }
  9. public function tearDown() {
  10. parent::tearDown();
  11. }
  12. public function normalize_string( $string ) {
  13. return trim( preg_replace( array(
  14. '/[\t\n\r]/', // Remove tabs and newlines
  15. '/\s{2,}/', // Replace repeating spaces with one space
  16. '/> </', // Remove spaces between carats
  17. ), array(
  18. '',
  19. ' ',
  20. '><',
  21. ), $string ) );
  22. }
  23. public function is_connected() {
  24. $connected = @fsockopen( 'www.youtube.com', 80 );
  25. if ( $connected ){
  26. $is_conn = true;
  27. fclose( $connected );
  28. } else {
  29. $is_conn = false; //action in connection failure
  30. }
  31. return $is_conn;
  32. }
  33. protected function capture_render( $cb ) {
  34. ob_start();
  35. call_user_func( $cb );
  36. $output = ob_get_contents();
  37. ob_end_clean();
  38. return $output;
  39. }
  40. protected function render_field( $field ) {
  41. return $this->capture_render( array( $field, 'render_field' ) );
  42. }
  43. public function assertHTMLstringsAreEqual( $expected_string, $string_to_test ) {
  44. $expected_string = $this->normalize_string( $expected_string );
  45. $string_to_test = $this->normalize_string( $string_to_test );
  46. $compare = strcmp( $expected_string, $string_to_test );
  47. if ( 0 !== $compare ) {
  48. $compare = strspn( $expected_string ^ $string_to_test, "\0" );
  49. $chars_to_show = 50;
  50. $start = ( $compare - 5 );
  51. $pointer = '|--->>';
  52. $sep = "\n". str_repeat( '-', 75 );
  53. $compare = sprintf(
  54. $sep . "\nFirst difference at position %d:\n\n Expected: \t%s\n Actual: \t%s\n" . $sep,
  55. $compare,
  56. substr( $expected_string, $start, 5 ) . $pointer . substr( $expected_string, $compare, $chars_to_show ),
  57. substr( $string_to_test, $start, 5 ) . $pointer . substr( $string_to_test, $compare, $chars_to_show )
  58. );
  59. }
  60. return $this->assertEquals( $expected_string, $string_to_test, ! empty( $compare ) ? $compare : null );
  61. }
  62. public function assertIsDefined( $definition ) {
  63. return $this->assertTrue( defined( $definition ), "$definition is not defined." );
  64. }
  65. /**
  66. * Backport assertNotFalse to PHPUnit 3.6.12 which only runs in PHP 5.2
  67. *
  68. * @link https://github.com/woothemes/woocommerce/blob/f5ff10711dc664f1c5ec5277634a5eea2b828765/tests/framework/class-wc-unit-test-case.php
  69. *
  70. */
  71. public static function assertNotFalse( $condition, $message = '' ) {
  72. if ( version_compare( phpversion(), '5.3', '<' ) ) {
  73. self::assertThat( $condition, self::logicalNot( self::isFalse() ), $message );
  74. } else {
  75. parent::assertNotFalse( $condition, $message );
  76. }
  77. }
  78. /**
  79. * Backport assertContainsOnlyInstancesOf to PHPUnit 3.6.12 which only runs in PHP 5.2
  80. */
  81. public static function assertContainsOnlyInstancesOf( $classname, $haystack, $message = '' ) {
  82. if ( version_compare( phpversion(), '5.3', '<' ) ) {
  83. foreach ( $haystack as $to_check ) {
  84. self::assertInstanceOf( $classname, $to_check, $message );
  85. }
  86. } else {
  87. parent::assertContainsOnlyInstancesOf( $classname, $haystack, $message );
  88. }
  89. }
  90. }