From 73180ceac463e80bcda2541a0b80a71a4ea508eb Mon Sep 17 00:00:00 2001 From: Francois Zaninotto Date: Fri, 10 Feb 2012 18:31:55 +0100 Subject: [PATCH] Fix Lorem::text() to take newlines into account. Closes #28 --- src/Faker/Provider/Lorem.php | 15 ++++++++------- test/Faker/Provider/LoremTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 test/Faker/Provider/LoremTest.php diff --git a/src/Faker/Provider/Lorem.php b/src/Faker/Provider/Lorem.php index 74b13604..4675719e 100644 --- a/src/Faker/Provider/Lorem.php +++ b/src/Faker/Provider/Lorem.php @@ -39,7 +39,7 @@ class Lorem extends \Faker\Provider\Base * @param integer $nbWords around how many words the sentence should contain * @return string */ - public function sentence($nbWords = 6) + public static function sentence($nbWords = 6) { $words = static::words($nbWords + mt_rand(-2, 2)); $words[0] = ucwords($words[0]); @@ -54,7 +54,7 @@ class Lorem extends \Faker\Provider\Base * @param integer $nb how many sentences to return * @return array */ - public function sentences($nb = 3) + public static function sentences($nb = 3) { $sentences = array(); for ($i=0; $i < $nb; $i++) { @@ -70,7 +70,7 @@ class Lorem extends \Faker\Provider\Base * @param integer $nbSentences around how many sentences the paragraph should contain * @return string */ - public function paragraph($nbSentences = 3) + public static function paragraph($nbSentences = 3) { return join(static::sentences($nbSentences + mt_rand(-1, 1)), ' '); } @@ -82,7 +82,7 @@ class Lorem extends \Faker\Provider\Base * @param integer $nb how many paragraphs to return * @return array */ - public function paragraphs($nb = 3) + public static function paragraphs($nb = 3) { $paragraphs = array(); for ($i=0; $i < $nb; $i++) { @@ -104,12 +104,13 @@ class Lorem extends \Faker\Provider\Base $text = array(); $size = 0; while ($size < $maxNbChars) { - $paragraph = self::paragraph(); + $paragraph = ($size ? "\n" : '') . static::paragraph(); $text []= $paragraph; $size += strlen($paragraph); } array_pop($text); - return join($text, "\n"); + return join($text, ''); } -} \ No newline at end of file +} + diff --git a/test/Faker/Provider/LoremTest.php b/test/Faker/Provider/LoremTest.php new file mode 100644 index 00000000..b4c09750 --- /dev/null +++ b/test/Faker/Provider/LoremTest.php @@ -0,0 +1,27 @@ +assertEquals($singleParagraph, $provider->text(51)); + $this->assertEquals($singleParagraph . "\n" . $singleParagraph, $provider->text(52)); + } +} + +class TestableLorem extends Lorem +{ + public static function paragraph($nbSentences = 3) + { + // 25 characters + return 'This is a test paragraph.'; + } +} \ No newline at end of file