Optimize DB model unit test

This commit is contained in:
huangzhhui 2019-02-01 09:58:52 +08:00
parent d9136269f3
commit f41949c711
2 changed files with 17 additions and 7 deletions

View File

@ -29,11 +29,13 @@ use Hyperf\Database\Model\Relations\Relation;
use Hyperf\Database\Query\Grammars\Grammar; use Hyperf\Database\Query\Grammars\Grammar;
use Hyperf\Database\Query\Processors\Processor; use Hyperf\Database\Query\Processors\Processor;
use Hyperf\Utils\Collection as BaseCollection; use Hyperf\Utils\Collection as BaseCollection;
use Hyperf\Utils\Context;
use Hyperf\Utils\InteractsWithTime; use Hyperf\Utils\InteractsWithTime;
use HyperfTest\Database\Stubs\DateModelStub; use HyperfTest\Database\Stubs\DateModelStub;
use HyperfTest\Database\Stubs\ModelCamelStub; use HyperfTest\Database\Stubs\ModelCamelStub;
use HyperfTest\Database\Stubs\ModelCastingStub; use HyperfTest\Database\Stubs\ModelCastingStub;
use HyperfTest\Database\Stubs\ModelDestroyStub; use HyperfTest\Database\Stubs\ModelDestroyStub;
use HyperfTest\Database\Stubs\ModelDynamicHiddenStub;
use HyperfTest\Database\Stubs\ModelDynamicVisibleStub; use HyperfTest\Database\Stubs\ModelDynamicVisibleStub;
use HyperfTest\Database\Stubs\ModelFindWithWritePdoStub; use HyperfTest\Database\Stubs\ModelFindWithWritePdoStub;
use HyperfTest\Database\Stubs\ModelSaveStub; use HyperfTest\Database\Stubs\ModelSaveStub;
@ -205,41 +207,48 @@ class ModelTest extends TestCase
public function testCreateMethodSavesNewModel() public function testCreateMethodSavesNewModel()
{ {
$_SERVER['__model.saved'] = false; Context::set('__model.saved', false);
$model = ModelSaveStub::create(['name' => 'taylor']); $model = ModelSaveStub::create(['name' => 'taylor']);
$this->assertTrue($_SERVER['__model.saved']); $this->assertTrue(Context::get('__model.saved'));
$this->assertEquals('taylor', $model->name); $this->assertEquals('taylor', $model->name);
} }
public function testMakeMethodDoesNotSaveNewModel() public function testMakeMethodDoesNotSaveNewModel()
{ {
$_SERVER['__model.saved'] = false; Context::set('__model.saved', false);
$model = ModelSaveStub::make(['name' => 'taylor']); $model = ModelSaveStub::make(['name' => 'taylor']);
$this->assertFalse($_SERVER['__model.saved']); $this->assertFalse(Context::get('__model.saved'));
$this->assertEquals('taylor', $model->name); $this->assertEquals('taylor', $model->name);
} }
public function testForceCreateMethodSavesNewModelWithGuardedAttributes() public function testForceCreateMethodSavesNewModelWithGuardedAttributes()
{ {
$_SERVER['__model.saved'] = false; Context::set('__model.saved', false);
$model = ModelSaveStub::forceCreate(['id' => 21]); $model = ModelSaveStub::forceCreate(['id' => 21]);
$this->assertTrue($_SERVER['__model.saved']); $this->assertTrue(Context::get('__model.saved'));
$this->assertEquals(21, $model->id); $this->assertEquals(21, $model->id);
} }
public function testFindMethodUseWritePdo() public function testFindMethodUseWritePdo()
{ {
ModelFindWithWritePdoStub::onWriteConnection()->find(1); ModelFindWithWritePdoStub::onWriteConnection()->find(1);
// Avoid 'This test did not perform any assertions' notice
$this->assertTrue(true);
} }
public function testDestroyMethodCallsQueryBuilderCorrectly() public function testDestroyMethodCallsQueryBuilderCorrectly()
{ {
ModelDestroyStub::destroy(1, 2, 3); ModelDestroyStub::destroy(1, 2, 3);
// Avoid 'This test did not perform any assertions' notice
$this->assertTrue(true);
} }
public function testDestroyMethodCallsQueryBuilderCorrectlyWithCollection() public function testDestroyMethodCallsQueryBuilderCorrectlyWithCollection()
{ {
ModelDestroyStub::destroy(new Collection([1, 2, 3])); ModelDestroyStub::destroy(new Collection([1, 2, 3]));
// Avoid 'This test did not perform any assertions' notice
$this->assertTrue(true);
} }
public function testWithMethodCallsQueryBuilderCorrectly() public function testWithMethodCallsQueryBuilderCorrectly()

View File

@ -17,6 +17,7 @@ use Hyperf\Database\ConnectionInterface as Connection;
use Hyperf\Database\Model\Model; use Hyperf\Database\Model\Model;
use Hyperf\Database\Query\Grammars\Grammar; use Hyperf\Database\Query\Grammars\Grammar;
use Hyperf\Database\Query\Processors\Processor; use Hyperf\Database\Query\Processors\Processor;
use Hyperf\Utils\Context;
use Mockery; use Mockery;
class ModelSaveStub extends Model class ModelSaveStub extends Model
@ -27,7 +28,7 @@ class ModelSaveStub extends Model
public function save(array $options = []) public function save(array $options = [])
{ {
$_SERVER['__eloquent.saved'] = true; Context::set('__model.saved', true);
} }
public function setIncrementing($value) public function setIncrementing($value)