1
0
mirror of https://github.com/fzaninotto/Faker.git synced 2025-04-04 23:52:34 +02:00

Merge pull request #40 from geoffrey-brier/master

Fix issue #39
This commit is contained in:
Francois Zaninotto 2012-05-31 02:25:56 -07:00
commit f6162c88c9
2 changed files with 56 additions and 8 deletions

View File

@ -15,7 +15,7 @@ class Lorem extends \Faker\Provider\Base
{
return static::randomElement(static::$wordList);
}
/**
* Generate an array of random words
*
@ -31,7 +31,7 @@ class Lorem extends \Faker\Provider\Base
}
return $words;
}
/**
* Generate a random sentence
*
@ -41,12 +41,18 @@ class Lorem extends \Faker\Provider\Base
*/
public static function sentence($nbWords = 6)
{
$words = static::words($nbWords + mt_rand(-2, 2));
if ($nbWords <= 0) {
return '';
}
$nbWords = ($nbWords * mt_rand(60, 140) / 100) + 1;
$words = static::words($nbWords);
$words[0] = ucwords($words[0]);
return join($words, ' ') . '.';
}
/**
* Generate an array of sentences
*
@ -62,7 +68,7 @@ class Lorem extends \Faker\Provider\Base
}
return $sentences;
}
/**
* Generate a single paragraph
*
@ -72,9 +78,15 @@ class Lorem extends \Faker\Provider\Base
*/
public static function paragraph($nbSentences = 3)
{
return join(static::sentences($nbSentences + mt_rand(-1, 1)), ' ');
if ($nbSentences <= 0) {
return '';
}
$nbSentences = ($nbSentences * mt_rand(60, 140) / 100) + 1;
return join(static::sentences($nbSentences), ' ');
}
/**
* Generate an array of paragraphs
*

View File

@ -15,6 +15,42 @@ class LoremTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($singleParagraph, $provider->text(51));
$this->assertEquals($singleParagraph . "\n" . $singleParagraph, $provider->text(52));
}
public function testSentenceWithZeroNbWordsReturnsEmptyString()
{
$this->assertEquals('', Lorem::sentence(0));
}
public function testSentenceWithNegativeNbWordsReturnsEmptyString()
{
$this->assertEquals('', Lorem::sentence(-1));
}
public function testParagraphWithZeroNbSentencesReturnsEmptyString()
{
$this->assertEquals('', Lorem::paragraph(0));
}
public function testParagraphWithNegativeNbSentencesReturnsEmptyString()
{
$this->assertEquals('', Lorem::paragraph(-1));
}
public function testSentenceWithPositiveNbWordsReturnsAtLeastOneWord()
{
$sentence = Lorem::sentence(1);
$this->assertGreaterThan(1, strlen($sentence));
$this->assertGreaterThanOrEqual(1, count(explode(' ', $sentence)));
}
public function testParagraphWithPositiveNbSentencesReturnsAtLeastOneWord()
{
$paragraph = Lorem::paragraph(1);
$this->assertGreaterThan(1, strlen($paragraph));
$this->assertGreaterThanOrEqual(1, count(explode(' ', $paragraph)));
}
}
class TestableLorem extends Lorem