|
| 1 | +<?php |
| 2 | +use Behat\Behat\Context\Context; |
| 3 | +use Behat\Behat\Hook\Scope\BeforeScenarioScope; |
| 4 | +use Behat\Gherkin\Node\PyStringNode; |
| 5 | +use WP_CLI\Tests\Context\FeatureContext as WPCLIFeatureContext; |
| 6 | + |
| 7 | +class SQLiteFeatureContext extends WPCLIFeatureContext implements Context { |
| 8 | + |
| 9 | + private $db; |
| 10 | + |
| 11 | + /** |
| 12 | + * @Given /^a SQL dump file named "([^"]*)" with content:$/ |
| 13 | + */ |
| 14 | + public function aSqlDumpFileNamedWithContent( $filename, PyStringNode $content ) { |
| 15 | + $this->create_file( $filename, $content ); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * @Then /^the SQLite database should contain a table named "([^"]*)"$/ |
| 20 | + */ |
| 21 | + public function theSqliteDatabaseShouldContainATableNamed( $tableName ) { |
| 22 | + $this->connectToDatabase(); |
| 23 | + $result = $this->db->query( "SELECT name FROM sqlite_master WHERE type='table' AND name='$tableName'" ); |
| 24 | + $row = $result->fetchArray(); |
| 25 | + if ( ! $row ) { |
| 26 | + throw new Exception( "Table '$tableName' not found in the database." ); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @Then /^the "([^"]*)" should contain a row with name "([^"]*)"$/ |
| 32 | + */ |
| 33 | + public function theTableShouldContainARowWithName( $tableName, $name ) { |
| 34 | + $this->connectToDatabase(); |
| 35 | + $result = $this->db->query( "SELECT * FROM $tableName WHERE name='$name'" ); |
| 36 | + $row = $result->fetchArray(); |
| 37 | + if ( ! $row ) { |
| 38 | + throw new Exception( "Row with name '$name' not found in table '$tableName'." ); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @Then /^the SQLite database should contain the imported data$/ |
| 44 | + */ |
| 45 | + public function theSqliteDatabaseShouldContainTheImportedData() { |
| 46 | + $this->connectToDatabase(); |
| 47 | + $result = $this->db->query( "SELECT name FROM sqlite_master WHERE type='table'" ); |
| 48 | + $tables = []; |
| 49 | + while ( $row = $result->fetchArray() ) { |
| 50 | + $tables[] = $row['name']; |
| 51 | + } |
| 52 | + if ( empty( $tables ) ) { |
| 53 | + throw new Exception( 'No tables found in the database after import.' ); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @Then /^the file "([^"]*)" should exist$/ |
| 59 | + */ |
| 60 | + public function theFileShouldExist( $filename ) { |
| 61 | + $full_path = $this->variables['RUN_DIR'] . '/' . $filename; |
| 62 | + if ( ! file_exists( $full_path ) ) { |
| 63 | + throw new Exception( "File does not exist: $full_path" ); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @BeforeScenario |
| 69 | + */ |
| 70 | + public function beforeScenario( BeforeScenarioScope $scope ) { |
| 71 | + parent::beforeScenario( $scope ); |
| 72 | + $this->variables['DB_TYPE'] = 'sqlite'; |
| 73 | + } |
| 74 | + |
| 75 | + private function connectToDatabase() { |
| 76 | + if ( ! $this->db ) { |
| 77 | + $run_dir = $this->variables['RUN_DIR']; |
| 78 | + $db_file = $run_dir . '/wp-content/database/.ht.sqlite'; |
| 79 | + $this->db = new SQLite3( $db_file ); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Create a file with given filename and content. |
| 85 | + * |
| 86 | + * @param string $filename |
| 87 | + * @param string|PyStringNode $content |
| 88 | + */ |
| 89 | + protected function create_file( $filename, $content ) { |
| 90 | + $content = $this->replace_variables( (string) $content ); |
| 91 | + $run_dir = $this->variables['RUN_DIR']; |
| 92 | + |
| 93 | + if ( strpos( $filename, '/' ) !== false ) { |
| 94 | + $subdir = dirname( $filename ); |
| 95 | + $run_dir .= "/$subdir"; |
| 96 | + if ( ! is_dir( $run_dir ) ) { |
| 97 | + mkdir( $run_dir, 0777, true ); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + $full_path = $run_dir . '/' . basename( $filename ); |
| 102 | + |
| 103 | + file_put_contents( $full_path, $content ); |
| 104 | + } |
| 105 | +} |
0 commit comments