protected function tearDown() { //Close & unsets if (is_object($this->em)) { $this->em->getConnection()->close(); $this->em->close(); } unset($this->em); unset($this->container); unset($this->kern); unset($this->client); //Nettoyage des mocks //http://kriswallsmith.net/post/18029585104/faster-phpunit $refl = new \ReflectionObject($this); foreach ($refl->getProperties() as $prop) { if (!$prop->isStatic() && 0 !== strpos($prop->getDeclaringClass()->getName(), 'PHPUnit_')) { $prop->setAccessible(true); $prop->setValue($this, null); } } //Nettoyage du garbage if (!gc_enabled()) { gc_enable(); } gc_collect_cycles(); //Parent parent::tearDown(); }
/** * Executes the given command and optionally returns a value * * @param object $command * @param callable $next * @return mixed * @throws Exception */ public function execute($command, callable $next) { $this->entityManager->beginTransaction(); try { $returnValue = $next($command); $this->entityManager->flush(); $this->entityManager->commit(); } catch (Exception $e) { $this->entityManager->close(); $this->entityManager->rollback(); throw $e; } return $returnValue; }
/** * Creates and persists a unique shortcode generated for provided url * * @param UriInterface $url * @param string[] $tags * @return string * @throws InvalidUrlException * @throws RuntimeException */ public function urlToShortCode(UriInterface $url, array $tags = []) { // If the url already exists in the database, just return its short code $shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy(['originalUrl' => $url]); if (isset($shortUrl)) { return $shortUrl->getShortCode(); } // Check that the URL exists $this->checkUrlExists($url); // Transactionally insert the short url, then generate the short code and finally update the short code try { $this->em->beginTransaction(); // First, create the short URL with an empty short code $shortUrl = new ShortUrl(); $shortUrl->setOriginalUrl($url); $this->em->persist($shortUrl); $this->em->flush(); // Generate the short code and persist it $shortCode = $this->convertAutoincrementIdToShortCode($shortUrl->getId()); $shortUrl->setShortCode($shortCode)->setTags($this->tagNamesToEntities($this->em, $tags)); $this->em->flush(); $this->em->commit(); return $shortCode; } catch (ORMException $e) { if ($this->em->getConnection()->isTransactionActive()) { $this->em->rollback(); $this->em->close(); } throw new RuntimeException('An error occurred while persisting the short URL', -1, $e); } }
protected function tearDown() { parent::tearDown(); $this->em->close(); }
/** * Commits the UnitOfWork, executing all operations that have been postponed * up to this point. The state of all managed entities will be synchronized with * the database. * * The operations are executed in the following order: * * 1) All entity insertions * 2) All entity updates * 3) All collection deletions * 4) All collection updates * 5) All entity deletions * * @param null|object|array $entity * * @return void * * @throws \Exception */ public function commit($entity = null) { // Raise preFlush if ($this->evm->hasListeners(Events::preFlush)) { $this->evm->dispatchEvent(Events::preFlush, new PreFlushEventArgs($this->em)); } // Compute changes done since last commit. if ($entity === null) { $this->computeChangeSets(); } elseif (is_object($entity)) { $this->computeSingleEntityChangeSet($entity); } elseif (is_array($entity)) { foreach ($entity as $object) { $this->computeSingleEntityChangeSet($object); } } if (!($this->entityInsertions || $this->entityDeletions || $this->entityUpdates || $this->collectionUpdates || $this->collectionDeletions || $this->orphanRemovals)) { $this->dispatchOnFlushEvent(); $this->dispatchPostFlushEvent(); return; // Nothing to do. } if ($this->orphanRemovals) { foreach ($this->orphanRemovals as $orphan) { $this->remove($orphan); } } $this->dispatchOnFlushEvent(); // Now we need a commit order to maintain referential integrity $commitOrder = $this->getCommitOrder(); $conn = $this->em->getConnection(); $conn->beginTransaction(); try { if ($this->entityInsertions) { foreach ($commitOrder as $class) { $this->executeInserts($class); } } if ($this->entityUpdates) { foreach ($commitOrder as $class) { $this->executeUpdates($class); } } // Extra updates that were requested by persisters. if ($this->extraUpdates) { $this->executeExtraUpdates(); } // Collection deletions (deletions of complete collections) foreach ($this->collectionDeletions as $collectionToDelete) { $this->getCollectionPersister($collectionToDelete->getMapping())->delete($collectionToDelete); } // Collection updates (deleteRows, updateRows, insertRows) foreach ($this->collectionUpdates as $collectionToUpdate) { $this->getCollectionPersister($collectionToUpdate->getMapping())->update($collectionToUpdate); } // Entity deletions come last and need to be in reverse commit order if ($this->entityDeletions) { for ($count = count($commitOrder), $i = $count - 1; $i >= 0 && $this->entityDeletions; --$i) { $this->executeDeletions($commitOrder[$i]); } } $conn->commit(); } catch (Exception $e) { $this->em->close(); $conn->rollback(); $this->afterTransactionRolledBack(); throw $e; } $this->afterTransactionComplete(); // Take new snapshots from visited collections foreach ($this->visitedCollections as $coll) { $coll->takeSnapshot(); } $this->dispatchPostFlushEvent(); // Clear up $this->entityInsertions = $this->entityUpdates = $this->entityDeletions = $this->extraUpdates = $this->entityChangeSets = $this->collectionUpdates = $this->collectionDeletions = $this->visitedCollections = $this->scheduledForSynchronization = $this->orphanRemovals = array(); }
/** * {@inheritdoc} */ public function close() { return $this->wrapped->close(); }