1
+ <?php
2
+
3
+ namespace App \Tests \Service ;
4
+
5
+ use App \Service \KnpUIpsum ;
6
+ use PHPUnit \Framework \TestCase ;
7
+
8
+ class KnpUIpsumTest extends TestCase
9
+ {
10
+ public function testGetWords ()
11
+ {
12
+ $ ipsum = new KnpUIpsum ();
13
+
14
+ $ words = $ ipsum ->getWords (1 );
15
+ $ this ->assertInternalType ('string ' , $ words );
16
+ $ this ->assertCount (1 , explode (' ' , $ words ));
17
+
18
+ $ words = $ ipsum ->getWords (10 );
19
+ $ this ->assertCount (10 , explode (' ' , $ words ));
20
+
21
+ $ words = $ ipsum ->getWords (10 , true );
22
+ $ this ->assertCount (10 , $ words );
23
+ }
24
+
25
+ public function testGetSentences ()
26
+ {
27
+ $ ipsum = new KnpUIpsum ();
28
+
29
+ $ text = $ ipsum ->getSentences (3 );
30
+ $ this ->assertEquals (3 , substr_count ($ text , '. ' ));
31
+ $ sentences = explode ('. ' , $ text );
32
+ // 3 items will be the sentences, then one final empty entry for the last period
33
+ $ this ->assertCount (4 , $ sentences );
34
+ // the second and third entries should start with a space
35
+ $ this ->assertEquals (' ' , $ sentences [1 ][0 ]);
36
+ $ this ->assertEquals (' ' , $ sentences [2 ][0 ]);
37
+ }
38
+
39
+ public function testGetParagraphs ()
40
+ {
41
+ // weird: using a loop because the results are random, and so
42
+ // they may pass several times by luck
43
+ for ($ i = 0 ; $ i < 100 ; $ i ++) {
44
+ $ ipsum = new KnpUIpsum ();
45
+ $ text = $ ipsum ->getParagraphs (3 );
46
+ $ paragraphs = explode ("\n\n" , $ text );
47
+ $ this ->assertCount (3 , $ paragraphs );
48
+
49
+ foreach ($ paragraphs as $ paragraph ) {
50
+ // default constructor args should give us 1 unicorn & 3 sunshines
51
+ $ this ->assertGreaterThanOrEqual (
52
+ 1 ,
53
+ substr_count (strtolower ($ paragraph ), 'unicorn ' ),
54
+ sprintf ('Text "%s" does not contain 1 unicorn ' , $ paragraph )
55
+ );
56
+ $ this ->assertGreaterThanOrEqual (
57
+ 3 ,
58
+ substr_count (strtolower ($ paragraph ), 'sunshine ' ),
59
+ sprintf ('Text "%s" does not contain 3 sunshine ' , $ paragraph )
60
+ );
61
+ }
62
+
63
+ $ i ++;
64
+ }
65
+ }
66
+ }
0 commit comments