CastsTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace EasySwoole\ORM\Tests;
  3. use EasySwoole\ORM\Db\Config;
  4. use EasySwoole\ORM\Db\Connection;
  5. use EasySwoole\ORM\DbManager;
  6. use EasySwoole\ORM\Tests\models\CastsModel;
  7. use PHPUnit\Framework\TestCase;
  8. class CastsTest extends TestCase
  9. {
  10. /**
  11. * @var $connection Connection
  12. */
  13. protected $connection;
  14. protected $data = [
  15. 'int_value' => '1',
  16. 'float_value' => '2',
  17. 'json_value' => ['a' => 1, 'b' => 2],
  18. 'array_value' => ['a' => 1, 'b' => 2],
  19. 'date_value' => '2020-11-27 15:08:25',
  20. 'datetime_value' => '2020-11-27 15:08:25',
  21. 'timestamp_value' => '1606460910'
  22. ];
  23. protected function setUp(): void
  24. {
  25. parent::setUp(); // TODO: Change the autogenerated stub
  26. $config = new Config(MYSQL_CONFIG);
  27. $config->setReturnCollection(true);
  28. $this->connection = new Connection($config);
  29. DbManager::getInstance()->addConnection($this->connection);
  30. $connection = DbManager::getInstance()->getConnection();
  31. $this->assertTrue($connection === $this->connection);
  32. }
  33. public function testInt()
  34. {
  35. $id = CastsModel::create()->data($this->data)->save();
  36. $this->assertIsInt(CastsModel::create()->get($id)->int_value);
  37. $this->assertIsInt(CastsModel::create()->get($id)->getAttr('int_value'));
  38. }
  39. public function testFloat()
  40. {
  41. $id = CastsModel::create()->data($this->data)->save();
  42. $this->assertIsFloat(CastsModel::create()->get($id)->float_value);
  43. $this->assertIsFloat(CastsModel::create()->get($id)->getAttr('float_value'));
  44. }
  45. public function testJson()
  46. {
  47. $id = CastsModel::create()->data($this->data)->save();
  48. $this->assertIsObject(CastsModel::create()->get($id)->json_value);
  49. $this->assertIsObject(CastsModel::create()->get($id)->getAttr('json_value'));
  50. }
  51. public function testArray()
  52. {
  53. $id = CastsModel::create()->data($this->data)->save();
  54. $this->assertIsArray(CastsModel::create()->get($id)->array_value);
  55. $this->assertIsArray(CastsModel::create()->get($id)->getAttr('array_value'));
  56. }
  57. }