PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/features/steps/then.php

https://gitlab.com/Blueprint-Marketing/wp-cli
PHP | 182 lines | 133 code | 38 blank | 11 comment | 18 complexity | 914dc6aac6f9ac82110d265e99ae1923 MD5 | raw file
  1. <?php
  2. use Behat\Gherkin\Node\PyStringNode,
  3. Behat\Gherkin\Node\TableNode;
  4. $steps->Then( '/^the return code should be (\d+)$/',
  5. function ( $world, $return_code ) {
  6. if ( $return_code != $world->result->return_code ) {
  7. throw new RuntimeException( $world->result );
  8. }
  9. }
  10. );
  11. $steps->Then( '/^(STDOUT|STDERR) should (be|contain|not contain):$/',
  12. function ( $world, $stream, $action, PyStringNode $expected ) {
  13. $stream = strtolower( $stream );
  14. $expected = $world->replace_variables( (string) $expected );
  15. checkString( $world->result->$stream, $expected, $action, $world->result );
  16. }
  17. );
  18. $steps->Then( '/^(STDOUT|STDERR) should be a number$/',
  19. function ( $world, $stream ) {
  20. $stream = strtolower( $stream );
  21. assertNumeric( trim( $world->result->$stream, "\n" ) );
  22. }
  23. );
  24. $steps->Then( '/^(STDOUT|STDERR) should not be a number$/',
  25. function ( $world, $stream ) {
  26. $stream = strtolower( $stream );
  27. assertNotNumeric( trim( $world->result->$stream, "\n" ) );
  28. }
  29. );
  30. $steps->Then( '/^STDOUT should be a table containing rows:$/',
  31. function ( $world, TableNode $expected ) {
  32. $output = $world->result->stdout;
  33. $actual_rows = explode( "\n", rtrim( $output, "\n" ) );
  34. $expected_rows = array();
  35. foreach ( $expected->getRows() as $row ) {
  36. $expected_rows[] = $world->replace_variables( implode( "\t", $row ) );
  37. }
  38. compareTables( $expected_rows, $actual_rows, $output );
  39. }
  40. );
  41. $steps->Then( '/^STDOUT should end with a table containing rows:$/',
  42. function ( $world, TableNode $expected ) {
  43. $output = $world->result->stdout;
  44. $actual_rows = explode( "\n", rtrim( $output, "\n" ) );
  45. $expected_rows = array();
  46. foreach ( $expected->getRows() as $row ) {
  47. $expected_rows[] = $world->replace_variables( implode( "\t", $row ) );
  48. }
  49. $start = array_search( $expected_rows[0], $actual_rows );
  50. if ( false === $start )
  51. throw new \Exception( $world->result );
  52. compareTables( $expected_rows, array_slice( $actual_rows, $start ), $output );
  53. }
  54. );
  55. $steps->Then( '/^STDOUT should be JSON containing:$/',
  56. function ( $world, PyStringNode $expected ) {
  57. $output = $world->result->stdout;
  58. $expected = $world->replace_variables( (string) $expected );
  59. if ( !checkThatJsonStringContainsJsonString( $output, $expected ) ) {
  60. throw new \Exception( $world->result );
  61. }
  62. });
  63. $steps->Then( '/^STDOUT should be a JSON array containing:$/',
  64. function ( $world, PyStringNode $expected ) {
  65. $output = $world->result->stdout;
  66. $expected = $world->replace_variables( (string) $expected );
  67. $actualValues = json_decode( $output );
  68. $expectedValues = json_decode( $expected );
  69. $missing = array_diff( $expectedValues, $actualValues );
  70. if ( !empty( $missing ) ) {
  71. throw new \Exception( $world->result );
  72. }
  73. });
  74. $steps->Then( '/^STDOUT should be CSV containing:$/',
  75. function ( $world, TableNode $expected ) {
  76. $output = $world->result->stdout;
  77. $expected_rows = $expected->getRows();
  78. foreach ( $expected as &$row ) {
  79. foreach ( $row as &$value ) {
  80. $value = $world->replace_variables( $value );
  81. }
  82. }
  83. if ( ! checkThatCsvStringContainsValues( $output, $expected_rows ) )
  84. throw new \Exception( $world->result );
  85. }
  86. );
  87. $steps->Then( '/^(STDOUT|STDERR) should be empty$/',
  88. function ( $world, $stream ) {
  89. $stream = strtolower( $stream );
  90. if ( !empty( $world->result->$stream ) ) {
  91. throw new \Exception( $world->result );
  92. }
  93. }
  94. );
  95. $steps->Then( '/^(STDOUT|STDERR) should not be empty$/',
  96. function ( $world, $stream ) {
  97. $stream = strtolower( $stream );
  98. if ( '' === rtrim( $world->result->$stream, "\n" ) ) {
  99. throw new Exception( $world->result );
  100. }
  101. }
  102. );
  103. $steps->Then( '/^the (.+) (file|directory) should (exist|not exist|be:|contain:|not contain:)$/',
  104. function ( $world, $path, $type, $action, $expected = null ) {
  105. $path = $world->replace_variables( $path );
  106. // If it's a relative path, make it relative to the current test dir
  107. if ( '/' !== $path[0] )
  108. $path = $world->variables['RUN_DIR'] . "/$path";
  109. if ( 'file' == $type ) {
  110. $test = 'file_exists';
  111. } else if ( 'directory' == $type ) {
  112. $test = 'is_dir';
  113. }
  114. switch ( $action ) {
  115. case 'exist':
  116. if ( ! $test( $path ) ) {
  117. throw new Exception( $world->result );
  118. }
  119. break;
  120. case 'not exist':
  121. if ( $test( $path ) ) {
  122. throw new Exception( $world->result );
  123. }
  124. break;
  125. default:
  126. if ( ! $test( $path ) ) {
  127. throw new Exception( "$path doesn't exist." );
  128. }
  129. $action = substr( $action, 0, -1 );
  130. $expected = $world->replace_variables( (string) $expected );
  131. if ( 'file' == $type ) {
  132. $contents = file_get_contents( $path );
  133. } else if ( 'directory' == $type ) {
  134. $files = glob( rtrim( $path, '/' ) . '/*' );
  135. foreach( $files as &$file ) {
  136. $file = str_replace( $path . '/', '', $file );
  137. }
  138. $contents = implode( PHP_EOL, $files );
  139. }
  140. checkString( $contents, $expected, $action );
  141. }
  142. }
  143. );