Skip to content

Instantly share code, notes, and snippets.

@doctrinebot
Created December 13, 2015 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doctrinebot/8e1cc73065be95be26a6 to your computer and use it in GitHub Desktop.
Save doctrinebot/8e1cc73065be95be26a6 to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-949 - https://github.com/doctrine/doctrine2/issues/5478
<?php
namespace Foo\Main\Tests\Functional;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Application\FooBundle\Entity\Poll;
/*
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Application\FooBundle\Entity\PollOption" table="poll_option" repository-class="Application\FooBundle\Entity\PollOptionRepository">
<id name="id" column="id" type="integer">
<generator strategy="IDENTITY" />
</id>
<field name="extid" column="extid" type="integer" />
<field name="text" column="text" type="string" length="255" />
<field name="letter" column="letter" type="string" length="1" />
<field name="pollid" column="pollid" type="integer" />
<many-to-one field="poll" target-entity="Poll">
<join-column name="pollid" referenced-column-name="id" />
</many-to-one>
<one-to-many field="votes" target-entity="PollVote" mapped-by="option" />
</entity>
</doctrine-mapping>
*/
use Application\FooBundle\Entity\PollOption;
/*
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Application\FooBundle\Entity\Poll" table="poll" repository-class="Application\FooBundle\Entity\PollRepository">
<id name="id" column="id" type="integer">
<generator strategy="IDENTITY" />
</id>
<field name="extid" column="extid" type="integer" unique="true" />
<field name="title" column="title" type="string" length="255" />
<field name="isActive" column="is_active" type="boolean" />
<one-to-many field="options" target-entity="PollOption" mapped-by="poll" />
</entity>
</doctrine-mapping>
*/
class LoadPollData implements FixtureInterface {
public function load($manager)
{
$this->createPoll($manager, 'A test poll', 1);
$this->createPoll($manager, 'Inactive Poll', 2, false);
}
protected function createPoll($manager, $title, $extid, $isActive = true, $nofOptions = 2)
{
$poll = new Poll($extid, $title);
$poll->setIsActive($isActive);
$manager->persist($poll);
$letters = array('A', 'B', 'C', 'D');
for ($i = 0; $i < $nofOptions; $i++) {
$option = new PollOption($i, null, $letters[$i], 'Option '.$letters[$i]);
$option->setPoll($poll);
$manager->persist($option);
}
$manager->flush();
}
}
class PollVoteTest extends WebTestCase
{
public function testInactivePollFail()
{
$kernel = $this->createKernel(array('environment' => 'test'));
$kernel->boot();
$em = $kernel->getContainer()->get('doctrine.orm.entity_manager');
$connection = $em->getConnection();
$purger = new ORMPurger();
$executor = new ORMExecutor($em, $purger);
$loader = new Loader();
$loader->addFixture(new LoadPollData());
$executor->execute($loader->getFixtures());
$connection->close();
// fixture loading complete
$config = $em->getConfiguration();
$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSqlLogger());
$classname = 'Application\FooBundle\Entity\Poll';
$pollRepository = $em->getRepository($classname);
$poll = $pollRepository->findOneBy(array('isActive' => false));
$this->assertEquals(get_class($this), get_class($poll));
// For PostgreSQL we have to use 'true'/'false'
$poll = $pollRepository->findOneBy(array('isActive' => 'false'));
$this->assertEquals($classname, get_class($poll));
// For SQLite we have to use 0/1
$poll = $pollRepository->findOneBy(array('isActive' => 0));
$this->assertEquals($classname, get_class($poll));
}
}
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Tests\Models\Generic\BooleanModel;
require_once __DIR__ . '/../../../TestInit.php';
class DDC949Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
public function setUp()
{
$this->useModelSet('generic');
parent::setUp();
}
/**
* @group DDC-949
*/
public function testFindOneByBoolean()
{
$bool = new BooleanModel();
$bool->booleanField = true;
$this->_em->persist($bool);
$this->_em->flush();
$boolFound = $this->_em->getRepository('Doctrine\Tests\Models\Generic\BooleanModel')
->findOneBy(array('booleanField' => true));
$this->assertSame($bool, $boolFound, "Should find boolean entity with a true value.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment