LoremTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Faker\Test\Provider;
  3. use Faker\Provider\Lorem;
  4. use PHPUnit\Framework\TestCase;
  5. class LoremTest extends TestCase
  6. {
  7. /**
  8. * @expectedException \InvalidArgumentException
  9. */
  10. public function testTextThrowsExceptionWhenAskedTextSizeLessThan5()
  11. {
  12. Lorem::text(4);
  13. }
  14. public function testTextReturnsWordsWhenAskedSizeLessThan25()
  15. {
  16. $this->assertEquals('Word word word word.', TestableLorem::text(24));
  17. }
  18. public function testTextReturnsSentencesWhenAskedSizeLessThan100()
  19. {
  20. $this->assertEquals('This is a test sentence. This is a test sentence. This is a test sentence.', TestableLorem::text(99));
  21. }
  22. public function testTextReturnsParagraphsWhenAskedSizeGreaterOrEqualThanThan100()
  23. {
  24. $this->assertEquals('This is a test paragraph. It has three sentences. Exactly three.', TestableLorem::text(100));
  25. }
  26. public function testSentenceWithZeroNbWordsReturnsEmptyString()
  27. {
  28. $this->assertEquals('', Lorem::sentence(0));
  29. }
  30. public function testSentenceWithNegativeNbWordsReturnsEmptyString()
  31. {
  32. $this->assertEquals('', Lorem::sentence(-1));
  33. }
  34. public function testParagraphWithZeroNbSentencesReturnsEmptyString()
  35. {
  36. $this->assertEquals('', Lorem::paragraph(0));
  37. }
  38. public function testParagraphWithNegativeNbSentencesReturnsEmptyString()
  39. {
  40. $this->assertEquals('', Lorem::paragraph(-1));
  41. }
  42. public function testSentenceWithPositiveNbWordsReturnsAtLeastOneWord()
  43. {
  44. $sentence = Lorem::sentence(1);
  45. $this->assertGreaterThan(1, strlen($sentence));
  46. $this->assertGreaterThanOrEqual(1, count(explode(' ', $sentence)));
  47. }
  48. public function testParagraphWithPositiveNbSentencesReturnsAtLeastOneWord()
  49. {
  50. $paragraph = Lorem::paragraph(1);
  51. $this->assertGreaterThan(1, strlen($paragraph));
  52. $this->assertGreaterThanOrEqual(1, count(explode(' ', $paragraph)));
  53. }
  54. public function testWordssAsText()
  55. {
  56. $words = TestableLorem::words(2, true);
  57. $this->assertEquals('word word', $words);
  58. }
  59. public function testSentencesAsText()
  60. {
  61. $sentences = TestableLorem::sentences(2, true);
  62. $this->assertEquals('This is a test sentence. This is a test sentence.', $sentences);
  63. }
  64. public function testParagraphsAsText()
  65. {
  66. $paragraphs = TestableLorem::paragraphs(2, true);
  67. $expected = "This is a test paragraph. It has three sentences. Exactly three.\n\nThis is a test paragraph. It has three sentences. Exactly three.";
  68. $this->assertEquals($expected, $paragraphs);
  69. }
  70. }
  71. class TestableLorem extends Lorem
  72. {
  73. public static function word()
  74. {
  75. return 'word';
  76. }
  77. public static function sentence($nbWords = 5, $variableNbWords = true)
  78. {
  79. return 'This is a test sentence.';
  80. }
  81. public static function paragraph($nbSentences = 3, $variableNbSentences = true)
  82. {
  83. return 'This is a test paragraph. It has three sentences. Exactly three.';
  84. }
  85. }