SetterGetterTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * 获取器、设置器
  4. * User: Siam
  5. * Date: 2019/10/31
  6. * Time: 9:35
  7. */
  8. namespace EasySwoole\ORM\Tests;
  9. use EasySwoole\ORM\Db\Config;
  10. use EasySwoole\ORM\Db\Connection;
  11. use EasySwoole\ORM\DbManager;
  12. use PHPUnit\Framework\TestCase;
  13. use EasySwoole\ORM\Tests\models\TestUserListGetterModel;
  14. class SetterGetterTest extends TestCase
  15. {
  16. /**
  17. * @var $connection Connection
  18. */
  19. protected $connection;
  20. protected $tableName = 'user_test_list';
  21. protected function setUp(): void
  22. {
  23. parent::setUp(); // TODO: Change the autogenerated stub
  24. $config = new Config(MYSQL_CONFIG);
  25. $this->connection = new Connection($config);
  26. DbManager::getInstance()->addConnection($this->connection);
  27. $connection = DbManager::getInstance()->getConnection();
  28. $this->assertTrue($connection === $this->connection);
  29. }
  30. // 插入的时候会调用setter
  31. public function testAdd()
  32. {
  33. $testUserModel = new TestUserListGetterModel();
  34. $testUserModel->state = 1;
  35. $testUserModel->name = 'Siam';
  36. $testUserModel->age = 18;
  37. $testUserModel->addTime = date('Y-m-d H:i:s');
  38. $data = $testUserModel->save();
  39. $this->assertIsInt($data);
  40. }
  41. // 查询结果不会调用setter
  42. // 获取会调用getter
  43. public function testGetter()
  44. {
  45. $test = TestUserListGetterModel::create()->all();
  46. $this->assertEquals($test[0]->addTime, 123);
  47. $test = TestUserListGetterModel::create()->get();
  48. $this->assertEquals($test['addTime'], 123);
  49. }
  50. public function testGetterJson()
  51. {
  52. $test = TestUserListGetterModel::create()->all();
  53. $json = json_encode($test);
  54. $decode = json_decode($json);
  55. $this->assertEquals($decode[0]->addTime, 123);
  56. }
  57. public function testDeleteAll()
  58. {
  59. $res = TestUserListGetterModel::create()->destroy(null, true);
  60. $this->assertIsInt($res);
  61. }
  62. }