ClosureTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * 闭包使用测试
  4. * User: Administrator
  5. * Date: 2019/11/9 0009
  6. * Time: 13:27
  7. */
  8. namespace EasySwoole\ORM\Tests;
  9. use EasySwoole\Mysqli\QueryBuilder;
  10. use EasySwoole\ORM\Db\Config;
  11. use EasySwoole\ORM\Db\Connection;
  12. use EasySwoole\ORM\DbManager;
  13. use PHPUnit\Framework\TestCase;
  14. use EasySwoole\ORM\Tests\models\TestUserListModel;
  15. use EasySwoole\ORM\Tests\models\TestUserModel;
  16. class ClosureTest extends TestCase
  17. {
  18. /**
  19. * @var $connection Connection
  20. */
  21. protected $connection;
  22. protected $tableName = 'user_test_list';
  23. protected function setUp(): void
  24. {
  25. parent::setUp(); // TODO: Change the autogenerated stub
  26. $config = new Config(MYSQL_CONFIG);
  27. $this->connection = new Connection($config);
  28. DbManager::getInstance()->addConnection($this->connection);
  29. $connection = DbManager::getInstance()->getConnection();
  30. $this->assertTrue($connection === $this->connection);
  31. }
  32. public function testAdd()
  33. {
  34. $testUserModel = new TestUserModel();
  35. $testUserModel->state = 1;
  36. $testUserModel->name = 'Siam';
  37. $testUserModel->age = 100;
  38. $testUserModel->addTime = date('Y-m-d H:i:s');
  39. $data = $testUserModel->save();
  40. $this->assertIsInt($data);
  41. $testUserModel = new TestUserModel();
  42. $testUserModel->state = 1;
  43. $testUserModel->name = 'Siam222';
  44. $testUserModel->age = 100;
  45. $testUserModel->addTime = date('Y-m-d H:i:s');
  46. $data = $testUserModel->save();
  47. $this->assertIsInt($data);
  48. }
  49. public function testGet()
  50. {
  51. $user = TestUserModel::create()->get(function (QueryBuilder $queryBuilder) {
  52. $queryBuilder->where('name', 'Siam222');
  53. $queryBuilder->fields(['name', 'age']);
  54. });
  55. $this->assertNotEmpty($user->age);
  56. $this->assertEmpty($user->state);
  57. $this->assertEmpty($user->addTime);
  58. }
  59. public function testUpdate()
  60. {
  61. $test = TestUserModel::create()->update([
  62. 'age' => QueryBuilder::inc(3)
  63. ], [
  64. 'name' => 'Siam222'
  65. ]);
  66. $this->assertTrue($test);
  67. $user = TestUserModel::create()->get(function (QueryBuilder $queryBuilder) {
  68. $queryBuilder->where('name', 'Siam222');
  69. $queryBuilder->fields(['name', 'age']);
  70. });
  71. $this->assertEquals(103, $user->age);
  72. }
  73. public function testDeleteAll()
  74. {
  75. $res = TestUserListModel::create()->destroy(null, true);
  76. $this->assertIsInt($res);
  77. $test = TestUserModel::create()->destroy(null, true);
  78. $this->assertIsInt($test);
  79. }
  80. }