phactory
A Database Factory for PHP Unit Tests
I have a trivially small PHPUnit test that looks like this:
<?php
namespace VNN\PressboxBundle\Tests\Entity;
namespace VNN\PressboxBundle\Entity;
use VNN\PressboxBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Phactory\Sql\Phactory;
class UserTest extends EntityTest
{
public function testCreate()
{
Phactory::reset();
}
}
When I try to run it, I get this:
There was 1 error:
1) VNN\PressboxBundle\Entity\UserTest::testCreate
ErrorException: Runtime Notice: Non-static method Phactory\Sql\Phactory::reset() should not be called statically, assuming $this from incompatible context in /Users/jason/Web/pressbox/src/VNN/PressboxBundle/Tests/Entity/UserTest.php line 13
What's up with that? All the docs call it statically.
I'm doing this on Symfony 2.0, if that makes a difference.
Source: (StackOverflow)
I am new to Unit testing and trying to use Phactory with Mongo, but when I run the test I keep getting the following error.
PHP Fatal error: Class 'Mongo' not found in /vagrant/Test/user/LoginTest.php on line 19
I know Mongo is installed because when I pull up the site it works, but for some reason PHPUnit isn't recognizing it.
I feel I am missing a step. Do I need to install the Mongo extension locally, or do I need to include the location in a config file somewhere?
Source: (StackOverflow)
I'm trying to get Phactory working with a Symfony2 project. Here's what I have in one of my unit tests:
<?php
namespace VNN\PressboxBundle\Tests\Entity;
namespace VNN\PressboxBundle\Entity;
use VNN\PressboxBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Phactory\Sql\Phactory;
class UserTest extends EntityTest
{
public function testCreate()
{
Phactory::define('user', array(
'username' => 'jasonswett',
));
}
}
The error I'm getting when I try to run that test is this:
PHP Fatal error: Class 'Phactory\Sql\Phactory' not found in /Users/jason/Web/pressbox/src/VNN/PressboxBundle/Tests/Entity/UserTest.php on line 12
Fair enough. How do I get this file to talk to Phactory? If it helps, I know that the Phactory
class is defined in vendor/chriskite/phactory/lib/Phactory/Sql/Phactory.php
and the first few lines of it look like this:
<?php
namespace Phactory\Sql;
class Phactory {
Source: (StackOverflow)
This code get an error: Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\kohana\application\controllers\phactory2_test.php on line 16
It's the example from the Phactory guide: http://phactory.org/guide/#phpunit-example
<?php
include_once('/simpletest/autorun.php');
require_once ('/Phactory/lib/Phactory.php');
/**
* This is the function we will test.
* It should retrieve a user from the db by id,
* and return that user's age.
*
* @param PDO $pdo
* @param int $user_id
* @return mixed The age of the user, or false if no user
*/
function getUserAge($pdo, $user_id)
{
$stmt = $pdo->prepare("SELECT * FROM `users` WHERE `id` = ?");
$stmt->execute(array($user_id));
$user = $stmt->fetch();
if(false === $user) {
return false;
}
return $user['age'];
}
class UserTest extends UnitTestCase
{
public static function setUpBeforeClass()
{
// create a db connection and tell Phactory to use it
$pdo = new PDO('mysql:host=127.0.0.1; dbname=testdb', 'root', '');
Phactory::setConnection($pdo);
/**
* Normally you would not need to create a table here, and would use
* an existing table in your test database instead.
* For the sake of creating a self-contained example, we create
* the 'users' table here.
*/
$pdo->exec("CREATE TABLE `users` ( id INTEGER PRIMARY KEY, name TEXT, age INTEGER )");
// reset any existing blueprints and empty any tables Phactory has used
Phactory::reset();
// define default values for each user we will create
Phactory::define('user', array('name' => 'Test User $n', 'age' => 18));
}
public static function tearDownAfterClass()
{
Phactory::reset();
// since we created a table in this test, we must drop it as well
Phactory::getConnection()->exec("DROP TABLE `users`");
}
public function testGetUserAge()
{
// test that getUserAge() returns false for a nonexistent user
$age = getUserAge(Phactory::getConnection(), 0);
$this->assertFalse($age);
// create 20 users, with ages from 1-20
$users = array();
for($i = 1; $i <= 20; $i++)
{
// create a row in the db with age = $i, and store a Phactory_Row object
$users[] = Phactory::create('user', array('age' => $i));
}
// test that getUserAge() returns the correct age for each user
foreach($users as $user)
{
// Phactory_Row provides getId() which returns the value of the PK column
$user_id = $user->getId();
$age = getUserAge(Phactory::getConnection(), $user_id);
$this->assertEqual($user->age, $age);
}
}
}
?>
What is the possible cause?
Thank you
Edit: I found out the problem, Phactory::getConnection() is returning NULL for some reason
Source: (StackOverflow)
I'm trying to get Phactory working with a Kohana v2.3.4 project. Here's what I have in one of my unit tests:
<?php
include_once ("test_index.php");
include_once ("/simpletest/autorun.php");
require_once '/Phactory/lib/Phactory/Sql/Phactory.php';
/**
* This is the function we will test.
* It should retrieve a user from the db by id,
* and return that user's age.
*
* @param PDO $pdo
* @param int $user_id
* @return mixed The age of the user, or false if no user
*/
function getUserAge($pdo, $user_id)
{
$stmt = $pdo->prepare("SELECT * FROM `users` WHERE `id` = ?");
$stmt->execute(array($user_id));
$user = $stmt->fetch();
if(false === $user) {
return false;
}
return $user['age'];
}
// class UserTest extends PHPUnit_Framework_TestCase
class UserTest extends UnitTestCase
{
public static function setUpBeforeClass()
{
// create a db connection and tell Phactory to use it
// $pdo = new PDO("sqlite:test.db");
$pdo = new PDO('mysql:host=127.0.0.1; dbname=testdb', 'root', '');
Phactory::setConnection($pdo);
/**
* Normally you would not need to create a table here, and would use
* an existing table in your test database instead.
* For the sake of creating a self-contained example, we create
* the 'users' table here.
*/
$pdo->exec("CREATE TABLE `users` ( id INTEGER PRIMARY KEY, name TEXT, age INTEGER )");
// reset any existing blueprints and empty any tables Phactory has used
Phactory::reset();
// define default values for each user we will create
Phactory::define('user', array('name' => 'Test User $n', 'age' => 18));
}
public static function tearDownAfterClass()
{
Phactory::reset();
// since we created a table in this test, we must drop it as well
Phactory::getConnection()->exec("DROP TABLE `users`");
}
public function testGetUserAge()
{
// test that getUserAge() returns false for a nonexistent user
$age = getUserAge(Phactory::getConnection(), 0);
$this->assertFalse($age);
// create 20 users, with ages from 1-20
$users = array();
for($i = 1; $i <= 20; $i++) {
// create a row in the db with age = $i, and store a Phactory_Row object
$users[] = Phactory::create('user', array('age' => $i));
}
// test that getUserAge() returns the correct age for each user
foreach($users as $user) {
// Phactory_Row provides getId() which returns the value of the PK column
$user_id = $user->getId();
$age = getUserAge(Phactory::getConnection(), $user_id);
$this->assertEquals($user->age, $age);
}
}
}
?>
The error I'm getting when I try to run that test is this:
Fatal error: Class 'Phactory' not found in C:\xampp\htdocs\kohana\application\controllers\phactory_test.php on line 66
How do I get this file to talk to Phactory?
Source: (StackOverflow)