EventTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * 回调事件测试
  4. * User: Siam
  5. * Date: 2019/12/10
  6. * Time: 9:42
  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 EasySwoole\ORM\Exception\Exception;
  14. use PHPUnit\Framework\TestCase;
  15. use EasySwoole\ORM\Tests\models\TestUserEventModel;
  16. /**
  17. * Class ErrorTest
  18. * @package EasySwoole\ORM\Tests
  19. */
  20. class EventTest extends TestCase
  21. {
  22. /**
  23. * @var $connection Connection
  24. */
  25. protected $connection;
  26. protected $tableName = 'user_test_list';
  27. protected function setUp(): void
  28. {
  29. parent::setUp(); // TODO: Change the autogenerated stub
  30. $config = new Config(MYSQL_CONFIG);
  31. $this->connection = new Connection($config);
  32. DbManager::getInstance()->addConnection($this->connection);
  33. $connection = DbManager::getInstance()->getConnection();
  34. $this->assertTrue($connection === $this->connection);
  35. }
  36. /**
  37. * @throws Exception
  38. * @throws \Throwable
  39. */
  40. public function testAdd()
  41. {
  42. $testUserModel = new TestUserEventModel();
  43. $testUserModel->state = 1;
  44. $testUserModel->name = 'Siam';
  45. $testUserModel->age = 100;
  46. $testUserModel->addTime = date('Y-m-d H:i:s');
  47. $data = $testUserModel->save();
  48. $this->assertFalse($data);
  49. // 更改为正常插入
  50. TestUserEventModel::$insert = true;
  51. $testUserModel = new TestUserEventModel();
  52. $testUserModel->state = 1;
  53. $testUserModel->name = 'Siam';
  54. $testUserModel->age = 100;
  55. $testUserModel->addTime = date('Y-m-d H:i:s');
  56. $data = $testUserModel->save();
  57. $this->assertIsInt($data);
  58. }
  59. /**
  60. * @throws Exception
  61. * @throws \EasySwoole\Mysqli\Exception\Exception
  62. * @throws \Throwable
  63. */
  64. public function testUpdate()
  65. {
  66. $model = TestUserEventModel::create()->get([
  67. 'name' => 'Siam',
  68. 'age' => 100
  69. ]);
  70. $model->age = 333;
  71. $res = $model->update();
  72. $this->assertFalse($res);
  73. TestUserEventModel::$update = true;
  74. $model = TestUserEventModel::create()->get([
  75. 'name' => 'Siam',
  76. 'age' => 100
  77. ]);
  78. $model->age = 102;
  79. $res = $model->update();
  80. $this->assertTrue($res);
  81. }
  82. /**
  83. * @throws Exception
  84. * @throws \Throwable
  85. */
  86. public function testDeleteAll()
  87. {
  88. $res = TestUserEventModel::create()->destroy(null, true);
  89. $this->assertFalse($res);
  90. TestUserEventModel::$delete = true;
  91. $res = TestUserEventModel::create()->destroy(null, true);
  92. $this->assertIsInt($res);
  93. }
  94. }