DataBaseHelperTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace Test\PhpDevCommunity\PaperORM\Helper;
  3. use DateTime;
  4. use PhpDevCommunity\PaperORM\EntityManagerInterface;
  5. use PhpDevCommunity\PaperORM\Generator\SchemaDiffGenerator;
  6. use PhpDevCommunity\PaperORM\Mapping\Column\BoolColumn;
  7. use PhpDevCommunity\PaperORM\Mapping\Column\DateTimeColumn;
  8. use PhpDevCommunity\PaperORM\Mapping\Column\JoinColumn;
  9. use PhpDevCommunity\PaperORM\Mapping\Column\PrimaryKeyColumn;
  10. use PhpDevCommunity\PaperORM\Mapping\Column\StringColumn;
  11. use Test\PhpDevCommunity\PaperORM\Entity\PostTest;
  12. use Test\PhpDevCommunity\PaperORM\Entity\UserTest;
  13. class DataBaseHelperTest
  14. {
  15. public static function drivers(): array
  16. {
  17. return [
  18. 'sqlite' => [
  19. 'driver' => 'sqlite',
  20. 'user' => null,
  21. 'password' => null,
  22. 'memory' => true,
  23. ],
  24. 'mariadb' => [
  25. 'driver' => 'mariadb',
  26. 'host' => getenv('MARIADB_HOST') ?: '127.0.0.1',
  27. 'port' => (int)(getenv('MARIADB_PORT') ?: 3306),
  28. 'dbname' => getenv('MARIADB_DB') ?: 'paper_orm_test',
  29. 'user' => getenv('MARIADB_USER') ?: 'root',
  30. 'password' => getenv('MARIADB_PASSWORD') ?: '',
  31. 'charset' => 'utf8mb4',
  32. ],
  33. ];
  34. }
  35. public static function init(EntityManagerInterface $entityManager, int $nbUsers = 5, bool $withPosts = true)
  36. {
  37. $userColumns = [
  38. new PrimaryKeyColumn('id'),
  39. (new JoinColumn('last_post_id', PostTest::class, 'id', true, true)),
  40. new StringColumn('firstname'),
  41. new StringColumn('lastname'),
  42. new StringColumn('email'),
  43. new StringColumn('password'),
  44. new BoolColumn('is_active'),
  45. new DateTimeColumn('created_at', true),
  46. ];
  47. $postColumns = [
  48. new PrimaryKeyColumn('id'),
  49. (new JoinColumn('user_id', UserTest::class, 'id', true, false, JoinColumn::SET_NULL)),
  50. new StringColumn('title'),
  51. new StringColumn('content'),
  52. new DateTimeColumn('created_at', true),
  53. ];
  54. $tagColumns = [
  55. new PrimaryKeyColumn('id'),
  56. (new JoinColumn('post_id', PostTest::class, 'id', true, false, JoinColumn::SET_NULL)),
  57. new StringColumn('name'),
  58. ];
  59. $commentColumns = [
  60. new PrimaryKeyColumn('id'),
  61. (new JoinColumn('post_id', PostTest::class, 'id', true, false, JoinColumn::SET_NULL)),
  62. new StringColumn('body'),
  63. ];
  64. $platform = $entityManager->createDatabasePlatform();
  65. $platform->createDatabaseIfNotExists();
  66. $platform->dropDatabase();
  67. $platform->createDatabaseIfNotExists();
  68. $statements = (new SchemaDiffGenerator($platform))->generateDiffStatements([
  69. 'user' => [
  70. 'columns' => $userColumns,
  71. 'indexes' => [],
  72. ],
  73. 'post' => [
  74. 'columns' => $postColumns,
  75. 'indexes' => [],
  76. ],
  77. 'tag' => [
  78. 'columns' => $tagColumns,
  79. 'indexes' => [],
  80. ],
  81. 'comment' => [
  82. 'columns' => $commentColumns,
  83. 'indexes' => [],
  84. ],
  85. ]
  86. );
  87. $connection = $entityManager->getConnection();
  88. $connection->close();
  89. $connection->connect();
  90. foreach ($statements['up'] as $statement) {
  91. $connection->executeStatement($statement);
  92. }
  93. for ($i = 0; $i < $nbUsers; $i++) {
  94. $user = [
  95. 'firstname' => 'John' . $i,
  96. 'lastname' => 'Doe' . $i,
  97. 'email' => $i . 'bqQpB@example.com',
  98. 'password' => 'password123',
  99. 'is_active' => true,
  100. 'created_at' => (new DateTime())->format($platform->getSchema()->getDateTimeFormatString()),
  101. ];
  102. $stmt = $connection->getPdo()->prepare("INSERT INTO user (firstname, lastname, email, password, is_active, created_at) VALUES (:firstname, :lastname, :email, :password, :is_active, :created_at)");
  103. $stmt->execute([
  104. 'firstname' => $user['firstname'],
  105. 'lastname' => $user['lastname'],
  106. 'email' => $user['email'],
  107. 'password' => $user['password'],
  108. 'is_active' => $user['is_active'],
  109. 'created_at' => $user['created_at']
  110. ]);
  111. }
  112. //
  113. if ($withPosts) {
  114. $nbPosts = $nbUsers - 1;
  115. for ($i = 0; $i < $nbPosts; $i++) {
  116. $id = uniqid('post_', true);
  117. $post = [
  118. 'user_id' => $i + 1,
  119. 'title' => 'Post ' . $id,
  120. 'content' => 'Content ' . $id,
  121. 'created_at' => (new DateTime())->format($platform->getSchema()->getDateTimeFormatString()),
  122. ];
  123. $stmt = $connection->getPdo()->prepare("INSERT INTO post (user_id, title, content, created_at) VALUES (:user_id, :title, :content, :created_at)");
  124. $stmt->execute([
  125. 'user_id' => $post['user_id'],
  126. 'title' => $post['title'],
  127. 'content' => $post['content'],
  128. 'created_at' => $post['created_at']
  129. ]);
  130. $id = uniqid('post_', true);
  131. $post = [
  132. 'user_id' => $i + 1,
  133. 'title' => 'Post ' . $id,
  134. 'content' => 'Content ' . $id,
  135. ];
  136. $connection->executeStatement("INSERT INTO post (user_id, title, content) VALUES (
  137. '{$post['user_id']}',
  138. '{$post['title']}',
  139. '{$post['content']}'
  140. )");
  141. $lastId = $connection->getPdo()->lastInsertId();
  142. $connection->executeStatement('UPDATE user SET last_post_id = ' . $lastId . ' WHERE id = ' . $post['user_id']);
  143. }
  144. $nbTags = $nbPosts * 2;
  145. for ($i = 0; $i < $nbTags; $i++) {
  146. $id = uniqid('tag_', true);
  147. $tag = [
  148. 'post_id' => $i + 1,
  149. 'name' => 'Tag ' . $id,
  150. ];
  151. $connection->executeStatement("INSERT INTO tag (post_id, name) VALUES (
  152. '{$tag['post_id']}',
  153. '{$tag['name']} '
  154. )");
  155. $id = uniqid('tag_', true);
  156. $tag = [
  157. 'post_id' => $i + 1,
  158. 'name' => 'Tag ' . $id,
  159. ];
  160. $connection->executeStatement("INSERT INTO tag (post_id, name) VALUES (
  161. '{$tag['post_id']}',
  162. '{$tag['name']} '
  163. )");
  164. }
  165. $nbComments = $nbTags - 1;
  166. for ($i = 0; $i < $nbComments; $i++) {
  167. $id = uniqid('comment_', true);
  168. $comment = [
  169. 'post_id' => $i + 1,
  170. 'body' => 'Comment ' . $id,
  171. ];
  172. $connection->executeStatement("INSERT INTO comment (post_id, body) VALUES (
  173. '{$comment['post_id']}',
  174. '{$comment['body']} '
  175. )");
  176. $comment['body'] = 'Comment ' . $id . ' 2';
  177. $connection->executeStatement("INSERT INTO comment (post_id, body) VALUES (
  178. '{$comment['post_id']}',
  179. '{$comment['body']} '
  180. )");
  181. }
  182. }
  183. }
  184. }