Skip to content

Commit a95f602

Browse files
committed
add: phpunit 测试用例
1 parent da2a533 commit a95f602

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

laravel/app/Services/TestService.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
class TestService
6+
{
7+
8+
protected $stack = [];
9+
10+
public function init()
11+
{
12+
$this->stack = ['学院君', 'Laravel学院', '单元测试'];
13+
}
14+
15+
public function stackContains($value)
16+
{
17+
return in_array($value, $this->stack);
18+
}
19+
20+
public function getStackSize()
21+
{
22+
return count($this->stack);
23+
}
24+
25+
public function invalidArgument()
26+
{
27+
throw new \InvalidArgumentException('无效的参数');
28+
}
29+
}

laravel/tests/Unit/ExampleTest.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,23 @@
22

33
namespace Tests\Unit;
44

5+
use App\Services\TestService;
56
use Tests\TestCase;
67
use Illuminate\Foundation\Testing\RefreshDatabase;
78

89
class ExampleTest extends TestCase
910
{
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+
1022
/**
1123
* A basic test example.
1224
*
@@ -16,4 +28,93 @@ public function testBasicTest()
1628
{
1729
$this->assertTrue(true);
1830
}
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+
19120
}

0 commit comments

Comments
 (0)