2
0

DataBaseHelperTest.php 7.6 KB

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