DataBaseHelperTest.php 8.9 KB

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