PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/features/steps/given.php

https://gitlab.com/Blueprint-Marketing/wp-cli
PHP | 156 lines | 130 code | 25 blank | 1 comment | 4 complexity | da6a51ca1e817d5fb26ee1db9affe474 MD5 | raw file
  1. <?php
  2. use Behat\Gherkin\Node\PyStringNode,
  3. Behat\Gherkin\Node\TableNode,
  4. WP_CLI\Process;
  5. $steps->Given( '/^an empty directory$/',
  6. function ( $world ) {
  7. $world->create_run_dir();
  8. }
  9. );
  10. $steps->Given( '/^an empty cache/',
  11. function ( $world ) {
  12. $world->variables['SUITE_CACHE_DIR'] = FeatureContext::create_cache_dir();
  13. }
  14. );
  15. $steps->Given( '/^an? ([^\s]+) file:$/',
  16. function ( $world, $path, PyStringNode $content ) {
  17. $content = (string) $content . "\n";
  18. $full_path = $world->variables['RUN_DIR'] . "/$path";
  19. Process::create( \WP_CLI\utils\esc_cmd( 'mkdir -p %s', dirname( $full_path ) ) )->run_check();
  20. file_put_contents( $full_path, $content );
  21. }
  22. );
  23. $steps->Given( '/^WP files$/',
  24. function ( $world ) {
  25. $world->download_wp();
  26. }
  27. );
  28. $steps->Given( '/^wp-config\.php$/',
  29. function ( $world ) {
  30. $world->create_config();
  31. }
  32. );
  33. $steps->Given( '/^a database$/',
  34. function ( $world ) {
  35. $world->create_db();
  36. }
  37. );
  38. $steps->Given( '/^a WP install$/',
  39. function ( $world ) {
  40. $world->install_wp();
  41. }
  42. );
  43. $steps->Given( "/^a WP install in '([^\s]+)'$/",
  44. function ( $world, $subdir ) {
  45. $world->install_wp( $subdir );
  46. }
  47. );
  48. $steps->Given( '/^a WP multisite install$/',
  49. function ( $world ) {
  50. $world->install_wp();
  51. $world->proc( 'wp core install-network', array( 'title' => 'WP CLI Network' ) )->run_check();
  52. }
  53. );
  54. $steps->Given( '/^these installed and active plugins:$/',
  55. function( $world, $stream ) {
  56. $plugins = implode( ' ', array_map( 'trim', explode( PHP_EOL, (string)$stream ) ) );
  57. $world->proc( "wp plugin install $plugins --activate" )->run_check();
  58. }
  59. );
  60. $steps->Given( '/^a custom wp-content directory$/',
  61. function ( $world ) {
  62. $wp_config_path = $world->variables['RUN_DIR'] . "/wp-config.php";
  63. $wp_config_code = file_get_contents( $wp_config_path );
  64. $world->move_files( 'wp-content', 'my-content' );
  65. $world->add_line_to_wp_config( $wp_config_code,
  66. "define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/my-content' );" );
  67. $world->move_files( 'my-content/plugins', 'my-plugins' );
  68. $world->add_line_to_wp_config( $wp_config_code,
  69. "define( 'WP_PLUGIN_DIR', __DIR__ . '/my-plugins' );" );
  70. file_put_contents( $wp_config_path, $wp_config_code );
  71. }
  72. );
  73. $steps->Given( '/^download:$/',
  74. function ( $world, TableNode $table ) {
  75. foreach ( $table->getHash() as $row ) {
  76. $path = $world->replace_variables( $row['path'] );
  77. if ( file_exists( $path ) ) {
  78. // assume it's the same file and skip re-download
  79. continue;
  80. }
  81. Process::create( \WP_CLI\Utils\esc_cmd( 'curl -sSL %s > %s', $row['url'], $path ) )->run_check();
  82. }
  83. }
  84. );
  85. $steps->Given( '/^save (STDOUT|STDERR) ([\'].+[^\'])?as \{(\w+)\}$/',
  86. function ( $world, $stream, $output_filter, $key ) {
  87. $stream = strtolower( $stream );
  88. if ( $output_filter ) {
  89. $output_filter = '/' . trim( str_replace( '%s', '(.+[^\b])', $output_filter ), "' " ) . '/';
  90. if ( false !== preg_match( $output_filter, $world->result->$stream, $matches ) )
  91. $output = array_pop( $matches );
  92. else
  93. $output = '';
  94. } else {
  95. $output = $world->result->$stream;
  96. }
  97. $world->variables[ $key ] = trim( $output, "\n" );
  98. }
  99. );
  100. $steps->Given( '/^a new Phar(?: with version "([^"]+)")$/',
  101. function ( $world, $version ) {
  102. $world->build_phar( $version );
  103. }
  104. );
  105. $steps->Given( '/^save the (.+) file ([\'].+[^\'])?as \{(\w+)\}$/',
  106. function ( $world, $filepath, $output_filter, $key ) {
  107. $full_file = file_get_contents( $world->replace_variables( $filepath ) );
  108. if ( $output_filter ) {
  109. $output_filter = '/' . trim( str_replace( '%s', '(.+[^\b])', $output_filter ), "' " ) . '/';
  110. if ( false !== preg_match( $output_filter, $full_file, $matches ) )
  111. $output = array_pop( $matches );
  112. else
  113. $output = '';
  114. } else {
  115. $output = $full_file;
  116. }
  117. $world->variables[ $key ] = trim( $output, "\n" );
  118. }
  119. );
  120. $steps->Given('/^a misconfigured WP_CONTENT_DIR constant directory$/',
  121. function($world) {
  122. $wp_config_path = $world->variables['RUN_DIR'] . "/wp-config.php";
  123. $wp_config_code = file_get_contents( $wp_config_path );
  124. $world->add_line_to_wp_config( $wp_config_code,
  125. "define( 'WP_CONTENT_DIR', '' );" );
  126. file_put_contents( $wp_config_path, $wp_config_code );
  127. }
  128. );