CoherentJoinTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * 连贯操作之join
  4. * User: Siam
  5. * Date: 2019/10/28
  6. * Time: 10:10
  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. use EasySwoole\ORM\Tests\models\TestUserModel;
  15. class CoherentJoinTest extends TestCase
  16. {
  17. /**
  18. * @var $connection Connection
  19. */
  20. protected $connection;
  21. protected $tableName = 'user_test_list';
  22. protected function setUp(): void
  23. {
  24. parent::setUp(); // TODO: Change the autogenerated stub
  25. $config = new Config(MYSQL_CONFIG);
  26. $this->connection = new Connection($config);
  27. DbManager::getInstance()->addConnection($this->connection);
  28. $connection = DbManager::getInstance()->getConnection();
  29. $this->assertTrue($connection === $this->connection);
  30. }
  31. public function testAdd()
  32. {
  33. $testUserModel = new TestUserModel();
  34. $testUserModel->state = 1;
  35. $testUserModel->name = 'Siam';
  36. $testUserModel->age = 100;
  37. $testUserModel->addTime = date('Y-m-d H:i:s');
  38. $data = $testUserModel->save();
  39. $this->assertIsInt($data);
  40. $testUserModel = new TestUserModel();
  41. $testUserModel->state = 1;
  42. $testUserModel->name = 'Siam222';
  43. $testUserModel->age = 100;
  44. $testUserModel->addTime = date('Y-m-d H:i:s');
  45. $data = $testUserModel->save();
  46. $this->assertIsInt($data);
  47. $testUserModel = new TestUserListModel();
  48. $testUserModel->state = 2;
  49. $testUserModel->name = 'Siam';
  50. $testUserModel->age = 18;
  51. $testUserModel->addTime = date('Y-m-d H:i:s');
  52. $data = $testUserModel->save();
  53. $this->assertIsInt($data);
  54. }
  55. public function testJoin()
  56. {
  57. $userModel = TestUserModel::create();
  58. $res = $userModel->alias('u')->join('user_test_list as u_list', 'u_list.name = u.name')->where([
  59. 'u.name' => 'Siam'
  60. ])->all();
  61. $this->assertNotEmpty($res);
  62. }
  63. public function testDeleteAll()
  64. {
  65. $res = TestUserListModel::create()->destroy(null, true);
  66. $this->assertIsInt($res);
  67. $test = TestUserModel::create()->destroy(null, true);
  68. $this->assertIsInt($test);
  69. }
  70. }