Mam obecnie duży problem. Podczas testu jednostkowego używam urządzeń na początku testu, aby dodać dane do bazy danych (wszystko działa poprawnie) i chciałbym użyć tego samego kodu co urządzenie, aby dodać dane podczas testu. Exemple:Symfony2, urządzenia i test jednostki. Dodaj dane testowe podczas testu w ManyToMany Relation
jednostki Person:
class Person {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
[...]
/**
* @ORM\ManyToMany(targetEntity="Family", cascade={"persist"})
* @Assert\Count(min = "1")
*/
private $families;
public function __construct() {
$this->families = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addFamily(Family $family) {
$this->families[] = $family;
return $this;
}
[...]
}
Podmiot Rodzina:
class Family {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
* @Gedmo\Slug(fields={"name"})
* @ORM\Column(name="slug", type="string", length=255, unique=true)
*/
private $slug;
[...]
}
Oprawy: test
class Fixture1 implements FixtureInterface, ContainerAwareInterface {
/**
* @var Family[]
*/
public $families = array();
public function setContainer(ContainerInterface $container = null) {
$this->container = $container;
}
public function load(ObjectManager $manager) {
$this->manager = $manager;
$SmithFamily= $this->addFamily("Smith");
$this->addPerson("Will", "Smith", array($SmithFamily));
}
public function addFamily($name) {
$family = new Family();
$family->setName($name);
$this->manager->persist($family);
$this->manager->flush();
$this->families[$name] = $family;
return $family;
}
public function addPerson($firstName, $lastName, array $families = array()) {
$person = new Person();
$person->setFirstname($firstName);
$person->setLastname($lastName);
foreach ($families as $family) {
$person->addFamily($family);
}
$this->manager->persist($person);
$this->manager->flush();
return $person;
}
}
Jednostka:
class PersonTest extends WebTestCase {
public $fixtures;
protected function setUp() {
$client = self::createClient();
$container = $client->getKernel()->getContainer();
$em = $container->get('doctrine')->getManager();
// Purge tables
$purger = new \Doctrine\Common\DataFixtures\Purger\ORMPurger($em);
$executor = new \Doctrine\Common\DataFixtures\Executor\ORMExecutor($em, $purger);
$executor->purge();
// Load fixtures
$loader = new \Doctrine\Common\DataFixtures\Loader;
$this->fixtures = new Fixture1();
$this->fixtures->setContainer($container);
$loader->addFixture($this->fixtures);
$executor->execute($loader->getFixtures());
parent::setUp();
}
public function test1() {
$this->fixtures->addPerson("James", "Smith", array($this->fixtures->families['Smith']));
[...]
}
}
Kiedy wykonać test, i uzyskać ten błąd:
Doctrine \ DBAL \ Wyjątek \ UniqueConstraintViolationException: AN wyjątek podczas wykonywania „INSERT INTO rodziny (ślimak, nazwy tworzone, last_updated_date, deleteddate) Wartości (?,?,?,?,?) "z parami [" kowal "," Smith "," 2015-01-10 13:59:28 "," 2015-01-10 13:59:28 " , zero] SQLSTATE [23000] naruszenie integralności ograniczenia: 1062 Duplicata du Champ "Smitha wlać Klucz 'UNIQ_A5E6215B989D9B62'
Ten błąd oznacza, że pole "slug" musi być unikalne. Próbuje dodać nową rodzinę, mimo że rodzina już istnieje. Powinno to po prostu dodać relację między nową Osobą (James Smith) a Rodziną. Nie rozumiem dlaczego! Każdy pomysł?