mirror of
https://github.com/fzaninotto/Faker.git
synced 2025-03-21 15:59:52 +01:00
testing ORM Faker
This commit is contained in:
parent
3f4f69afba
commit
3e6def385c
44
src/Faker/ORM/Propel.php
Normal file
44
src/Faker/ORM/Propel.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Faker\ORM;
|
||||
|
||||
class Propel
|
||||
{
|
||||
protected $class;
|
||||
protected $columnFormatters = array();
|
||||
|
||||
public function __construct($class)
|
||||
{
|
||||
$this->class = $class;
|
||||
}
|
||||
|
||||
public function setColumnFormatters($columnFormatters)
|
||||
{
|
||||
$this->columnFormatters = array_merge($columnFormatters, $this->columnFormatters);
|
||||
}
|
||||
|
||||
public function populate($nb = 100, &$insertedEntities = array())
|
||||
{
|
||||
$pks = array();
|
||||
$class = $this->class;
|
||||
$peer = $class::PEER;
|
||||
$con = \Propel::getConnection($peer::DATABASE_NAME, \Propel::CONNECTION_WRITE);
|
||||
$con->beginTransaction();
|
||||
for ($i=0; $i < $nb; $i++) {
|
||||
$insertedEntities[$this->class][]= $this->populateOne($con, $insertedEntities);
|
||||
}
|
||||
$con->commit();
|
||||
}
|
||||
|
||||
public function populateOne($con, $insertedEntities)
|
||||
{
|
||||
$obj = new $this->class();
|
||||
foreach ($this->columnFormatters as $column => $format) {
|
||||
if (null !== $column) {
|
||||
$obj->setByName($column, is_callable($format) ? $format($insertedEntities) : $format);
|
||||
}
|
||||
}
|
||||
$obj->save($con);
|
||||
return $obj->getPrimaryKey();
|
||||
}
|
||||
}
|
27
test/test2.php
Normal file
27
test/test2.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
require_once dirname(__FILE__) . '/../../Propel/runtime/lib/Propel.php';
|
||||
set_include_path(dirname(__FILE__) . '/../../Propel/test/fixtures/bookstore/build/classes' . PATH_SEPARATOR . get_include_path());
|
||||
Propel::init(dirname(__FILE__) . '/../../Propel/test/fixtures/bookstore/build/conf/bookstore-conf.php');
|
||||
|
||||
require_once '../src/Faker/ORM/Propel.php';
|
||||
require_once '../src/Faker/Factory.php';
|
||||
|
||||
$entities = array();
|
||||
$generator = Faker\Factory::create();
|
||||
AuthorQuery::create()->deleteAll();
|
||||
$populator = new Faker\ORM\Propel('Author');
|
||||
$authorColumnFormatters = array(
|
||||
'FirstName' => function() use ($generator) { return $generator->firstName; },
|
||||
'LastName' => function() use ($generator) { return $generator->lastName; }
|
||||
);
|
||||
$populator->setColumnFormatters($authorColumnFormatters);
|
||||
$populator->populate(10, $entities);
|
||||
$bookColumnFormatters = array(
|
||||
'Title' => function() use ($generator) { return $generator->sentence; },
|
||||
'AuthorId' => function($inserted) { return $inserted['Author'][mt_rand(0, count($inserted['Author']) - 1)]; }
|
||||
);
|
||||
$populator = new Faker\ORM\Propel('Book');
|
||||
$populator->setColumnFormatters($bookColumnFormatters);
|
||||
$inserted = $populator->populate(10, $entities);
|
||||
echo BookQuery::create()->joinWith('Book.Author')->find();
|
Loading…
x
Reference in New Issue
Block a user