StrictTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * 严格模式
  4. * User: haoxu
  5. * Date: 2019-10-30
  6. * Time: 18:07
  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\TestUserListModel;
  14. class StrictTest 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 testStrict()
  31. {
  32. $testUserModel = new TestUserListModel();
  33. $testUserModel->state = 1;
  34. $testUserModel->name = 'Siam';
  35. $testUserModel->age = 18;
  36. $testUserModel->addTime = date('Y-m-d H:i:s');
  37. $testUserModel->strict = 'strict';
  38. $array = $testUserModel->toArray();
  39. $this->assertArrayNotHasKey('strict', $array);
  40. }
  41. public function testUnStrict()
  42. {
  43. $testUserModel = new TestUserListModel();
  44. $testUserModel->state = 1;
  45. $testUserModel->name = 'Siam';
  46. $testUserModel->age = 18;
  47. $testUserModel->addTime = date('Y-m-d H:i:s');
  48. $testUserModel->strict = 'strict';
  49. $array = $testUserModel->toArray(false, false);
  50. $this->assertEquals($array['strict'], 'strict');
  51. }
  52. // 插入过滤数据
  53. public function testSaveFilter()
  54. {
  55. $model = TestUserListModel::create([
  56. 'state' => 1,
  57. 'name' => 'Siam',
  58. 'age' => 18,
  59. 'addTime' => date('Y-m-d H:i:s'),
  60. 'strict' => 'error',
  61. ]);
  62. $test = $model->save();
  63. $this->assertIsInt($test);
  64. }
  65. // 更新过滤数据
  66. public function testUpdateFilter()
  67. {
  68. $test = TestUserListModel::create()->get([
  69. 'state' => 1,
  70. 'name' => 'Siam',
  71. 'age' => 18,
  72. 'addTime' => date('Y-m-d H:i:s'),
  73. ]);
  74. $res = $test->update([
  75. 'age' => 19,
  76. 'strict' => 'error'
  77. ]);
  78. $this->assertTrue($res);
  79. $this->assertEquals($test->age, 19);
  80. }
  81. public function testDeleteAll()
  82. {
  83. $res = TestUserListModel::create()->destroy(null, true);
  84. $this->assertIsInt($res);
  85. }
  86. }