0

I created a thread about Testing Services with phpunit inside symfony. Now that I figured it out, I have the following problem. I have this service, which get’s User Cart by ID.

class CartService
{

    private CartRepository     $cartRepository;
    private ManagerRegistry    $managerRegistry;
    private CartItemRepository $cartItemRepository;
    private Security           $security;

    public function __construct(Security $security, CartItemRepository $cartItemRepository, CartRepository $cartRepository, ManagerRegistry $managerRegistry)
    {
        $this->cartItemRepository = $cartItemRepository;
        $this->cartRepository = $cartRepository;
        $this->managerRegistry = $managerRegistry;
        $this->security = $security;
    }

    /**
     * Get Cart by ID
     *
     * @return Cart|null
     */
    public function getCartByUserId(): ?Cart
    {
        $user = $this->security->getUser();
        return $this->cartRepository->findOneBy(['customer' => $user]);
    }

Since I am not logged in, I want to test this method with custom $user ID. I tried to add integer to $user variable inside CartServiceTest.php, but I get NULL as result.

class CartServiceTest extends KernelTestCase
{
    public CartService $cartService;

    public function setUp(): void
    {
        self::bootKernel();
        $container = static::getContainer();
        $this->cartService = $container->get(CartService::class);
    }

    public function testShowCart()
    {
        $user = 11; // Here 
        $cart = $this->cartService->getCartByUserId();
        dump($cart);

    }

}

Result:

PHPUnit 9.5.21 #StandWithUkraine

Testing App\Tests\CartServiceTest
^ null
R

As soon as I change my CartService, and add $user as argument, works, and I get Cart Object back.

/**
 * Get Cart by ID
 *
 * @return Cart|null
 */
public function getCartByUserId($user): ?Cart
{
    return $this->cartRepository->findOneBy(['customer' => $user]);
}

How can I change $user value inside unit testing? So I can run test with different user id’s?

1
  • Can't you mock a Security object which will return a valid user id when it's getUser() method is called? It is the Cart class you would like to the and not the Security class. Read more about mocking here: phpunit.readthedocs.io/en/9.5/test-doubles.html Commented Jul 21, 2022 at 15:21

1 Answer 1

2

You can't.

But what you can do, is to mock the Security service and make it return the $user object of your choice. For that, your Security service must be public.

$myUser = ...;//here comes whatever you expect as a user value
$myMock = $this->createMock(Security::class);
$myMock->method('getUser')
  ->willReturn($myUser);

$container = static::getContainer();
$container->set(Security::class, $myMock)

Now you should be able to test your service.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.