From a84b879240577f99414ee026a68a05564bc81ac4 Mon Sep 17 00:00:00 2001 From: Julian PEREZ-RAMIREZ <julian.perez-ramirez@imt-atlantique.net> Date: Tue, 25 Feb 2025 22:59:48 +0100 Subject: [PATCH] editing fixtures files to created sample records --- web_app/src/DataFixtures/ItemFixtures.php | 46 +++++++++++++- web_app/src/DataFixtures/UserFixtures.php | 57 ++++++++++++++++- web_app/src/DataFixtures/WishListFixtures.php | 63 ++++++++++++++++++- 3 files changed, 158 insertions(+), 8 deletions(-) diff --git a/web_app/src/DataFixtures/ItemFixtures.php b/web_app/src/DataFixtures/ItemFixtures.php index e0f1985..a8dcc0c 100644 --- a/web_app/src/DataFixtures/ItemFixtures.php +++ b/web_app/src/DataFixtures/ItemFixtures.php @@ -2,6 +2,7 @@ namespace App\DataFixtures; +use App\Entity\Item; use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Persistence\ObjectManager; @@ -9,8 +10,49 @@ class ItemFixtures extends Fixture { public function load(ObjectManager $manager): void { - // $product = new Product(); - // $manager->persist($product); + $itemsData = [ + [ + 'title' => 'Smartphone', + 'description' => 'Latest model with high-end specs', + 'price' => 799.99, + 'purchase_url' => 'https://example.com/smartphone', + ], + [ + 'title' => 'Laptop', + 'description' => 'Powerful laptop for work and gaming', + 'price' => 1299.49, + 'purchase_url' => 'https://example.com/laptop', + ], + [ + 'title' => 'Wireless Headphones', + 'description' => 'Noise-canceling headphones with long battery life', + 'price' => 199.99, + 'purchase_url' => 'https://example.com/headphones', + ], + [ + 'title' => 'Coffee Maker', + 'description' => 'Automatic coffee maker with multiple settings', + 'price' => 89.99, + 'purchase_url' => 'https://example.com/coffee-maker', + ], + [ + 'title' => 'Gaming Chair', + 'description' => 'Ergonomic chair for long gaming sessions', + 'price' => 249.99, + 'purchase_url' => 'https://example.com/gaming-chair', + ] + ]; + + foreach ($itemsData as $itemData) { + $item = new Item(); + $item->setTitle($itemData['title']); + $item->setDescription($itemData['description']); + $item->setPrice($itemData['price']); + $item->setPurchaseUrl($itemData['purchase_url']); + $item->setCreatedAt(new \DateTimeImmutable()); + + $manager->persist($item); + } $manager->flush(); } diff --git a/web_app/src/DataFixtures/UserFixtures.php b/web_app/src/DataFixtures/UserFixtures.php index 987f6fe..fdb357f 100644 --- a/web_app/src/DataFixtures/UserFixtures.php +++ b/web_app/src/DataFixtures/UserFixtures.php @@ -2,15 +2,66 @@ namespace App\DataFixtures; +use App\Entity\User; +use App\Enum\UserRole; use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Persistence\ObjectManager; +use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; -class AppFixtures extends Fixture +class UserFixtures extends Fixture { + private UserPasswordHasherInterface $passwordHasher; + + public function __construct(UserPasswordHasherInterface $passwordHasher) + { + $this->passwordHasher = $passwordHasher; + } + public function load(ObjectManager $manager): void { - // $product = new Product(); - // $manager->persist($product); + $usersData = [ + [ + 'user_name' => 'john_doe', + 'name' => 'John', + 'surname' => 'Doe', + 'email' => 'john.doe@example.com', + 'password' => 'password123', + 'is_blocked' => false, + 'role' => UserRole::ADMIN, + ], + [ + 'user_name' => 'jane_smith', + 'name' => 'Jane', + 'surname' => 'Smith', + 'email' => 'jane.smith@example.com', + 'password' => 'securepass', + 'is_blocked' => false, + 'role' => UserRole::USER, + ], + [ + 'user_name' => 'bob_martin', + 'name' => 'Bob', + 'surname' => 'Martin', + 'email' => 'bob.martin@example.com', + 'password' => 'test123', + 'is_blocked' => true, + 'role' => UserRole::USER, + ] + ]; + + foreach ($usersData as $userData) { + $user = new User(); + $user->setUserName($userData['user_name']); + $user->setName($userData['name']); + $user->setSurname($userData['surname']); + $user->setEmail($userData['email']); + $user->setPassword($this->passwordHasher->hashPassword($user, $userData['password'])); + $user->setIsBlocked($userData['is_blocked']); + $user->setRole($userData['role']); + $user->setCreatedAt(new \DateTimeImmutable()); + + $manager->persist($user); + } $manager->flush(); } diff --git a/web_app/src/DataFixtures/WishListFixtures.php b/web_app/src/DataFixtures/WishListFixtures.php index c6a39ab..cd735e6 100644 --- a/web_app/src/DataFixtures/WishListFixtures.php +++ b/web_app/src/DataFixtures/WishListFixtures.php @@ -2,16 +2,73 @@ namespace App\DataFixtures; +use App\Entity\User; +use App\Entity\Item; +use App\Entity\WishList; use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Persistence\ObjectManager; +use Doctrine\Common\DataFixtures\DependentFixtureInterface; -class WishListFixtures extends Fixture +class WishListFixtures extends Fixture implements DependentFixtureInterface { public function load(ObjectManager $manager): void { - // $product = new Product(); - // $manager->persist($product); + // Retrieve all users + $users = $manager->getRepository(User::class)->findAll(); + // Retrieve all items + $items = $manager->getRepository(Item::class)->findAll(); + + if (empty($users) || empty($items)) { + throw new \Exception('No users or items found. Please load UserFixtures and ItemFixtures first.'); + } + + $wishListsData = [ + [ + 'name' => 'John\'s Birthday Wishlist', + 'user' => $users[0], // Assuming at least one user exists + 'items' => [$items[0], $items[1]], // Adding first two items + 'expiration_date' => new \DateTime('+30 days'), + 'is_active' => true, + ], + [ + 'name' => 'Tech Gadgets Wishlist', + 'user' => $users[1], // Use second user if available, else first user + 'items' => [$items[2], $items[3], $items[4]], // Adding three different items + 'expiration_date' => new \DateTime('+60 days'), + 'is_active' => true, + ], + [ + 'name' => 'Home Essentials Wishlist', + 'user' => $users[2], // Use third user if available, else first user + 'items' => [$items[1], $items[4]], // Adding two different items + 'expiration_date' => new \DateTime('+90 days'), + 'is_active' => false, + ], + ]; + + foreach ($wishListsData as $wishListData) { + $wishList = new WishList(); + $wishList->setName($wishListData['name']); + $wishList->setUser($wishListData['user']); + $wishList->setExpirationDate($wishListData['expiration_date']); + $wishList->setIsActive($wishListData['is_active']); + $wishList->setCreatedAt(new \DateTimeImmutable()); + + foreach ($wishListData['items'] as $item) { + $wishList->getItems()->add($item); + } + + $manager->persist($wishList); + } $manager->flush(); } + + public function getDependencies(): array + { + return [ + UserFixtures::class, + ItemFixtures::class, + ]; + } } -- GitLab