Skip to content

Commit

Permalink
testing ORM Faker
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Oct 23, 2011
1 parent 3f4f69a commit 3e6def3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Faker/ORM/Propel.php
Original file line number Diff line number Diff line change
@@ -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 changes: 27 additions & 0 deletions test/test2.php
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 3e6def3

Please sign in to comment.