Symfony5创建一个用于测试的用户

huangapple go评论95阅读模式
英文:

Symfony5 create a user for testing

问题

I have a PurchaceFactory and one of its parameter is a User entity object.

I would like to create a user:

  1. class PurchaceFactoryTest extends TestCase
  2. {
  3. public function testCreate()
  4. {
  5. $user = new User();
  6. $user->setEmail('user1@email.com');
  7. $user->setPassword('password');
  8. $user->setRoles(["ROLE_USER", "ROLE_BUY"]);
  9. $jsonEncoder = new JsonEncoder();
  10. $artworkData = [
  11. "ID" => 129884,
  12. "title" => "Starry Night and the Astronauts",
  13. "author" => "Alma Thomas",
  14. "thumbnail" => [
  15. "lqip" => "data:image/gif;base64,somebase64data",
  16. "width" => 800,
  17. "height" => 600,
  18. "alt_text" => "Al text"
  19. ]
  20. ];
  21. $purchaceFactory = new PurchaceFactory($jsonEncoder);
  22. $purchace = $purchaceFactory->create($user, $artworkData);
  23. //assertions
  24. //...
  25. }
  26. }

The problem is, that I can not set the id of the user, since there is no setId for the entity when I made the bin/console make:user.

The purchaseFactory use that property of $user.

How could I simulate a User object with id?

I do not want to modify the UserEntity and add setId because id should be immutable.

英文:

I have a PurchaceFactory and one of its parameter is a User entity object.

I would like to create a user:

  1. class PurchaceFactoryTest extends TestCase
  2. {
  3. public function testCreate()
  4. {
  5. $user = new User();
  6. $user->setEmail('user1@email.com');
  7. $user->setPassword('password');
  8. $user->setRoles(["ROLE_USER", "ROLE_BUY"]);
  9. $jsonEncoder = new JsonEncoder();
  10. $artworkData = [
  11. "ID" => 129884,
  12. "title" => "Starry Night and the Astronauts",
  13. "author" => "Alma Thomas",
  14. "thumbnail" => [
  15. "lqip" => "data:image/gif;base64,somebase64data",
  16. "width" => 800,
  17. "height" => 600,
  18. "alt_text" => "Al text"
  19. ]
  20. ];
  21. $purchaceFactory = new PurchaceFactory($jsonEncoder);
  22. $purchace = $purchaceFactory->create($user, $artworkData);
  23. //assertions
  24. //...
  25. }
  26. }

The problem is, that I can not set the id of the user, since there is no setId for the entity when I made the bin/console make:user.

The purchaseFactory use that property of $user.

How could I simulate a User object with id?

I do not want to modify the UserEntity and add setId because id should be immutable.

答案1

得分: 0

尝试创建一个TestUser实体并扩展它使用User实体。然后停用在属性id中设置id的策略。

  1. use Doctrine\ORM\Mapping as ORM;
  2. /**
  3. * @ORM\Table(name="user")
  4. */
  5. class TestUser extend User
  6. {
  7. /**
  8. * @ORM\Id
  9. * @ORM\GeneratedValue(strategy="NONE")
  10. * @ORM\Column(type="integer")
  11. */
  12. private $id;
  13. public function setId(int $id): void
  14. {
  15. $this->id = $id;
  16. }
  17. }
  18. $user = new TestUser();
  19. $user->setId(1000);
英文:

Try creating a TestUser entity and extending it with the User entity. Then deactivate the strategy for setting the id in the property id.

  1. use Doctrine\ORM\Mapping as ORM;
  2. /**
  3. * @ORM\Table(name="user")
  4. */
  5. class TestUser extend User
  6. {
  7. /**
  8. * @ORM\Id
  9. * @ORM\GeneratedValue(strategy="NONE")
  10. * @ORM\Column(type="integer")
  11. */
  12. private $id;
  13. public function setId(int $id): void
  14. {
  15. $this->id = $id;
  16. }
  17. }
  18. $user = new TestUser();
  19. $user->setId(1000);

huangapple
  • 本文由 发表于 2023年6月30日 00:25:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定