|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WP_CLI\Tests\Tests; |
| 4 | + |
| 5 | +use WP_CLI\Tests\TestCase; |
| 6 | +use WP_CLI\Utils; |
| 7 | + |
| 8 | +class TestBehatConfig extends TestCase { |
| 9 | + |
| 10 | + /** |
| 11 | + * @var string |
| 12 | + */ |
| 13 | + private $config_file = ''; |
| 14 | + |
| 15 | + protected function set_up(): void { |
| 16 | + parent::set_up(); |
| 17 | + |
| 18 | + $this->config_file = Utils\get_temp_dir() . uniqid( 'wp-cli-test-behat-config-', true ) . '.yml'; |
| 19 | + |
| 20 | + file_put_contents( |
| 21 | + $this->config_file, |
| 22 | + "default:\n suites:\n default:\n contexts: [ FeatureContext ]\n paths: [ features/a, features/b ]\n formatters:\n pretty: true\n" |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + protected function tear_down(): void { |
| 27 | + if ( file_exists( $this->config_file ) ) { |
| 28 | + unlink( $this->config_file ); |
| 29 | + } |
| 30 | + |
| 31 | + parent::tear_down(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Run the converter the way run-behat-tests does, from the root of this package. |
| 36 | + * |
| 37 | + * @param string|null $params Value of BEHAT_PARAMS, if any. |
| 38 | + * @return array<mixed> |
| 39 | + */ |
| 40 | + private function convert( $params = null ): array { |
| 41 | + $root = dirname( dirname( __DIR__ ) ); |
| 42 | + $command = 'cd ' . escapeshellarg( $root ) . ' && ' |
| 43 | + . ( null === $params ? 'unset BEHAT_PARAMS; ' : 'BEHAT_PARAMS=' . escapeshellarg( $params ) . ' ' ) |
| 44 | + . escapeshellarg( PHP_BINARY ) . ' utils/behat-config.php ' . escapeshellarg( $this->config_file ); |
| 45 | + |
| 46 | + $output = (string) shell_exec( $command ); |
| 47 | + |
| 48 | + if ( '' === $output ) { |
| 49 | + $this->markTestSkipped( 'The installed Behat reads behat.yml by itself.' ); |
| 50 | + } |
| 51 | + |
| 52 | + $config = json_decode( $output, true ); |
| 53 | + $this->assertIsArray( $config ); |
| 54 | + |
| 55 | + return $config; |
| 56 | + } |
| 57 | + |
| 58 | + public function test_default_profile_is_passed_on(): void { |
| 59 | + $this->assertSame( |
| 60 | + [ |
| 61 | + 'suites' => [ |
| 62 | + 'default' => [ |
| 63 | + 'contexts' => [ 'FeatureContext' ], |
| 64 | + 'paths' => [ 'features/a', 'features/b' ], |
| 65 | + ], |
| 66 | + ], |
| 67 | + 'formatters' => [ 'pretty' => true ], |
| 68 | + ], |
| 69 | + $this->convert() |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + public function test_existing_params_are_merged_over_the_profile(): void { |
| 74 | + $config = $this->convert( '{"formatters":{"progress":true}}' ); |
| 75 | + |
| 76 | + $this->assertSame( |
| 77 | + [ |
| 78 | + 'pretty' => true, |
| 79 | + 'progress' => true, |
| 80 | + ], |
| 81 | + $config['formatters'] |
| 82 | + ); |
| 83 | + $this->assertSame( [ 'FeatureContext' ], $config['suites']['default']['settings']['contexts'] ); |
| 84 | + } |
| 85 | + |
| 86 | + public function test_lists_are_replaced_as_a_whole(): void { |
| 87 | + $config = $this->convert( '{"suites":{"default":{"paths":["features/smoke"]}}}' ); |
| 88 | + $this->assertSame( [ 'features/smoke' ], $config['suites']['default']['settings']['paths'] ); |
| 89 | + |
| 90 | + $config = $this->convert( '{"suites":{"default":{"paths":[]}}}' ); |
| 91 | + $this->assertSame( [], $config['suites']['default']['settings']['paths'] ); |
| 92 | + } |
| 93 | + |
| 94 | + public function test_both_forms_of_suite_settings_are_reconciled(): void { |
| 95 | + $config = $this->convert( '{"suites":{"default":{"settings":{"paths":["features/smoke"]}}}}' ); |
| 96 | + |
| 97 | + $this->assertSame( |
| 98 | + [ |
| 99 | + 'settings' => [ |
| 100 | + 'contexts' => [ 'FeatureContext' ], |
| 101 | + 'paths' => [ 'features/smoke' ], |
| 102 | + ], |
| 103 | + ], |
| 104 | + $config['suites']['default'] |
| 105 | + ); |
| 106 | + } |
| 107 | +} |
0 commit comments