Skip to content

Commit

Permalink
Use semicolon as delimiter instead of newline (#4)
Browse files Browse the repository at this point in the history
* Use semicolon as delimiter instead of newline

* Change findings from code review

* Add some more spaces in the README
  • Loading branch information
TheBay0r authored Aug 7, 2019
1 parent f84602c commit 761ea51
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ final class MyFixture implements ConnectionFixtureInterface
public function load(Connection $connection): void
{
$connection->exec(
'insert into `rules`(`id`,`block_id`,`field`,`operator`,`value`) values (1100,1,"visits","lower_than","5")'
'insert into `rules` (`id`, `block_id`, `field`, `operator`, `value`) values (1100, 1, "visits", "lower_than" , "5")'
);
}
}
Expand All @@ -88,7 +88,7 @@ use Kununu\DataFixtures\Adapter\ConnectionSqlFixture;
final class MyFixtureSql extends ConnectionSqlFixture
{
protected function filesName(): array
protected function fileNames(): array
{
return [
__DIR__ . '/Sql/pages.sql',
Expand Down
9 changes: 6 additions & 3 deletions src/Adapter/ConnectionSqlFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,27 @@ abstract class ConnectionSqlFixture implements ConnectionFixtureInterface
{
final public function load(Connection $connection): void
{
foreach ($this->filesName() as $fileName) {
foreach ($this->fileNames() as $fileName) {
$file = new \SplFileInfo($fileName);

if ($file->getExtension() !== 'sql') {
continue;
}

foreach ($this->getSql($file) as $sql) {
if (empty($sql)) {
continue;
}
$connection->exec($sql);
}
}
}

abstract protected function filesName() : array;
abstract protected function fileNames() : array;

private function getSql(\SplFileInfo $fileInfo) : array
{
return array_values(array_filter(explode("\n", $this->getFileContents($fileInfo))));
return array_map('trim', (explode(";", $this->getFileContents($fileInfo))));
}

private function getFileContents(\SplFileInfo $fileInfo) : string
Expand Down
8 changes: 4 additions & 4 deletions tests/Adapter/ConnectionSqlFixtureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public function testLoad() : void
->expects($this->exactly(4))
->method('exec')
->withConsecutive(
['INSERT INTO `database`.`table` (`id`, `name`, `description`) VALUES (\'1\', \'name\', \'description\');'],
['INSERT INTO `database`.`table` (`id`, `name`, `description`) VALUES (\'2\', \'name2\', \'description2\');'],
['INSERT INTO `database`.`table` (`id`, `name`, `description`) VALUES (\'3\', \'name3\', \'description3\');'],
['INSERT INTO `database`.`table` (`id`, `name`, `description`) VALUES (\'4\', \'name4\', \'description4\');']
['INSERT INTO `database`.`table` (`id`, `name`, `description`) VALUES (\'1\', \'name\', \'description\')'],
['INSERT INTO `database`.`table` (`id`, `name`, `description`) VALUES (\'2\', \'name2\', \'description2\')'],
['INSERT INTO `database`.`table` (`id`, `name`, `description`) VALUES (\'3\', \'name3\', \'description3\')'],
['INSERT INTO `database`.`table` (`id`, `name`, `description`) VALUES (\'4\', \'name4\', \'description4\')']
);

$fixture = new ConnectionSqlFixture1();
Expand Down
2 changes: 1 addition & 1 deletion tests/Adapter/InvalidConnectionSqlFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class InvalidConnectionSqlFixture extends ConnectionSqlFixture
{
protected function filesName(): array
protected function fileNames(): array
{
return [
__DIR__ . '/fixture1.sql',
Expand Down
2 changes: 1 addition & 1 deletion tests/TestFixtures/ConnectionFixture3.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class ConnectionFixture3 extends ConnectionSqlFixture
{
protected function filesName(): array
protected function fileNames(): array
{
}
}
2 changes: 1 addition & 1 deletion tests/TestFixtures/ConnectionSqlFixture1.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class ConnectionSqlFixture1 extends ConnectionSqlFixture
{
protected function filesName(): array
protected function fileNames(): array
{
return [
__DIR__ . '/Sql/fixture1.sql',
Expand Down

0 comments on commit 761ea51

Please sign in to comment.