Commit c7cc5e97 authored by 康帅杰's avatar 康帅杰 :speech_balloon:
Browse files

增加数据库事务

Showing with 153 additions and 5 deletions
+153 -5
<?php
/**
* @Author: kidkang
* @Date: 2021-03-08 11:34:36
* @Last Modified by: kidkang
* @Last Modified time: 2021-03-08 11:36:28
*/
namespace Yjtec\Repo;
use DB;
trait DBTrait
{
public function start()
{
DB::beginTransaction();
}
public function commit()
{
DB::commit();
}
public function rollback()
{
DB::rollBack();
}
public function transaction($callback)
{
DB::transaction($callback);
}
}
......@@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Model;
abstract class Repository
{
use DBTrait;
protected $app; //App容器
protected $model; //操作的model
public $ErrorCode; //错误码
......@@ -45,7 +46,7 @@ abstract class Repository
return $this->model->paginate($page);
}
public function list($where){
function list($where) {
return $this->model->where($where)->get();
}
public function add($data)
......
<?php
/**
* @Author: kidkang
* @Date: 2021-03-08 11:27:55
* @Last Modified by: kidkang
* @Last Modified time: 2021-03-08 13:37:17
*/
namespace Yjtec\Repo\Test;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model as BaseModel;
class Model extends BaseModel
{
use HasFactory;
protected $table = 'demo';
protected $fillable = ['name'];
}
......@@ -4,15 +4,71 @@
* @Author: kidkang
* @Date: 2021-03-02 18:04:27
* @Last Modified by: kidkang
* @Last Modified time: 2021-03-02 18:05:21
* @Last Modified time: 2021-03-08 13:47:45
*/
namespace Yjtec\Repo\Test;
use Yjtec\Repo\Repository;
class RepositoryTest extends TestCase
{
public function testExample()
public function setUp(): void
{
parent::setUp();
}
public function testDBevent()
{
$repo = $this->getRepo();
$repo->start();
$repo->commit();
$repo->rollback();
$this->assertTrue(true);
}
public function testTransaction()
{
$repo = $this->getRepo();
$repo->transaction(function () {
Model::create([
'name' => 'foo',
]);
});
$a = Model::where('name', 'foo')->first();
$this->assertEquals('foo', $a->name);
}
public function testTransactionWithException()
{
$repo = $this->getRepo();
$this->expectException(\Exception::class);
$repo->transaction(function () use ($repo) {
Model::create([
'name' => 'bar',
]);
$repo->throw();
});
}
protected function getRepo()
{
return new RepositoryDemo($this->app);
}
}
class RepositoryDemo extends Repository
{
/**
* Specify Model class name
*
* @return string
*/
public function model()
{
// TODO: Implement model() method.
return Model::class;
}
function throw () {
throw new \Exception('error');
}
}
......@@ -4,7 +4,7 @@
* @Author: kidkang
* @Date: 2021-03-02 18:01:31
* @Last Modified by: kidkang
* @Last Modified time: 2021-03-07 10:00:03
* @Last Modified time: 2021-03-08 13:36:49
*/
namespace Yjtec\Repo\Test;
......@@ -36,7 +36,15 @@ abstract class TestCase extends Orchestra
*/
protected function getEnvironmentSetUp($app)
{
$app['config']->set('database.default', 'sqlite');
$app['config']->set('database.connections.sqlite', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
$migraiont = include_once __DIR__ . '/migration.php';
(new \CreateDemosTable)->up();
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateDemosTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('demo', function (Blueprint $table) {
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('demo');
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment