Commit c1ac3954 authored by Niklas de Vries's avatar Niklas de Vries

removed cwd generic dependency, fixes for symfony 3

parent db2ae09e
......@@ -13,9 +13,9 @@ use Cwd\MediaBundle\Service\MediaService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use JMS\SecurityExtraBundle\Annotation\PreAuthorize;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Cwd\GenericBundle\Controller\GenericController as CwdController;
/**
* Class SummernoteController
......@@ -26,7 +26,7 @@ use Cwd\GenericBundle\Controller\GenericController as CwdController;
* @PreAuthorize("hasRole('ROLE_ADMIN')")
* @Route("/media")
*/
class SummernoteController extends CwdController
class SummernoteController extends Controller
{
/**
* @Route("/upload")
......
......@@ -9,7 +9,7 @@
*/
namespace Cwd\MediaBundle\Model\Repository;
use Cwd\GenericBundle\Doctrine\EntityRepository;
use Doctrine\ORM\EntityRepository;
/**
* Media Repository
......
......@@ -2,30 +2,30 @@ services:
cwd.media.service:
class: Cwd\MediaBundle\Service\MediaService
arguments:
- "@entity_manager"
- "@doctrine.orm.entity_manager"
- "@logger"
- "%cwd.media.config%"
cwd.media.twig.image:
class: Cwd\MediaBundle\Twig\ImageExtension
arguments: [@cwd.media.service]
arguments: ["@cwd.media.service"]
tags:
- { name: twig.extension }
cwd_image_type:
class: Cwd\MediaBundle\Form\Type\ImageType
arguments: [@cwd.media.service]
arguments: ["@cwd.media.service"]
tags:
- { name: form.type }
cwd.media.resize_uploaded_images_subscriber:
class: Cwd\MediaBundle\Form\EventListener\ResizeUploadedImagesSubscriber
arguments: [@cwd.media.service]
arguments: ["@cwd.media.service"]
tags:
- { name: form.event_listener }
cwd_summernote_image_type:
class: Cwd\MediaBundle\Form\Type\SummernoteImageType
arguments: [@cwd.media.resize_uploaded_images_subscriber]
arguments: ["@cwd.media.resize_uploaded_images_subscriber"]
tags:
- { name: form.type }
\ No newline at end of file
......@@ -9,14 +9,14 @@
*/
namespace Cwd\MediaBundle\Service;
use Aspetos\Service\Exception\NotFoundException;
use Cwd\GenericBundle\Service\Generic;
use Cwd\MediaBundle\MediaException;
use Cwd\MediaBundle\Model\Entity\Media;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityNotFoundException;
use Doctrine\ORM\Mapping\Entity;
use Gregwar\Image\Image;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Monolog\Logger;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
......@@ -26,7 +26,7 @@ use Symfony\Component\Process\Process;
* @package CwdMediaBundel\Service
* @author Ludwig Ruderstaller <lr@cwd.at>
*/
class MediaService extends Generic
class MediaService
{
/**
* @var array
......@@ -38,6 +38,16 @@ class MediaService extends Generic
*/
protected $debug = false;
/** @var
* EntityManager
*/
protected $entityManager;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @param EntityManager $entityManager
* @param LoggerInterface $logger
......@@ -49,8 +59,8 @@ class MediaService extends Generic
{
$this->config = $config;
$this->debug = $config['throw_exception'];
parent::__construct($entityManager, $logger);
$this->entityManager = $entityManager;
$this->logger = $logger;
$this->directorySetup();
}
......@@ -451,21 +461,21 @@ class MediaService extends Generic
* @param int $pid
*
* @return Entity
* @throws NotFoundException
* @throws EntityNotFoundException
*/
public function find($pid)
{
try {
$obj = parent::findById('Model:Media', intval($pid));
$obj = $this->findById('Model:Media', intval($pid));
if ($obj === null) {
$this->getLogger()->info('Row with ID {id} not found', array('id' => $pid));
throw new NotFoundException('Row with ID '.$pid.' not found');
throw new EntityNotFoundException('Row with ID '.$pid.' not found');
}
return $obj;
} catch (\Exception $e) {
throw new NotFoundException();
throw new EntityNotFoundException();
}
}
......@@ -481,4 +491,68 @@ class MediaService extends Generic
return $this->findAllByModel('Model:Media', $filter, $order, $amount, $offset);
}
/**
* @return EntityManager
*/
public function getEm()
{
return $this->entityManager;
}
/**
* Flush EntityManager.
*/
public function flush()
{
$this->getEm()->flush();
}
/**
* @return Logger
*/
public function getLogger()
{
return $this->logger;
}
/**
* @param string $model
* @param int $id
*
* @return object
*/
public function findById($model, $id)
{
return $this->entityManager->getRepository($model)->find($id);
}
/**
* Find one Entities by fields in given Model.
*
* @param string $model
* @param array $filter
*
* @return object
*/
public function findOneByFilter($model, $filter = array())
{
return $this->getEm()->getRepository($model)->findOneby($filter);
}
/**
* Find All by Model.
*
* @param string $model
* @param array $filter
* @param array $order
* @param int $amount
* @param int $offset
*
* @return \Doctrine\Common\Collections\Collection
*/
public function findAllByModel($model, $filter = array(), $order = array(), $amount = 10, $offset = 0)
{
return $this->getEm()->getRepository($model)->findBy($filter, $order, $amount, $offset);
}
}
......@@ -9,9 +9,11 @@
*/
namespace CwdMediaBundle\Tests\Service;
use Cwd\GenericBundle\Tests\Repository\DoctrineTestCase;
use Cwd\MediaBundle\Model\Entity\Media;
use Doctrine\ORM\EntityManager;
use Gregwar\Image\Image;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\Container;
/**
* Class MediaTest
......@@ -19,8 +21,18 @@ use Gregwar\Image\Image;
* @package CwdMediaBundle\Tests\Service
* @author Ludwig Ruderstaller <lr@cwd.at>
*/
class MediaTest extends DoctrineTestCase
class MediaTest extends WebTestCase
{
/**
* @var Container
*/
protected $container;
/**
* @var EntityManager
*/
protected $em;
/**
* @var \Cwd\MediaBundle\Service\MediaService
*/
......@@ -35,6 +47,10 @@ class MediaTest extends DoctrineTestCase
{
parent::setUp();
self::bootKernel();
$this->container = static::$kernel->getContainer();
$this->em = $this->container->get('doctrine.orm.entity_manager');
$this->service = $this->container->get('cwd.media.service');
$config = $this->service->getConfig();
......@@ -55,14 +71,14 @@ class MediaTest extends DoctrineTestCase
public function tearDown()
{
parent::tearDown();
$this->getEntityManager()->clear();
$repository = $this->getEntityManager()->getRepository($this->service->getConfig('entity_class'));
$this->em->clear();
$repository = $this->em->getRepository($this->service->getConfig('entity_class'));
$result = $repository->findBy(array());
foreach ($result as $row) {
$this->getEntityManager()->remove($row);
$this->em->remove($row);
}
$this->getEntityManager()->flush();
$this->em->flush();
}
public function testSetup()
......@@ -72,7 +88,7 @@ class MediaTest extends DoctrineTestCase
$this->assertTrue(is_writeable($this->service->getConfig('cache')['path']));
try {
$repository = $this->getEntityManager()->getRepository($this->service->getConfig('entity_class'));
$repository = $this->em->getRepository($this->service->getConfig('entity_class'));
$this->assertInstanceOf('Cwd\MediaBundle\Model\Repository\MediaRepository', $repository);
} catch (\Exception $e) {
$this->assertTrue(false, $this->service->getConfig('entity_class').' is not a valid Repository');
......@@ -109,9 +125,9 @@ class MediaTest extends DoctrineTestCase
public function testCreate()
{
$media = $this->service->create(__DIR__.'/../data/demo.jpg');
$this->assertEquals(get_class($media), $this->getEntityManager()->getRepository($this->service->getConfig('entity_class'))->getClassName());
$this->assertEquals(get_class($media), $this->em->getRepository($this->service->getConfig('entity_class'))->getClassName());
$this->assertNull($media->getId());
$this->getEntityManager()->flush($media);
$this->em->flush($media);
$this->assertNotNull($media->getId());
$this->assertGreaterThan(0, $media->getId());
......@@ -120,7 +136,7 @@ class MediaTest extends DoctrineTestCase
$media = $this->service->create(__DIR__.'/../data/demo.jpg', true);
$this->assertGreaterThan(0, $media->getId());
$this->assertEquals($id, $media->getId());
$this->getEntityManager()->flush($media);
$this->em->flush($media);
return $media;
}
......@@ -133,7 +149,7 @@ class MediaTest extends DoctrineTestCase
$media = $this->service->create(__DIR__.'/../data/demo.jpg', true);
$this->assertNull($media->getId());
$this->getEntityManager()->flush($media);
$this->em->flush($media);
$this->setExpectedException('Exception');
$this->service->create(__DIR__.'/../data/demo.jpg', false);
......
......@@ -9,10 +9,12 @@
*/
namespace CwdMediaBundle\Tests\Twig;
use Cwd\GenericBundle\Tests\Repository\DoctrineTestCase;
use Cwd\MediaBundle\Model\Entity\Media;
use Cwd\MediaBundle\Twig\ImageExtension;
use Doctrine\ORM\EntityManager;
use Gregwar\Image\Image;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\Container;
/**
* Class MediaTest
......@@ -20,8 +22,18 @@ use Gregwar\Image\Image;
* @package CwdMediaBundle\Tests\Service
* @author Ludwig Ruderstaller <lr@cwd.at>
*/
class ImageExtensionTest extends DoctrineTestCase
class ImageExtensionTest extends WebTestCase
{
/**
* @var Container
*/
protected $container;
/**
* @var EntityManager
*/
protected $em;
/**
* @var \Cwd\MediaBundle\Service\MediaService
*/
......@@ -35,6 +47,11 @@ class ImageExtensionTest extends DoctrineTestCase
public function setUp()
{
parent::setUp();
self::bootKernel();
$this->container = static::$kernel->getContainer();
$this->em = $this->container->get('doctrine.orm.entity_manager');
$this->service = $this->container->get('cwd.media.service');
$config = $this->service->getConfig();
$config['storage']['path'] = '/tmp/unitest-mediastore-'.date("U");
......@@ -50,22 +67,22 @@ class ImageExtensionTest extends DoctrineTestCase
public function tearDown()
{
parent::tearDown();
$this->getEntityManager()->clear();
$repository = $this->getEntityManager()->getRepository($this->service->getConfig('entity_class'));
$this->em->clear();
$repository = $this->em->getRepository($this->service->getConfig('entity_class'));
$result = $repository->findBy(array());
foreach ($result as $row) {
$this->getEntityManager()->remove($row);
$this->em->remove($row);
}
$this->getEntityManager()->flush();
$this->em->flush();
}
public function testExtension()
{
$media = $this->service->create(__DIR__.'/../data/demo.jpg');
$this->assertEquals(get_class($media), $this->getEntityManager()->getRepository($this->service->getConfig('entity_class'))->getClassName());
$this->assertEquals(get_class($media), $this->em->getRepository($this->service->getConfig('entity_class'))->getClassName());
$this->assertNull($media->getId());
$this->getEntityManager()->flush($media);
$this->em->flush($media);
$output = $this->getTemplate('{{ cwdImage(media).cropResize(200) }}')->render(array('media' => $media));
$this->assertTrue(file_exists($this->service->getConfig('cache')['path'].'/'.$output));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment