Skip to content

Commit

Permalink
fix new line on string output,
Browse files Browse the repository at this point in the history
fix multiple fakeTemplate call
  • Loading branch information
niklongstone committed May 23, 2015
1 parent d307673 commit 85c28c0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 15 deletions.
9 changes: 9 additions & 0 deletions CHANGES.md
@@ -1,3 +1,12 @@
0.6.3 / 2015-05-23
==================
* Fix new line on string output,
* Fix multiple fakeTemplate call

0.6.2 / 2015-05-21
==================
* Downgrade doctrine/dbal

0.6.1 / 2015-05-19
==================
* Removed deprecated method fillEntity and fillTable
Expand Down
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -14,13 +14,13 @@ Fakerino is a fake data generator framework fully extensible.

###Main features
Fakerino can:
* Fakes simple data (e.g. name, surname, integer, text, ...).
* Fakes single data (e.g. name, surname, integer, text, ...).
* Fakes complex data (e.g. person: name, surname, hobby, country, ... ).
* Fakes data multiple times.
* Fakes a database table row/s automatically.
* Fakes a string or file template automatically (e.g. Hello Mr {{ surname }})
* Fakes a PHP Object.
* Supports JSON, Array and string output.
* Supports JSON, array and string output.

For more information about installation, functions, support, contribution, or other,
please read the __[Fakerino wiki](https://github.com/niklongstone/Fakerino/wiki)__.
Expand Down Expand Up @@ -70,3 +70,7 @@ print_r($fakerino->fake('fake1')->toArray());
`app/fake surname -l de-DE` //Schleßinger
`app/fake -s 'Hello Mrs {{namefemale}} {{surname}}' -l de-DE` //Hello Mrs Seeliger Ceylin
`app/fake -t tableName -c path/confix.xml -n 10` //Inserts 10 fake rows into tableName

#### Third parties

* [Symfony Fakerino](https://github.com/niklongstone/symfony-fakerino) - Symfony Fakerino Bundle
26 changes: 16 additions & 10 deletions src/Fakerino/Core/Console/FakeConsole.php
Expand Up @@ -44,13 +44,8 @@ public function __construct($input)

return;
}
$this->json = $this->getParam('-j');
$this->num = $this->getParam('-n', true);
$this->confFile = $this->getParam('-c', true);
$this->locale = $this->getParam('-l', true);
$this->help = $this->getParam('-h');
$this->table = $this->getParam('-t', true);
$this->templateSource = $this->getParam('-s', true);

$this->getParameters();
}

private function getParam($flag, $hasValue = false)
Expand Down Expand Up @@ -99,17 +94,28 @@ public function run()
return;
}
if ($this->templateSource) {
return $fakerino->num($this->num)->fakeTemplate($this->templateSource);
return $fakerino->num($this->num)->fakeTemplate($this->templateSource) . PHP_EOL;
}
$fakerino = $fakerino->fake($this->input)->num($this->num);

if ($this->json) {
$result = $fakerino->toJson() . PHP_EOL;
$result = $fakerino->toJson();
} else {
$result = (string) $fakerino;
}

return $result;
return $result . PHP_EOL;
}

private function getParameters()
{
$this->json = $this->getParam('-j');
$this->num = $this->getParam('-n', true);
$this->confFile = $this->getParam('-c', true);
$this->locale = $this->getParam('-l', true);
$this->help = $this->getParam('-h');
$this->table = $this->getParam('-t', true);
$this->templateSource = $this->getParam('-s', true);
}

private function showHelp()
Expand Down
12 changes: 9 additions & 3 deletions src/Fakerino/Core/FakeDataFactory.php
Expand Up @@ -137,10 +137,15 @@ public function fakeTemplate($file)
{
$this->template->loadTemplate($file);
$varsName = $this->template->getVariables();
$fakeData = $this->num(1)->fake($varsName)->toArray();
$data = array_combine(array_values($varsName), $fakeData);
$out = '';
$num = $this->num;
for ($i = 0; $i < $num; $i++) {
$fakeData = $this->num(1)->fake($varsName)->toArray();
$data = array_combine(array_values($varsName), $fakeData);
$out .= $this->template->render($data);
}

return $this->template->render($data);
return $out;
}

/**
Expand Down Expand Up @@ -185,6 +190,7 @@ public function __toString()
{
$this->startFake($this->startElement, $this->num);
array_walk_recursive($this->out, array($this, 'arrayToString'));
$this->outString = substr($this->outString, 0, -1);

return $this->outString;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Fakerino/Test/Core/FakeDataFactoryTest.php
Expand Up @@ -116,6 +116,7 @@ public function testMultipleFakesToString()
$num = 3;
$fakeString = (string) $this->fakeGenerator->fake('fake1')->num($num);
$lineExpected = count($this->conf['fake']['fake1']) * $num;
$lineExpected--;

$this->assertEquals($lineExpected, substr_count($fakeString, "\n"));
}
Expand Down Expand Up @@ -143,4 +144,24 @@ public function testFakeTemplateString()

$this->assertNotContains('{{ surname }}', $res);
}

public function testFakeTemplateStringMultipleTimes()
{
$num = 2;
$res = $this->fakeGenerator->num(2)->fakeTemplate('Hello {{ surname }},');

$this->assertEquals($num, substr_count($res, ","));
}

public function testDifferentCallsType()
{
$res1 = (string)$this->fakeGenerator->fake('integer');
$res2 = $this->fakeGenerator->fakeTemplate('Hello {{ surname }}');
$res3 = $this->fakeGenerator->fake('integer')->__toString();

$this->assertTrue(is_numeric($res1));
$this->assertInternalType('string', $res2);
$this->assertContains('Hello', $res2);
$this->assertTrue(is_numeric($res3));
}
}

0 comments on commit 85c28c0

Please sign in to comment.