2
2
3
3
namespace Tests \Unit ;
4
4
5
+ use App \Services \TestService ;
5
6
use Tests \TestCase ;
6
7
use Illuminate \Foundation \Testing \RefreshDatabase ;
7
8
8
9
class ExampleTest extends TestCase
9
10
{
11
+ /**
12
+ * @var TestService
13
+ */
14
+ protected $ service ;
15
+
16
+ protected function setUp (): void
17
+ {
18
+ parent ::setUp ();
19
+ $ this ->service = new TestService ();
20
+ }
21
+
10
22
/**
11
23
* A basic test example.
12
24
*
@@ -16,4 +28,93 @@ public function testBasicTest()
16
28
{
17
29
$ this ->assertTrue (true );
18
30
}
31
+
32
+ public function testVariables ()
33
+ {
34
+ $ bool = false ;
35
+ $ number = 100 ;
36
+ $ arr = ['Laravel ' , 'PHP ' , 'test ' ];
37
+ $ obj = null ;
38
+
39
+ // 断言变量值是否为假,与 assertTrue 相对
40
+ $ this ->assertFalse ($ bool );
41
+ // 断言给定变量值是否与期望值相等,与 assertNotEquals 相对
42
+ $ this ->assertEquals (100 , $ number );
43
+ // 断言数组中是否包含给定值,与 assertNotContains 相对
44
+ $ this ->assertContains ('test ' , $ arr );
45
+ // 断言数组长度是否与期望值相等,与 assertNotCount 相对
46
+ $ this ->assertCount (3 , $ arr );
47
+ // 断言数组是否不会空,与 assertEmpty 相对
48
+ $ this ->assertNotEmpty ($ arr );
49
+ // 断言变量是否为 null,与 assertNotNull 相对
50
+ $ this ->assertNull ($ obj );
51
+ }
52
+
53
+ public function testOutput ()
54
+ {
55
+ $ this ->expectOutputString ('学院君 ' );
56
+ echo '学院君 ' ;
57
+ }
58
+
59
+ public function testOutputRegex ()
60
+ {
61
+ $ this ->expectOutputRegex ('/Laravel/i ' );
62
+ echo 'Laravel学院 ' ;
63
+ }
64
+
65
+ public function testException ()
66
+ {
67
+ $ this ->expectException (\Exception::class);
68
+ $ service = new TestService ();
69
+ $ service ->invalidArgument ();
70
+ }
71
+
72
+ /**
73
+ * @expectedException \InvalidArgumentException
74
+ */
75
+ public function testExceptionAnnotation ()
76
+ {
77
+ $ this ->service ->invalidArgument ();
78
+ }
79
+
80
+ /******************** 测试的依赖关系 start *******************/
81
+ public function testInitStack ()
82
+ {
83
+ $ this ->service ->init ();
84
+ $ this ->assertEquals (3 , $ this ->service ->getStackSize ());
85
+
86
+ return $ this ->service ;
87
+ }
88
+
89
+ /**
90
+ * @depends testInitStack
91
+ * @param TestService $service
92
+ */
93
+ public function testStackContains (TestService $ service )
94
+ {
95
+ $ contains = $ service ->stackContains ('学院君 ' );
96
+ $ this ->assertTrue ($ contains );
97
+ }
98
+ /******************** 测试的依赖关系 end *******************/
99
+
100
+ public function initDataProvider ()
101
+ {
102
+ return [
103
+ ['学院君1 ' ],
104
+ ['Laravel学院 ' ]
105
+ ];
106
+ }
107
+
108
+ /**
109
+ * @depends testInitStack
110
+ * @dataProvider initDataProvider
111
+ */
112
+ public function testIsStackContains ()
113
+ {
114
+ $ arguments = func_get_args ();
115
+ $ service = $ arguments [1 ];
116
+ $ value = $ arguments [0 ];
117
+ $ this ->assertTrue ($ service ->stackContains ($ value ));
118
+ }
119
+
19
120
}
0 commit comments