, 우리는 그기구의 정적 속성에 엔티티를 설정하는, 그래서 그들은 쉽게 테스트에서 사용할 수 있습니다. 몇 가지 중요한 규칙은 여기에있다
class CategoryTestFixture
extends \Doctrine\Common\DataFixtures\AbstractFixture
implements
\Doctrine\Common\DataFixtures\OrderedFixtureInterface,
\Symfony\Component\DependencyInjection\ContainerAwareInterface
{
/** @var \My\Category */
public static $fooCategory;
/** @var \My\Category */
public static $barCategory;
/** @var \Symfony\Component\DependencyInjection\ContainerInterface */
private $container;
public function load(ObjectManager $manager)
{
self::$fooCategory = new Category('Foo');
$entityManager->persist(self::$fooCategory);
self::$barCategory = new Category('Bar');
$entityManager->persist(self::$barCategory);
$entityManager->flush();
}
// you can inject the container,
// so you can use your Facades in fixtures
public function getContainer(): ContainerInterface
{
return $this->container;
}
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
}
있습니다
- 가 직접 다른 비품 클래스의 실체를 사용할 수 있도록 당신이 당신의 설비에
$em->clear()
를 호출하지 않도록합니다.
- 일단 조명기가로드되면
$em->clear()
으로 전화하면 검사에 영향을 미치지 않습니다.
- 모든 테스트를 수행하기 전에 준비된 데이터베이스를 복사 한 다음 해당 복사본을 "템플릿 데이터베이스"가 아닌 테스트 용으로 사용하십시오. 따라서 데이터를 안전하게 수정할 수 있습니다. 더욱 최적화 할 수 있지만 가장 간단한 해결책입니다.
- 테스트에서 원래의 조명기를 관리하거나 등록하려고하지 마십시오.
은 이제 비품이 만든 것을, 당신은 당신이 모든 속성뿐만 아니라 ID를 참조 할 수 있습니다, 또한이
$id = CategoryTestFixture::$barCategory->getId();
처럼 사용합니다. 따라서 API가 올바른 카테고리를 반환했다는 주장을하고 싶다면 이렇게 할 수 있습니다.
$this->assertArraySubset([
[
'id' => CategoryTestFixture::$fooCategory->getId(),
'name' => CategoryTestFixture::$fooCategory->getName(),
],
[
'id' => CategoryTestFixture::$barCategory->getId(),
'name' => CategoryTestFixture::$barCategory->getName(),
]
], $apiResponseData);
당신은 당신이 이미 채워진 신원지도와 부작용을 생성하지 않도록, aftewards, 하나 개의 테스트 케이스에 대한 데이터를 수정 데이터베이스를 수정하는기구의 속성을 사용하고 명확 EM 싶다면 엔티티 관리자.
$barCategory = $entityManager->find(
Category::class,
CategoryTestFixture::$barCategory->getId()
);
$barCategory->setName('Another name');
$entityManager->flush();
$entityManager->clear();
당신은 심포니와 함께 귀하의 질문에 태그를하지만, 여기를 읽고 있지 않았다 https://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html#sharing-objects-between-fixtures는 당신에게 어떤을 줄 수도 아이디어. – Cerad
글쎄, 나는 Symfony와 함께 작동하지 않기 때문에하지 않았다. 나는 Nett 프레임 워크를 위해서 Doctrine을 사용한다. 그러나 마침내 필자는이 Symfony의 접근 방식으로 끝났습니다. 감사합니다. – amik