Skip to content

Commit d0cafb2

Browse files
author
Ivan Kerin
committed
Removed support for files, now operates directly with sql strings
1 parent ae2ba52 commit d0cafb2

File tree

2 files changed

+15
-32
lines changed

2 files changed

+15
-32
lines changed

src/Openbuildings/DBFixtures/Fixture.php

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,26 +78,26 @@ public function truncate_all()
7878
* @param string $file sql inserts file
7979
* @return Fixture $this
8080
*/
81-
public function replace($file)
81+
public function replace($sql)
8282
{
8383
$this->truncate_all();
8484

85-
$this->load($file);
85+
$this->load($sql);
8686

8787
return $this;
8888
}
8989

9090
/**
91-
* Dump the contents of the database as insert scripts
92-
* @param string $file
93-
* @return Fixture $this
91+
* Dump the contents of the database as insert statements
92+
*
93+
* @return string
9494
*/
95-
public function dump($file)
95+
public function dump()
9696
{
97-
$handle = fopen($file, 'w');
98-
9997
$pdo = $this->pdo();
10098

99+
$sql = '';
100+
101101
foreach ($this->list_tables() as $table)
102102
{
103103
$query = $pdo->query("SELECT * FROM `{$table}`");
@@ -111,13 +111,11 @@ public function dump($file)
111111
$values[] = $pdo->quote($column);
112112
}
113113

114-
fputs($handle, "INSERT INTO `{$table}` VALUES (".join(',', $values). ");\n");
114+
$sql .= "INSERT INTO `{$table}` VALUES (".join(',', $values). ");\n";
115115
}
116116
}
117117

118-
fclose($handle);
119-
120-
return $this;
118+
return $sql;
121119
}
122120

123121
/**
@@ -139,20 +137,9 @@ public function execute_import_files(array $files)
139137
* @param string $file
140138
* @return Fixture $this
141139
*/
142-
public function load($file)
140+
public function load($sql)
143141
{
144-
$handle = fopen($file, 'r');
145-
146-
while (($sql_line = fgets($handle)) !== FALSE)
147-
{
148-
if (trim($sql_line))
149-
{
150-
$this->pdo()->exec($sql_line);
151-
}
152-
}
153-
154-
fclose($handle);
155-
142+
$this->pdo()->exec($sql);
156143
$this->pdo()->exec('FLUSH TABLES');
157144

158145
return $this;

tests/tests/DatabaseTest.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,24 @@ public function test_connect()
1414
$database = new Fixture();
1515
$database->connect('mysql:host=localhost;dbname=test-db-fixtures', 'root');
1616

17-
$database->load(__DIR__.'/../test_data/database.sql');
17+
$database->load(file_get_contents(__DIR__.'/../test_data/database.sql'));
1818

1919
$this->assertEquals(array('table1', 'table2'), $database->list_tables());
2020

2121
$database->pdo()->exec('INSERT INTO table1 SET id = 1, name = "test1", description = "test test", price = 0.32');
2222
$database->pdo()->exec('INSERT INTO table1 SET id = 2, name = "test2", description = "test test2", price = 0.11');
2323

24-
$database->dump(__DIR__.'/../test_data/dumped.sql');
24+
$dumped = $database->dump();
2525

2626
$expected = <<<DUMPED
2727
INSERT INTO `table1` VALUES ('1','test1','test test','0.32');
2828
INSERT INTO `table1` VALUES ('2','test2','test test2','0.11');
2929
3030
DUMPED;
31-
32-
$dumped = file_get_contents(__DIR__.'/../test_data/dumped.sql');
3331

3432
$this->assertEquals($expected, $dumped);
3533

36-
unlink(__DIR__.'/../test_data/dumped.sql');
37-
38-
$database->replace(__DIR__.'/../test_data/replacement.sql');
34+
$database->replace(file_get_contents(__DIR__.'/../test_data/replacement.sql'));
3935

4036

4137
$database = new Fixture();

0 commit comments

Comments
 (0)