在 Laravel 7.0 開發套件

前言

本文建立一個簡單的 Laravel 套件,並撰寫測試案例。

建立專案

建立專案。

1
mkdir calculator

專案目錄

專案目錄如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
|- calculator/
|- src/
|- Facades/
|- Calculator.php
|- Calculator.php
|- CalculatorServiceProvider.php
|- tests/
|- CalculatorTest.php
|- TestCase.php
|- vendor/
|- .gitignore
|- composer.json
|- composer.lock
|- phpunit.xml

配置文件

進到 calculator 資料夾,新增 composer.json 檔:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{
"name": "memochou1993/laravel-package-example",
"description": "",
"type": "library",
"authors": [
{
"name": "Memo Chou",
"email": "[email protected]"
}
],
"require": {
"php": "^7.4"
},
"require-dev": {
"orchestra/testbench": "^5.3"
},
"autoload": {
"psr-4": {
"MemoChou1993\\Calculator\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"MemoChou1993\\Calculator\\Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
"MemoChou1993\\Calculator\\CalculatorServiceProvider"
],
"aliases": {
"Calculator": "MemoChou1993\\Calculator\\Facades\\Calculator"
}
}
}
}

主程式

src 資料夾新增 Calculator.php 檔:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace MemoChou1993\Calculator;

class Calculator
{
/**
* @param int $a
* @param int $b
* @return int
*/
public function add(int $a, int $b): int
{
return $a + $b;
}
}

服務提供者

src 資料夾新增 CalculatorServiceProvider.php 檔:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
namespace MemoChou1993\Calculator;

use Illuminate\Support\ServiceProvider;

class CalculatorServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->singleton('calculator', function() {
return new Calculator();
});
}

/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}

類別參考

Facades 資料夾新增 Calculator.php 檔:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
namespace MemoChou1993\Calculator\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @method static int add(int $a, int $b)
*
* @see \MemoChou1993\Calculator\Calculator
*/
class Calculator extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'calculator';
}
}

測試

安裝 orchestra/testbench 套件,可以在測試時使用 Laravel 的方法,或者對套件中使用到的 Laravel 方法進行測試。

1
composer require "orchestra/testbench" --dev

tests 資料夾新增 TestCase.php 檔:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
namespace MemoChou1993\Calculator\Tests;

use Illuminate\Foundation\Application;
use MemoChou1993\Calculator\Facades\Calculator;
use MemoChou1993\Calculator\CalculatorServiceProvider;
use Orchestra\Testbench\TestCase as OrchestraTestCase;

class TestCase extends OrchestraTestCase
{
/**
* @param Application $app
* @return array
*/
protected function getPackageProviders($app)
{
return [
CalculatorServiceProvider::class,
];
}

/**
* @param Application $app
* @return array
*/
protected function getPackageAliases($app)
{
return [
'calculator' => Calculator::class,
];
}
}

tests 資料夾新增 CalculatorTest.php 檔:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace MemoChou1993\Calculator\Tests;

use MemoChou1993\Calculator\Facades\Calculator;

class CalculatorTest extends TestCase
{
/**
* @return void
*/
public function testAdd(): void
{
$this->assertEquals(3, Calculator::add(1, 2));
}
}

重新產生 Composer 自動載入檔案。

1
composer dump-autoload

執行測試。

1
2
./vendor/bin/phpunit
OK

程式碼

參考資料