Skip to content

Instantly share code, notes, and snippets.

@Bendihossan
Last active December 12, 2015 03:08
Show Gist options
  • Save Bendihossan/4703993 to your computer and use it in GitHub Desktop.
Save Bendihossan/4703993 to your computer and use it in GitHub Desktop.
<?php
/**
* ProTalk
*
* Copyright (c) 2012-2013, ProTalk
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Protalk\MediaBundle\Tests\Fixtures;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Collections\ArrayCollection;
use Protalk\MediaBundle\Entity\Media;
use Protalk\MediaBundle\Entity\Mediatype;
use Protalk\MediaBundle\Entity\Speaker;
use Protalk\MediaBundle\Entity\Category;
use Protalk\MediaBundle\Entity\Tag;
class LoadMediaData implements FixtureInterface
{
public function load(ObjectManager $manager)
{
$videoType = new Mediatype();
$videoType->setName('video');
$videoType->setType('video');
$manager->persist($videoType);
$speaker1 = new Speaker();
$speaker1->setName('Joe Bloggs');
$speaker1->setBiography('Joe Bloggs bio.');
$manager->persist($speaker1);
$category1 = new Category();
$category1->setName('PHP');
$category1->setSlug('php');
$manager->persist($category1);
$tag1 = new Tag();
$tag1->setName('PHPNW');
$tag1->setSlug('phpnw');
$manager->persist($tag1);
$video1 = new Media();
$video1->setMediatype($videoType);
$video1->setSpeakers(
new ArrayCollection(
array(
$speaker1
)
)
);
$video1->setCategories(
new ArrayCollection(
array(
$category1
)
)
);
$video1->setTags(
new ArrayCollection(
array(
$tag1
)
)
);
$video1->setDate(new \DateTime());
$video1->setCreationDate(new \DateTime);
$video1->setTitle('My video about PHP');
$video1->setDescription('A video about PHP!');
$video1->setContent('http://some.video-url.com');
$video1->setLength('20:00:00');
$video1->setRating(2.5);
$video1->setVisits(100);
$video1->setLanguage('EN');
$video1->setSlug('php');
$video1->setHostName('PHP');
$video1->setHostUrl('php');
$video1->setIsPublished(1);
$manager->persist($video1);
$manager->flush();
}
}
<?php
/**
* ProTalk
*
* Copyright (c) 2012-2013, ProTalk
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ProTalk\PageBundle\Tests\Controller;
use Liip\FunctionalTestBundle\Test\WebTestCase;
use Protalk\MediaBundle\Controller\MediaController;
class MediaControllerTest extends WebTestCase
{
public function setUp()
{
$this->loadFixtures(
array(
'Protalk\MediaBundle\Tests\Fixtures\LoadMediaData'
)
);
}
public function testMediaPageShowsAllData()
{
$client = static::createClient();
$client->request('GET', '/php');
$response = $client->getResponse()->getContent();
//var_dump($response);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}
<?php
...
/**
* Override native findOneBySlug method to include
* mediatype join, reducing no. of queries to db
* and increment no of visits made to media item
*
* @param string $slug
* @return Doctrine Record
*/
public function findOneBySlug($slug)
{
$query = $this->createQueryBuilder('c')
->select('m')
->from("ProtalkMediaBundle:Media", "m")
->where('m.slug = :slug')
->andWhere('m.isPublished = :isPublished')
->setParameter('slug', $slug)
->setParameter('isPublished', 1);
//var_dump($query->getQuery()->getSingleScalarResult());die();
return $query->getQuery()->getOneOrNullResult();
// return $this->getEntityManager()
// ->createQuery(
// 'SELECT m, mt
// FROM ProtalkMediaBundle:Media m
// JOIN m.mediatype mt
// WHERE m.slug = :slug AND m.isPublished = 1'
// )
// ->setParameter('slug', $slug)
// ->getSingleResult();
}
@Bendihossan
Copy link
Author

It's odd, I checked the sqlite test.db and the data does get inserted into the DB :(

@aitboudad
Copy link

Hi, you must change $this->createQueryBuilder('c')
By $this->_em->createQueryBuilder()

Or delete lines 15 and 16 like this:

$query = $this->createQueryBuilder('c')
        ->where('m.slug = :slug')
        ->andWhere('m.isPublished = :isPublished')
        ->setParameter('slug', $slug)
        ->setParameter('isPublished', 1);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment