Edit in GitHubLog an issue

Dependency annotation

The @depends annotation helps you to define some dependencies between methods.

Format

Copied to your clipboard
/**
* @depends methodName
*/

Example 1

Let's check the following basic example.

Copied to your clipboard
/**
* @return int
*/
public function testOne(): int
{
$number = 2;
$this->assertEquals(2, $number);
return $number;
}
/**
* @depends testOne
*
* @param $number
*/
public function testNumber($number)
{
$this->assertEquals(2, $number);
}

Example 2

Copied to your clipboard
/**
* @return int
*/
public function testTwo(): int
{
$number = 2;
$this->assertEquals(2, $number);
return $number;
}
/**
* @return int
*/
public function testOne(): int
{
$number = 1;
$this->assertEquals(1, $number);
return $number;
}
/**
* @depends testOne
* @depends testTwo
*
* @param $one
* @param $two
*/
public function testNumber(int $one, int $two)
{
$this->assertEquals(1, $one);
$this->assertEquals(2, $two);
}

Example 3

Let's check the following practical example, where we'll be checking the customer email by customer ID.

Copied to your clipboard
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\TestFramework\Helper\Bootstrap;
...
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
*/
public function testLoadCustomer(): CustomerInterface
{
$customerId = 1;
$objectManager = Bootstrap::getObjectManager();
$customerRepository = $objectManager->create(CustomerRepositoryInterface::class);
return $customerRepository->getById($customerId);
}
/**
* @depends testLoadCustomer
*
* @param CustomerInterface $customer
*/
public function testEmail(CustomerInterface $customer)
{
$this->assertEquals('customer@example.com', $customer->getEmail());
}

You can read more about PHPUnit dependency annotation here.

  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.