public

  • Download Gist
ZF-3709_reproduction.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
<?php
 
/**
* This reproduction script shall accompany the issue reported at
* http://framework.zend.com/issues/browse/ZF-3709
*
* Assumptions:
* Zend_Db_Table_* from trunk
* PHP Environment has SQLite with :memory: capabilities
*
* Result:
* This script should run without any assertions failing (empty output)
*/
 
 
 
// ensure that Zend Framework trunk is being tested against & classes are available
// set_include_path('/path/to/ZendFramework/library'); // if needed
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
 
// setup the adapter, this uses SQLite so that its minimally invasive
// to anyone wishing to reproduce the issue on their local machine
$dbAdapter = Zend_Db::factory(
'Pdo_Sqlite',
array('dbname' => ':memory:')
);
 
// ensure all tables have access to the adapter
Zend_Db_Table::setDefaultAdapter($dbAdapter);
 
// setup the database, classes, & assertion system
setup();
 
 
 
/**
* BEGIN Reproduction Code
*/
 
 
 
// find a record that has a relationship to some bars through foo_to_bar
$fooTable = new Foo();
$fooRow = $fooTable->fetchRow('id = 2');
$fooIdOnesBars = $fooRow->findManyToManyRowset('Bar', 'FooToBar');
 
// the expected values for the next call
$expectedValues = array(
array('id' => '2', 'name' => 'bravo'),
array('id' => '3', 'name' => 'charlie')
);
 
 
// when we loop through the rows, they should match the expected results above
foreach ($fooIdOnesBars as $index => $barRow) {
// I'll use assert here to throw warnings when expected does not match actual
$actualValue = $barRow->toArray();
assert($expectedValues[$index] === $actualValue);
}
 
 
 
/**
* END Reproduction Code
*
* Supporting code below
*/
 
 
// setup function
function setup() {
setup_database();
setup_classes();
setup_assertions();
}
 
// This function will setup the proper database structure with test data
function setup_database() {
global $dbAdapter;
 
$conn = $dbAdapter->getConnection();
$conn->query('
CREATE TABLE foo (
id INTEGER PRIMARY KEY,
name VARCHAR(25)
);
');
 
foreach (array('one', 'two', 'three', 'four') as $numberName) {
$conn->query('INSERT INTO foo (name) VALUES ("' . $numberName . '");');
}
 
$conn->query('
CREATE TABLE bar (
id INTEGER PRIMARY KEY,
name VARCHAR(25));
');
foreach (array('alpha', 'bravo', 'charlie', 'delta') as $word) {
$conn->query('INSERT INTO bar (name) VALUES ("' . $word . '");');
}
 
$conn->query('
CREATE TABLE foo_to_bar (
id INTEGER PRIMARY KEY,
foo_id INTEGER,
bar_id INTEGER,
extra VARCHAR(20)
);
');
$datas = array(
array('foo_id' => 2, 'bar_id' => 2, 'extra' => 'Two to Two'),
array('foo_id' => 2, 'bar_id' => 3, 'extra' => 'Two to Three'),
array('foo_id' => 3, 'bar_id' => 4, 'extra' => 'Three to Four'),
);
foreach ($datas as $datum) {
$conn->query('INSERT INTO foo_to_bar '
. '(' . implode(',', array_keys($datum)) . ')'
. ' VALUES ("' . implode('", "', array_values($datum))
. '");');
}
}
 
// This function will define the proper Zend_Db_Tables and their relationships
function setup_classes() {
class Foo extends Zend_Db_Table_Abstract
{
protected $_name = 'foo';
}
class Bar extends Zend_Db_Table_Abstract
{
protected $_name = 'bar';
}
class FooToBar extends Zend_Db_Table_Abstract
{
protected $_name = 'foo_to_bar';
protected $_referenceMap = array(
'Foo' => array(
'columns' => 'foo_id',
'refTableClass' => 'Foo',
'refColumn' => 'id'
),
'Bar' => array(
'columns' => 'bar_id',
'refTableClass' => 'Bar',
'refColumn' => 'id'
)
);
}
}
 
// assertion setup
function setup_assertions() {
assert_options(ASSERT_ACTIVE, true);
assert_options(ASSERT_WARNING, false);
assert_options(ASSERT_CALLBACK, 'assert_failure');
}
 
// callback for assertion failures
function assert_failure() {
global $expectedValues, $index, $actualValue;
echo 'Was expecting an array that looked like:' . PHP_EOL;
var_dump($expectedValues[$index]);
echo 'But got array that looked like:' . PHP_EOL;
var_dump($actualValue);
echo PHP_EOL . PHP_EOL;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.