ErrorTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * 错误测试
  4. * User: Siam
  5. * Date: 2019/11/22
  6. * Time: 14:47
  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\TestUserModel;
  16. /**
  17. * Class ErrorTest
  18. * @package EasySwoole\ORM\Tests
  19. */
  20. class ErrorTest 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. public function testGet()
  37. {
  38. try {
  39. $model = TestUserModel::create();
  40. $testUserModel = $model->where("xxx", 1)->get("");
  41. $this->assertFalse($testUserModel);
  42. if ($model->lastQueryResult()->getLastErrorNo() !== 0) {
  43. $this->assertIsString($model->lastQueryResult()->getLastError());
  44. }
  45. } catch (Exception $e) {
  46. $this->assertNotFalse(strpos($e->getMessage(), "SQLSTATE[42S22] [1054] Unknown column 'xxx' in 'where clause'"));
  47. } catch (\Throwable $e) {
  48. }
  49. }
  50. // 全部字段为null id自增
  51. public function testSaveNull()
  52. {// 没有准备表结构 本地临时测试通过
  53. // $model = TestUserListModel::create();
  54. // $res = $model->save();
  55. // $this->assertIsInt($res);
  56. }
  57. public function testThrow()
  58. {
  59. // 不存在的字段 where 抛出异常
  60. try {
  61. $test = TestUserModel::create()->get([
  62. 'fuck_life' => 1
  63. ]);
  64. } catch (Exception $e) {
  65. $this->assertEquals("SQLSTATE[42S22] [1054] Unknown column 'fuck_life' in 'where clause' [SELECT * FROM `test_user_model` WHERE `fuck_life` = 1 LIMIT 1]", $e->getMessage());
  66. } catch (\Throwable $e) {
  67. }
  68. }
  69. }