DuplicateTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * @author gaobinzhan <gaobinzhan@gmail.com>
  4. */
  5. namespace EasySwoole\ORM\Tests;
  6. use EasySwoole\ORM\Db\Config;
  7. use EasySwoole\ORM\Db\Connection;
  8. use EasySwoole\ORM\DbManager;
  9. use EasySwoole\ORM\Exception\Exception;
  10. use EasySwoole\ORM\Tests\models\DuplicateModel;
  11. use PHPUnit\Framework\TestCase;
  12. class DuplicateTest extends TestCase
  13. {
  14. /**
  15. * @var $connection Connection
  16. */
  17. protected $connection;
  18. protected function setUp(): void
  19. {
  20. parent::setUp(); // TODO: Change the autogenerated stub
  21. $config = new Config(MYSQL_CONFIG);
  22. $config->setReturnCollection(true);
  23. $this->connection = new Connection($config);
  24. DbManager::getInstance()->addConnection($this->connection);
  25. $connection = DbManager::getInstance()->getConnection();
  26. $this->assertTrue($connection === $this->connection);
  27. }
  28. public function testAdd()
  29. {
  30. DuplicateModel::create()->destroy([], true);
  31. DuplicateModel::create()->data(['id' => 1, 'id1' => 1, 'nickname' => '史迪仔', 'nickname1' => '史迪奇'])->save();
  32. try {
  33. DuplicateModel::create()->data(['id' => 1, 'id1' => 1, 'nickname' => '史迪仔', 'nickname1' => '史迪奇'])->save();
  34. } catch (Exception $throwable) {
  35. $this->assertEquals(1062,$throwable->lastQueryResult()->getLastErrorNo());
  36. }
  37. DuplicateModel::create()->duplicate(['nickname' => '史迪奇'])->data(['id' => 1, 'id1' => 1, 'nickname' => '史迪仔', 'nickname1' => '史迪奇'])->save();
  38. $ret = DuplicateModel::create()->get(['id' => 1, 'id1' => 1])->toArray();
  39. $this->assertEquals('史迪奇', $ret['nickname']);
  40. $this->assertEquals('史迪奇', $ret['nickname1']);
  41. DuplicateModel::create()->duplicate(['nickname' => '史迪仔','nickname1' => '史迪仔'])->data(['id' => 1, 'id1' => 1, 'nickname' => '史迪仔', 'nickname1' => '史迪奇'])->save();
  42. $ret = DuplicateModel::create()->get(['id' => 1, 'id1' => 1])->toArray();
  43. $this->assertEquals('史迪仔', $ret['nickname']);
  44. $this->assertEquals('史迪仔', $ret['nickname1']);
  45. DuplicateModel::create()->duplicate(['nickname' => '史迪仔','nickname1' => '史迪仔'])->data(['id' => 1, 'id1' => 2, 'nickname' => '史迪仔', 'nickname1' => '史迪奇'])->save();
  46. $ret = DuplicateModel::create()->get(['id' => 1, 'id1' => 2])->toArray();
  47. $this->assertNotEquals('史迪奇', $ret['nickname']);
  48. $this->assertNotEquals('史迪仔', $ret['nickname1']);
  49. DuplicateModel::create()->destroy([], true);
  50. }
  51. }