2
0

DataBaseHelperTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace Test\PhpDevCommunity\PaperORM\Helper;
  3. use PhpDevCommunity\PaperORM\EntityManager;
  4. use PhpDevCommunity\PaperORM\Mapping\Column\BoolColumn;
  5. use PhpDevCommunity\PaperORM\Mapping\Column\DateTimeColumn;
  6. use PhpDevCommunity\PaperORM\Mapping\Column\JoinColumn;
  7. use PhpDevCommunity\PaperORM\Mapping\Column\PrimaryKeyColumn;
  8. use PhpDevCommunity\PaperORM\Mapping\Column\StringColumn;
  9. use PhpDevCommunity\PaperORM\Metadata\IndexMetadata;
  10. use PhpDevCommunity\PaperORM\PaperConnection;
  11. use Test\PhpDevCommunity\PaperORM\Entity\PostTest;
  12. use Test\PhpDevCommunity\PaperORM\Entity\UserTest;
  13. class DataBaseHelperTest
  14. {
  15. public static function init(EntityManager $entityManager, int $nbUsers = 5)
  16. {
  17. $connection = $entityManager->getConnection();
  18. $connection->close();
  19. $connection->connect();
  20. $platform = $entityManager->createDatabasePlatform();
  21. $platform->createTable('user', [
  22. new PrimaryKeyColumn('id'),
  23. new JoinColumn('post', 'last_post_id', 'id', PostTest::class, true),
  24. new StringColumn('firstname'),
  25. new StringColumn('lastname'),
  26. new StringColumn('email'),
  27. new StringColumn('password'),
  28. new BoolColumn('is_active'),
  29. new DateTimeColumn('created_at', 'created_at', true),
  30. ]);
  31. $platform->createTable('post', [
  32. new PrimaryKeyColumn('id'),
  33. new JoinColumn('user', 'user_id', 'id', UserTest::class, false),
  34. new StringColumn('title'),
  35. new StringColumn('content'),
  36. new DateTimeColumn('created_at', 'created_at', true),
  37. ]);
  38. $platform->createIndex(new IndexMetadata('post', 'idx_post_user_id', ['user_id']));
  39. $platform->createTable('tag', [
  40. new PrimaryKeyColumn('id'),
  41. new JoinColumn('post', 'post_id', 'id', PostTest::class),
  42. new StringColumn('name'),
  43. ]);
  44. $platform->createTable('comment', [
  45. new PrimaryKeyColumn('id'),
  46. new JoinColumn('post', 'post_id', 'id', PostTest::class),
  47. new StringColumn('body'),
  48. ]);
  49. for ($i = 0; $i <$nbUsers; $i++) {
  50. $user = [
  51. 'firstname' => 'John' . $i,
  52. 'lastname' => 'Doe' . $i,
  53. 'email' => $i . 'bqQpB@example.com',
  54. 'password' => 'password123',
  55. 'is_active' => true,
  56. 'created_at' => (new \DateTime())->format($platform->getSchema()->getDateTimeFormatString()),
  57. ];
  58. $stmt = $connection->getPdo()->prepare("INSERT INTO user (firstname, lastname, email, password, is_active, created_at) VALUES (:firstname, :lastname, :email, :password, :is_active, :created_at)");
  59. $stmt->execute([
  60. 'firstname' => $user['firstname'],
  61. 'lastname' => $user['lastname'],
  62. 'email' => $user['email'],
  63. 'password' => $user['password'],
  64. 'is_active' => $user['is_active'],
  65. 'created_at' => $user['created_at']
  66. ]);
  67. }
  68. //
  69. $nbPosts = $nbUsers - 1;
  70. for ($i = 0; $i < $nbPosts; $i++) {
  71. $id = uniqid('post_', true);
  72. $post = [
  73. 'user_id' => $i + 1,
  74. 'title' => 'Post ' . $id,
  75. 'content' => 'Content ' . $id,
  76. 'created_at' => (new \DateTime())->format($platform->getSchema()->getDateTimeFormatString()),
  77. ];
  78. $stmt = $connection->getPdo()->prepare("INSERT INTO post (user_id, title, content, created_at) VALUES (:user_id, :title, :content, :created_at)");
  79. $stmt->execute([
  80. 'user_id' => $post['user_id'],
  81. 'title' => $post['title'],
  82. 'content' => $post['content'],
  83. 'created_at' => $post['created_at']
  84. ]);
  85. $id = uniqid('post_', true);
  86. $post = [
  87. 'user_id' => $i + 1,
  88. 'title' => 'Post ' . $id,
  89. 'content' => 'Content ' . $id,
  90. ];
  91. $connection->executeStatement("INSERT INTO post (user_id, title, content) VALUES (
  92. '{$post['user_id']}',
  93. '{$post['title']}',
  94. '{$post['content']}'
  95. )");
  96. $lastId = $connection->getPdo()->lastInsertId();
  97. $connection->executeStatement('UPDATE user SET last_post_id = ' . $lastId . ' WHERE id = ' . $post['user_id']);
  98. }
  99. $nbTags = $nbPosts * 2;
  100. for ($i = 0; $i < $nbTags; $i++) {
  101. $id = uniqid('tag_', true);
  102. $tag = [
  103. 'post_id' => $i + 1,
  104. 'name' => 'Tag ' . $id,
  105. ];
  106. $connection->executeStatement("INSERT INTO tag (post_id, name) VALUES (
  107. '{$tag['post_id']}',
  108. '{$tag['name']} '
  109. )");
  110. $id = uniqid('tag_', true);
  111. $tag = [
  112. 'post_id' => $i + 1,
  113. 'name' => 'Tag ' . $id,
  114. ];
  115. $connection->executeStatement("INSERT INTO tag (post_id, name) VALUES (
  116. '{$tag['post_id']}',
  117. '{$tag['name']} '
  118. )");
  119. }
  120. $nbComments = $nbTags - 1;
  121. for ($i = 0; $i <$nbComments; $i++) {
  122. $id = uniqid('comment_', true);
  123. $comment = [
  124. 'post_id' => $i + 1,
  125. 'body' => 'Comment ' . $id,
  126. ];
  127. $connection->executeStatement("INSERT INTO comment (post_id, body) VALUES (
  128. '{$comment['post_id']}',
  129. '{$comment['body']} '
  130. )");
  131. $comment['body'] = 'Comment ' . $id . ' 2';
  132. $connection->executeStatement("INSERT INTO comment (post_id, body) VALUES (
  133. '{$comment['post_id']}',
  134. '{$comment['body']} '
  135. )");
  136. }
  137. }
  138. }