隨着Smyfony2和Doctrin2,數據固定裝置可以使用下面的示例中創建可以爲功能測試創建一個純粹的測試數據環境。如何才能在功能測試期間運行一組特定的僅用於測試的燈具,以及如何將這些燈具與我的標準燈具分離,以便控制檯命令忽略它們?與Symfony的+學說環境的具體數據燈具
看來,這樣做的方式是複製教條的功能:夾具控制檯命令並將測試夾具存儲在別處。有沒有人有更好的解決方案?
隨着Smyfony2和Doctrin2,數據固定裝置可以使用下面的示例中創建可以爲功能測試創建一個純粹的測試數據環境。如何才能在功能測試期間運行一組特定的僅用於測試的燈具,以及如何將這些燈具與我的標準燈具分離,以便控制檯命令忽略它們?與Symfony的+學說環境的具體數據燈具
看來,這樣做的方式是複製教條的功能:夾具控制檯命令並將測試夾具存儲在別處。有沒有人有更好的解決方案?
根據目錄分解燈具的替代方法是使用自定義燈具類。然後,您的夾具類將擴展此類,並指定將在實際加載的環境
<?php
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* Provides support for environment specific fixtures.
*
* This container aware, abstract data fixture is used to only allow loading in
* specific environments. The environments the data fixture will be loaded in is
* determined by the list of environment names returned by `getEnvironments()`.
*
* > The fixture will still be shown as having been loaded by the Doctrine
* > command, `doctrine:fixtures:load`, despite not having been actually
* > loaded.
*
* @author Kevin Herrera <[email protected]>
*/
abstract class AbstractDataFixture implements ContainerAwareInterface, FixtureInterface
{
/**
* The dependency injection container.
*
* @var ContainerInterface
*/
protected $container;
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
/** @var KernelInterface $kernel */
$kernel = $this->container->get('kernel');
if (in_array($kernel->getEnvironment(), $this->getEnvironments())) {
$this->doLoad($manager);
}
}
/**
* {@inheritDoc}
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
/**
* Performs the actual fixtures loading.
*
* @see \Doctrine\Common\DataFixtures\FixtureInterface::load()
*
* @param ObjectManager $manager The object manager.
*/
abstract protected function doLoad(ObjectManager $manager);
/**
* Returns the environments the fixtures may be loaded in.
*
* @return array The name of the environments.
*/
abstract protected function getEnvironments();
}
你的燈具最終會看起來像這樣:
<?php
namespace Vendor\Bundle\ExampleBundle\DataFixtures\ORM;
use AbstractDataFixture;
use Doctrine\Common\Persistence\ObjectManager;
/**
* Loads data only on "prod".
*/
class ExampleData extends AbstractDataFixture
{
/**
* @override
*/
protected function doLoad(ObjectManager $manager)
{
// ... snip ...
}
/**
* @override
*/
protected function getEnvironments()
{
return array('prod');
}
}
我認爲,這應與工作ORM一個ODM數據裝置。
最簡單的方法是將您的燈具放入不同的文件夾,然後使用php app/console doctrine:fixtures:load --fixtures=../src/Acme/TestBundle/DataFixtures/ORM/test
命令加載它們。 Fixtures選項必須指向您應用程序文件夾的相對路徑!
然後,您可以將數據拆分爲初始,測試等,或者根據需要創建開發,測試,分期,產品固件。
如果你想混合起來,我不知道比我做的任何更好的解決方案:我創建一個「模板」文件夾,其中所有燈具駐留在。在我的dev文件夾中,我創建了一個類,從模板中選擇適當的夾具類並調整需要調整的內容(如覆蓋getOrder
方法)。這不是完美的,我想可以考慮擴展燈具:load命令採取多個路徑,但它適用於我。
這聽起來像一個好的開始。謝謝! – 2012-08-06 22:58:39
這是天才! – ferdynator 2014-04-04 12:43:01
如何從控制檯定義環境?使用'php app/console fixture:load --env = prod'? – xDaizu 2016-02-12 08:57:53
回答自己:Yesh,'php app/console fixture:load --env = prod',與提供的解決方案一起使用:) – xDaizu 2016-04-08 14:13:34