CastsAttrTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Siam
  5. * Date: 2020/6/4
  6. * Time: 16:20
  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 EasySwoole\ORM\Tests\models\TestCastsModel;
  13. use PHPUnit\Framework\TestCase;
  14. class CastsAttrTest 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. public function testAdd()
  31. {
  32. $testUserModel = new TestCastsModel();
  33. $testUserModel->state = 0;
  34. $testUserModel->name = 'Siam';
  35. $testUserModel->age = 100;
  36. $testUserModel->addTime = date('Y-m-d H:i:s');
  37. $data = $testUserModel->save();
  38. $this->assertIsInt($data);
  39. $testUserModel = new TestCastsModel();
  40. $testUserModel->state = 1;
  41. $testUserModel->name = 'Siam222';
  42. $testUserModel->age = 100;
  43. $testUserModel->addTime = date('Y-m-d H:i:s');
  44. $data = $testUserModel->save();
  45. $this->assertIsInt($data);
  46. }
  47. public function testFloat()
  48. {
  49. $test = TestCastsModel::create()->get([
  50. 'state' => 0
  51. ]);
  52. $this->assertIsFloat($test->id);
  53. }
  54. public function testInt()
  55. {
  56. $test = TestCastsModel::create()->get([
  57. 'state' => 0
  58. ]);
  59. $this->assertIsInt($test->age);
  60. }
  61. public function testTimestamp()
  62. {
  63. $test = TestCastsModel::create()->get([
  64. 'state' => 0
  65. ]);
  66. $this->assertIsInt($test->addTime);
  67. }
  68. public function testBool()
  69. {
  70. $test = TestCastsModel::create()->get([
  71. 'state' => 0
  72. ]);
  73. $this->assertIsBool($test->state);
  74. }
  75. public function testString()
  76. {
  77. $test = TestCastsModel::create();
  78. $test->setAttr('test_string', 1);
  79. $this->assertIsString($test->test_string);
  80. }
  81. public function testJson()
  82. {
  83. $test = TestCastsModel::create();
  84. $test->setAttr('test_json', [
  85. 'name' => 'siam'
  86. ]);
  87. $this->assertInstanceOf(\stdClass::class, $test->test_json);
  88. }
  89. public function testArray()
  90. {
  91. $test = TestCastsModel::create();
  92. $test->setAttr('test_array', [
  93. 'name' => 'siam'
  94. ]);
  95. $this->assertIsArray($test->test_array);
  96. }
  97. public function testDate()
  98. {
  99. $test = TestCastsModel::create();
  100. $test->setAttr('test_date', time());
  101. $this->assertEquals(date("Y-m-d"), $test->test_date);
  102. }
  103. public function testDateTime()
  104. {
  105. $test = TestCastsModel::create();
  106. $test->setAttr('test_datetime', time());
  107. $this->assertEquals(date("Y-m-d H:i:s"), $test->test_datetime);
  108. }
  109. public function testDelete()
  110. {
  111. $res = TestCastsModel::create()->destroy(null, true);
  112. $this->assertIsInt($res);
  113. }
  114. }