function testProperty(Connection $conn, \Murphy\Test $runner, $name, $initial, $additional) { $sel = new Select($conn); $sel->{$name}($initial); if ($sel->{$name}() == $initial) { $runner->pass(); } else { $runner->fail('Unable to set ' . $name . ' clause to ' . $initial); } if ($additional) { $sel->{$name}($sel->{$name}() . $additional); if ($sel->{$name}() == $initial . $additional) { $runner->pass(); } else { $runner->fail('Unable to update ' . $name . ' clause to ' . $initial . $additional); } } }
\Murphy\Test::add(function ($runner) { \Murphy\Fixture::load(dirname(__FILE__) . '/../on_clause.class.php.murphy/fixture.php')->execute(); $conn = new Connection('localhost', 'plusql', 'plusql', 'plusql'); $conn->connect(); $query = new Query('SELECT * FROM strong_guy', $conn->link()); if ($query->strong_guy instanceof QueryIterator) { $runner->pass(); } else { $runner->fail('Did not get a QueryIterator for a table we should have been able to'); } try { $query->non_existant; $runner->fail('Why was I able to try and get a non-existant member variable that isn\'t a valid table or field?'); } catch (TableInspectorException $exc) { $runner->pass(); } $row = $query->nextRow(); if (count($row)) { $runner->fail('You called nextRow() on a query from which you\'d already extracted a QueryIterator - you should only be able to call rowAtIndex'); } $row = $query->rowAtIndex(0); if ($row['strong_name'] == 'Strongy Strongo') { $runner->pass(); } else { $runner->fail('The row returned from your query did not contain the right data'); } $query = new Query('SELECT * FROM strong_guy', $conn->link()); $row = $query->nextRow(); if ($row['strong_name'] == 'Strongy Strongo') { $runner->pass(); } else { $runner->fail('The row returned from your query did not contain the right data'); } });
\Murphy\Test::add(function ($runner) { $sample_values = array(3, 3.5, '3,5', 3.55, 20120101131313.0, 20120101, '2012-01-01 13:13:13', 0, '', 'this is a string', '1234567890-09876543456y7uioijuhygfdszxcvbnjml,;/.,mn,.,mnbnm\'\\"\'\'\\\'\\\\"[]}{].......3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3=-0987654321!@#$%^&*(~~!@#$%^&*()+_)OP{}|}{P;\';lkl;KJHGVBCDRFGHbvcxszaswsadfGVBHN'); $sample_queries = array('INSERT INTO `type_test`(`int_auto_field`,`varchar_field_default_null`,`varchar_field_default_something`,`int_field_default_10`,`int_field_default_null`,`float_field_default_null`,`float_field_default_2point5`,`double_field_default_null`,`double_field_default_2point555`,`decimal_field_default_null`,`decimal_field_default_10point2`,`datetime_field_default_null`,`datetime_field_default_something`) VALUES(3,\'3\',\'3\',3,3,3,3,3,3,\'3\',\'3\',\'3\',\'3\')', 'INSERT INTO `type_test`(`int_auto_field`,`varchar_field_default_null`,`varchar_field_default_something`,`int_field_default_10`,`int_field_default_null`,`float_field_default_null`,`float_field_default_2point5`,`double_field_default_null`,`double_field_default_2point555`,`decimal_field_default_null`,`decimal_field_default_10point2`,`datetime_field_default_null`,`datetime_field_default_something`) VALUES(3,\'3.5\',\'3.5\',3,3,3.5,3.5,3.5,3.5,\'3.5\',\'3.5\',\'3.5\',\'3.5\')', 'INSERT INTO `type_test`(`int_auto_field`,`varchar_field_default_null`,`varchar_field_default_something`,`int_field_default_10`,`int_field_default_null`,`float_field_default_null`,`float_field_default_2point5`,`double_field_default_null`,`double_field_default_2point555`,`decimal_field_default_null`,`decimal_field_default_10point2`,`datetime_field_default_null`,`datetime_field_default_something`) VALUES(3,\'3,5\',\'3,5\',3,3,35,35,35,35,\'3,5\',\'3,5\',\'3,5\',\'3,5\')', 'INSERT INTO `type_test`(`int_auto_field`,`varchar_field_default_null`,`varchar_field_default_something`,`int_field_default_10`,`int_field_default_null`,`float_field_default_null`,`float_field_default_2point5`,`double_field_default_null`,`double_field_default_2point555`,`decimal_field_default_null`,`decimal_field_default_10point2`,`datetime_field_default_null`,`datetime_field_default_something`) VALUES(3,\'3.55\',\'3.55\',3,3,3.55,3.55,3.55,3.55,\'3.55\',\'3.55\',\'3.55\',\'3.55\')', 'INSERT INTO `type_test`(`int_auto_field`,`varchar_field_default_null`,`varchar_field_default_something`,`int_field_default_10`,`int_field_default_null`,`float_field_default_null`,`float_field_default_2point5`,`double_field_default_null`,`double_field_default_2point555`,`decimal_field_default_null`,`decimal_field_default_10point2`,`datetime_field_default_null`,`datetime_field_default_something`) VALUES(20120101131313,\'20120101131313\',\'20120101131313\',20120101131313,20120101131313,20120101131313,20120101131313,20120101131313,20120101131313,\'20120101131313\',\'20120101131313\',\'20120101131313\',\'20120101131313\')', 'INSERT INTO `type_test`(`int_auto_field`,`varchar_field_default_null`,`varchar_field_default_something`,`int_field_default_10`,`int_field_default_null`,`float_field_default_null`,`float_field_default_2point5`,`double_field_default_null`,`double_field_default_2point555`,`decimal_field_default_null`,`decimal_field_default_10point2`,`datetime_field_default_null`,`datetime_field_default_something`) VALUES(20120101,\'20120101\',\'20120101\',20120101,20120101,20120101,20120101,20120101,20120101,\'20120101\',\'20120101\',\'20120101\',\'20120101\')', 'INSERT INTO `type_test`(`int_auto_field`,`varchar_field_default_null`,`varchar_field_default_something`,`int_field_default_10`,`int_field_default_null`,`float_field_default_null`,`float_field_default_2point5`,`double_field_default_null`,`double_field_default_2point555`,`decimal_field_default_null`,`decimal_field_default_10point2`,`datetime_field_default_null`,`datetime_field_default_something`) VALUES(2012,\'2012-01-01 13:13:13\',\'2012-01-01 13:13:13\',2012,2012,20120101131313,20120101131313,20120101131313,20120101131313,\'2012-01-01 13:13:13\',\'2012-01-01 13:13:13\',\'2012-01-01 13:13:13\',\'2012-01-01 13:13:13\')', 'INSERT INTO `type_test`(`int_auto_field`,`varchar_field_default_null`,`varchar_field_default_something`,`int_field_default_10`,`int_field_default_null`,`float_field_default_null`,`float_field_default_2point5`,`double_field_default_null`,`double_field_default_2point555`,`decimal_field_default_null`,`decimal_field_default_10point2`,`datetime_field_default_null`,`datetime_field_default_something`) VALUES(0,\'0\',\'0\',0,0,0,0,0,0,\'0\',\'0\',\'0\',\'0\')', 'INSERT INTO `type_test`(`int_auto_field`,`varchar_field_default_null`,`varchar_field_default_something`,`int_field_default_10`,`int_field_default_null`,`float_field_default_null`,`float_field_default_2point5`,`double_field_default_null`,`double_field_default_2point555`,`decimal_field_default_null`,`decimal_field_default_10point2`,`datetime_field_default_null`,`datetime_field_default_something`) VALUES(0,\'\',\'\',0,0,0,0,0,0,\'\',\'\',\'\',\'\')', 'INSERT INTO `type_test`(`int_auto_field`,`varchar_field_default_null`,`varchar_field_default_something`,`int_field_default_10`,`int_field_default_null`,`float_field_default_null`,`float_field_default_2point5`,`double_field_default_null`,`double_field_default_2point555`,`decimal_field_default_null`,`decimal_field_default_10point2`,`datetime_field_default_null`,`datetime_field_default_something`) VALUES(0,\'this is a string\',\'this is a string\',0,0,0,0,0,0,\'this is a string\',\'this is a string\',\'this is a string\',\'this is a string\')', 'INSERT INTO `type_test`(`int_auto_field`,`varchar_field_default_null`,`varchar_field_default_something`,`int_field_default_10`,`int_field_default_null`,`float_field_default_null`,`float_field_default_2point5`,`double_field_default_null`,`double_field_default_2point555`,`decimal_field_default_null`,`decimal_field_default_10point2`,`datetime_field_default_null`,`datetime_field_default_something`) VALUES(1234567890,\'1234567890-09876543456y7uioijuhygfdszxcvbnjml,;/.,mn,.,mnbnm\\\'\\\\\\"\\\'\\\'\\\\\\\'\\\\\\\\\\"[]}{].......3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3=-0987654321!@#$%^&*(~~!@#$%^&*()+_)OP{}|}{P;\\\';lkl;KJHGVBCDRFGHbvcxszaswsadfGVBHN\',\'1234567890-09876543456y7uioijuhygfdszxcvbnjml,;/.,mn,.,mnbnm\\\'\\\\\\"\\\'\\\'\\\\\\\'\\\\\\\\\\"[]}{].......3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3=-0987654321!@#$%^&*(~~!@#$%^&*()+_)OP{}|}{P;\\\';lkl;KJHGVBCDRFGHbvcxszaswsadfGVBHN\',1234567890,1234567890,1234567890098765434567.33333333333333330987654321,1234567890098765434567.33333333333333330987654321,1234567890098765434567.33333333333333330987654321,1234567890098765434567.33333333333333330987654321,\'1234567890-09876543456y7uioijuhygfdszxcvbnjml,;/.,mn,.,mnbnm\\\'\\\\\\"\\\'\\\'\\\\\\\'\\\\\\\\\\"[]}{].......3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3=-0987654321!@#$%^&*(~~!@#$%^&*()+_)OP{}|}{P;\\\';lkl;KJHGVBCDRFGHbvcxszaswsadfGVBHN\',\'1234567890-09876543456y7uioijuhygfdszxcvbnjml,;/.,mn,.,mnbnm\\\'\\\\\\"\\\'\\\'\\\\\\\'\\\\\\\\\\"[]}{].......3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3=-0987654321!@#$%^&*(~~!@#$%^&*()+_)OP{}|}{P;\\\';lkl;KJHGVBCDRFGHbvcxszaswsadfGVBHN\',\'1234567890-09876543456y7uioijuhygfdszxcvbnjml,;/.,mn,.,mnbnm\\\'\\\\\\"\\\'\\\'\\\\\\\'\\\\\\\\\\"[]}{].......3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3=-0987654321!@#$%^&*(~~!@#$%^&*()+_)OP{}|}{P;\\\';lkl;KJHGVBCDRFGHbvcxszaswsadfGVBHN\',\'1234567890-09876543456y7uioijuhygfdszxcvbnjml,;/.,mn,.,mnbnm\\\'\\\\\\"\\\'\\\'\\\\\\\'\\\\\\\\\\"[]}{].......3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3=-0987654321!@#$%^&*(~~!@#$%^&*()+_)OP{}|}{P;\\\';lkl;KJHGVBCDRFGHbvcxszaswsadfGVBHN\')'); $field_names = array('int_auto_field', 'varchar_field_default_null', 'varchar_field_default_something', 'int_field_default_10', 'int_field_default_null', 'float_field_default_null', 'float_field_default_2point5', 'double_field_default_null', 'double_field_default_2point555', 'decimal_field_default_null', 'decimal_field_default_10point2', 'datetime_field_default_null', 'datetime_field_default_something'); foreach ($sample_values as $index => $sv) { $expected_query = $sample_queries[$index]; \Murphy\Fixture::load(dirname(__FILE__) . '/fixture.php')->execute(); $to = 'plusql'; $conn = new Connection('localhost', $to, $to, $to); $conn->connect(); $ins = new Insert($conn); $cur_array = array(); foreach ($field_names as $fn) { $cur_array[$fn] = $sv; } $sql = $ins->type_test($cur_array)->insertSql(); //WE SHOULDN'T GET ANY SQL ERRORS - INJECTION FREE $conn->query($sql); if ($sql !== $expected_query) { $runner->fail('The sample value: ' . $sv . ' produced an unexpected query result: ' . $sql . ' !== ' . $expected_query); } else { $runner->pass(); } } $cur_array = array('does', 'not', 'exist'); $ins = new Insert($conn); try { echo 'is: ' . $ins->type_test($cur_array)->insertSql(); $runner->fail('Why did we not get an exception of type InvalidInsertQueryException?'); } catch (InvalidInsertQueryException $exc) { $runner->pass(); } });
\Murphy\Test::add(function ($runner) { $conn = NULL; \Murphy\Fixture::load(dirname(__FILE__) . '/../on_clause.class.php.murphy/fixture.php')->execute(); \Murphy\Fixture::load(dirname(__FILE__) . '/../query_iterator.class.php.murphy/fixture.php')->execute(function ($aliases) use(&$conn) { $deets = $aliases['plusql']; $conn = new Connection($deets[0], $deets[1], $deets[2], $deets[3]); $conn->connect(); }); $query = new Query('SELECT * FROM strong_guy', $conn->link()); $row = new QueryRow($query, 'strong_guy', 0); if ($row->keySignature() == 2) { $runner->pass(); } else { $runner->fail('Got the incorrect key signature for the strong_guy at index 0'); } if ($row->strong_name == 'Strong 1') { $runner->pass(); } else { $runner->fail('Got the wrong strong_name for strong_guy at index 0'); } $query = new Query('SELECT * FROM weak_guy', $conn->link()); $row = new QueryRow($query, 'weak_guy', 0); if ($row->keySignature() == '2::1') { $runner->pass(); } else { $runner->fail('Did not get correct key signature for weak_guy at index 0'); } try { $row->nothing_here; $runner->fail('Why was I able to get something that does not exist?'); } catch (InvalidQueryRowException $exc) { $runner->pass(); } if ($row->weak_name == 'Weak 1') { $runner->pass(); } else { $runner->fail('Did not get the correct name for weak_name at index 0'); } try { $new_iterator = $row->strong_guy; $runner->pass(); } catch (InvalidQueryRowException $exc) { $runner->fail('We should have been able to get an iterator for strong_guy because the id was present'); } });
<?php namespace PluSQL; use Plusql, mysqli; \Murphy\Test::add(function ($runner) { \Murphy\Fixture::load(dirname(__FILE__) . '/fixture.php')->execute(); $to = 'plusql'; $conn = new Connection('localhost', $to, $to, $to); $conn->connect(); $ins = new Insert($conn); $field_names = array('int_auto_field', 'varchar_field_default_null', 'varchar_field_default_something', 'int_field_default_10', 'int_field_default_null', 'float_field_default_null', 'float_field_default_2point5', 'double_field_default_null', 'double_field_default_2point555', 'decimal_field_default_null', 'decimal_field_default_10point2', 'datetime_field_default_null', 'datetime_field_default_something'); $ins->type_test(array('varchar_field_default_null' => new SqlFunction('REPLACE(\'onetwo\',\'one\',\'two\')')))->insert(); Plusql::credentials('live', array('localhost', 'plusql', 'plusql', 'plusql')); if (Plusql::from('live')->type_test->select('int_auto_field,varchar_field_default_null')->run()->type_test->varchar_field_default_null != 'twotwo') { $runner->fail('The REPLACE aggregate function did not work'); } else { $runner->pass(); } });
\Murphy\Test::add(function ($runner) { \Murphy\Fixture::load(dirname(__FILE__) . '/../on_clause.class.php.murphy/fixture.php')->execute(); Plusql::credentials('live', array('localhost', 'plusql', 'plusql', 'plusql')); /* mysql_query('TRUNCATE weak_guy'); //LOOP THROUGH $for_strong_guy = 1; $stmt = Plusql::into('live'); $names = array('Iain\'s','Another name'); foreach($names as $name) $stmt->weak_guy(array('strong_guy_id' => $for_strong_guy,'weak_name' => $name)); $stmt->insert(); $expected = array('1:1:Iain\'s', '2:1:Another name'); $actual = array(); foreach(Plusql::from('live')->weak_guy->select('weak_guy_id,strong_guy_id,weak_name')->run()->weak_guy as $row) $actual[] = $row->weak_guy_id.':'.$row->strong_guy_id.':'.$row->weak_name; if(serialize($expected) != serialize($actual)) $runner->fail('Did not get the expected data back out after inserting multiple'); else $runner->pass(); */ mysql_query('TRUNCATE weak_guy'); //LET'S TRY A JAGGED ARRAY $one = array('weak_name' => 'Only the name', 'french_guy_id' => 10); $two = array('strong_guy_id' => 1, 'weak_name' => 'With strong id'); $ins = Plusql::into('live'); $ins->weak_guy($one); $ins->weak_guy($two); $ins->insert(); $expected = array('0:1:Only the name:10', '1:1:With strong id:0'); $actual = array(); foreach (Plusql::from('live')->weak_guy->select('strong_guy_id,weak_guy_id,weak_name,french_guy_id')->run()->weak_guy as $wg) { $actual[] = $wg->strong_guy_id . ':' . $wg->weak_guy_id . ':' . $wg->weak_name . ':' . $wg->french_guy_id; } if (serialize($expected) != serialize($actual)) { $runner->fail('Did not get the expected data back out after inserting jagged'); } else { $runner->pass(); } mysql_query('TRUNCATE weak_guy'); //YOU CAN ALSO JUST INSERT A SINGLE RECORD Plusql::into('live')->weak_guy(array('strong_guy_id' => 1, 'weak_name' => 'Ron Weakly'))->insert(); if (Plusql::from('live')->weak_guy->select('strong_guy_id,weak_guy_id,weak_name')->run()->weak_guy->weak_name != 'Ron Weakly') { $runner->fail('Did not get expected value back after single insert'); } else { $runner->pass(); } mysql_query('TRUNCATE weak_guy'); Plusql::into('live')->weak_guy(array('strong_guy_id' => 1, 'weak_name' => 'Ron Weakly'))->insert(function ($link, $field, $value) { if ($link instanceof mysqli) { $ret = $link->escape_string($value); } else { $ret = mysql_real_escape_string($value, $link); } if ($field['Type'] != 'int(10)') { $ret = '\'' . $ret . '\''; } return $ret; }); if (Plusql::from('live')->weak_guy->select('strong_guy_id,weak_guy_id,weak_name')->run()->weak_guy->weak_name != 'Ron Weakly') { $runner->fail('Did not get expected value back after single insert with custom filter'); } else { $runner->pass(); } });
\Murphy\Test::add(function ($runner) { $table = new Table('strong_guy'); if ($table->name() == 'strong_guy') { $runner->pass(); } else { $runner->fail('Table name not set correctly in constructor'); } $table->setJoinType(Table::INNER_JOIN); if ($table->joinType() == 'INNER JOIN') { $runner->pass(); } else { $runner->fail('Unable to set join type'); } $table = new Table('strong_guy'); $table2 = new Table('weak_guy'); $table->joinTable($table2); if ($table2->joinType() == 'INNER JOIN') { $runner->pass(); } else { $runner->fail('The join type was not set to INNER JOIN by default'); } ob_start(); print_r($table->joinTo()); $actual = ob_get_clean(); $expected = 'Array ( [weak_guy] => PluSQL\\Table Object ( [name:PluSQL\\Table:private] => weak_guy [join_to:PluSQL\\Table:private] => Array ( ) [join_type:PluSQL\\Table:private] => INNER JOIN ) ) '; if ($actual == $expected) { $runner->pass(); } else { $runner->fail('After joining a table, the resulting joinTo() was not what we expected'); } });
* having * limit */ \Murphy\Test::add(function ($runner) { $conn = getConnection(); $sel = new Select($conn); $properties = array('select' => array('strong_name', ',weak_name'), 'where' => array('strong_name = \'Strongy Strongo\'', ' AND weak_name = \'Weak Guy 1\''), 'groupBy' => array('strong_guy_id', ',weak_guy_id'), 'having' => array('strong_guy_id > 4', ' AND weak_guy_id = 7'), 'orderBy' => array('strong_guy_id ASC', ',weak_guy_id DESC'), 'limit' => array('100', NULL)); foreach ($properties as $name => $data) { testProperty($conn, $runner, $name, $data[0], $data[1]); } }); /** * Test an empty where clause */ \Murphy\Test::add(function ($runner) { $conn = getConnection(); $sel = new Select($conn); (string) $sel->strong_guy; }); /** * Now just do a bit of an end-to-end test building a big fuckoff query */ \Murphy\Test::add(function ($runner) { $sel = new Select(getConnection()); $query = (string) $sel->strong_guy->weak_guy->rogue_guy('weak_guy')->french_guy->select('strong_guy.strong_name,weak_guy.weak_name,rogue_guy.rogue_name,french_guy.french_name')->where('strong_guy.strong_guy_id > 1')->groupBy('strong_guy.strong_guy_id')->having('weak_guy_id > 1')->orderBy('strong_guy.strong_guy_id,weak_guy.weak_guy_id')->limit('100'); if ($query == 'SELECT strong_guy.strong_name,weak_guy.weak_name,rogue_guy.rogue_name,french_guy.french_name FROM strong_guy INNER JOIN weak_guy ON strong_guy.strong_guy_id = weak_guy.strong_guy_id INNER JOIN is_rogue ON weak_guy.strong_guy_id = is_rogue.strong_guy_id AND weak_guy.weak_guy_id = is_rogue.weak_guy_id INNER JOIN rogue_guy ON is_rogue.rogue_guy_id = rogue_guy.rogue_guy_id INNER JOIN french_guy ON weak_guy.french_guy_id = french_guy.french_guy_id WHERE strong_guy.strong_guy_id > 1 GROUP BY strong_guy.strong_guy_id HAVING weak_guy_id > 1 ORDER BY strong_guy.strong_guy_id,weak_guy.weak_guy_id LIMIT 100') { $runner->pass(); } else { $runner->fail('Unexpected output from big fuckoff query using Select'); } });
<?php namespace PluSQL; use Plusql; require dirname(__FILE__) . '/functions.php'; /** * Test if we can do a left join */ \Murphy\Test::add(function ($runner) { $sel = new Select(getConnection()); $query = (string) $sel->strong_guy->weak_guy->joinType(Table::LEFT_JOIN)->rogue_guy('weak_guy')->joinType(Table::LEFT_JOIN)->french_guy->joinType(Table::LEFT_JOIN)->select('strong_guy.strong_name,weak_guy.weak_name,rogue_guy.rogue_name,french_guy.french_name')->where('strong_guy.strong_guy_id > 1'); if ($query == 'SELECT strong_guy.strong_name,weak_guy.weak_name,rogue_guy.rogue_name,french_guy.french_name FROM strong_guy LEFT JOIN weak_guy ON (strong_guy.strong_guy_id = weak_guy.strong_guy_id OR (weak_guy.strong_guy_id IS NULL)) LEFT JOIN is_rogue ON (weak_guy.strong_guy_id = is_rogue.strong_guy_id AND weak_guy.weak_guy_id = is_rogue.weak_guy_id OR (is_rogue.strong_guy_id IS NULL AND is_rogue.weak_guy_id IS NULL)) LEFT JOIN rogue_guy ON (is_rogue.rogue_guy_id = rogue_guy.rogue_guy_id OR (rogue_guy.rogue_guy_id IS NULL)) LEFT JOIN french_guy ON (weak_guy.french_guy_id = french_guy.french_guy_id OR (french_guy.french_guy_id IS NULL)) WHERE strong_guy.strong_guy_id > 1') { $runner->pass(); } else { $runner->fail('Left joins do not work'); } });
\Murphy\Test::add(function ($runner) { $conn = NULL; \Murphy\Fixture::load(dirname(__FILE__) . '/../on_clause.class.php.murphy/fixture.php')->execute(); \Murphy\Fixture::load(dirname(__FILE__) . '/../query_iterator.class.php.murphy/fixture.php')->execute(function ($aliases) use(&$conn) { $aliases = $aliases['plusql']; $host = $aliases[0]; $username = $aliases[1]; $password = $aliases[2]; $dbname = $aliases[3]; $conn = new Connection($host, $username, $password, $dbname); $conn->connect(); }); $worker = new TableInspectorWorker('strong_guy', $conn->link()); $expected = 'bd3251f7c5acccb2b73fd83be63c7ea2'; ob_start(); print_r($worker->allFields()); $actual = md5(trim(ob_get_clean())); if ($actual == $expected) { $runner->pass(); } else { $runner->fail('Did not get the correct field data from TableInspectorWorker'); } $worker = new TableInspectorWorker('strong_guy', $conn->link()); $expected = '1b656c3c43833bf1ac9cf5620643522f'; ob_start(); print_r($worker->primaryKeys()); $actual = md5(trim(ob_get_clean())); if ($actual == $expected) { $runner->pass(); } else { $runner->fail('TableInspectorWorker::primaryKeys did not return the correct fields'); } });
$aliases = $aliases['plusql']; $host = $aliases[0]; $username = $aliases[1]; $password = $aliases[2]; $dbname = $aliases[3]; $conn = new Connection($host, $username, $password, $dbname); $conn->connect(); }); $one = TableInspector::forTable('strong_guy', $conn->link()); $two = TableInspector::forTable('strong_guy', $conn->link()); if ($one === $two) { $runner->pass(); } else { $runner->fail('TableInspector::forTable not returning the same object for the same table on the same link'); } }); /** * Test that a different able is returned for the same name with different database links */ \Murphy\Test::add(function ($runner) { \Murphy\Fixture::load(dirname(__FILE__) . '/fixture.php')->execute(); $conn1 = new Connection('localhost', 'plusql_one', 'plusql_one', 'plusql_one'); $conn2 = new Connection('localhost', 'plusql_two', 'plusql_two', 'plusql_two'); $one = TableInspector::forTable('strong_guy', $conn1->link()); $two = TableInspector::forTable('strong_guy', $conn2->link()); if ($one !== $two) { $runner->pass(); } else { $runner->fail('TableInspector::forTable not returning the same object for the same table on the same link'); } });
\Murphy\Test::add(function ($runner) { \Murphy\Fixture::load(dirname(__FILE__) . '/fixture.php')->execute(); $conn = new Connection('localhost', 'plusql', 'plusql', 'plusql'); $conn->connect(); //test strong to weak $clause = new OnClause($conn->link(), 'strong_guy', 'weak_guy'); if ($clause->toString() == 'strong_guy.strong_guy_id = weak_guy.strong_guy_id') { $runner->pass(); } else { $runner->fail('The on clause for strong -> weak dependency isn\'t working right'); } //test weak to strong $clause = new OnClause($conn->link(), 'weak_guy', 'strong_guy'); if ($clause->toString() == 'weak_guy.strong_guy_id = strong_guy.strong_guy_id') { $runner->pass(); } else { $runner->fail('The on clause for weak -> strong dependency isn\'t working right'); } //test foreign $clause = new OnClause($conn->link(), 'weak_guy', 'french_guy'); if ($clause->toString() == 'weak_guy.french_guy_id = french_guy.french_guy_id') { $runner->pass(); } else { $runner->fail('The on clause for foreign relationships isn\'t working right'); } //test many to many join with composite primary key try { $clause = new OnClause($conn->link(), 'weak_guy', 'rogue_guy'); $clause->toString(); $runner->fail('You should have seen a ManyToManyJoinException for weak -> rogue guy'); } catch (ManyToManyJoinException $exc) { if ($exc->joiningTable()->name() == 'is_rogue') { $runner->pass(); } else { $runner->fail('You got a many to many join for the weak -> rogue guys but it wasn\'t the right table, it was: ' . $exc->joiningTable()); } } //we should not be able to join strong guy directly to rogue guy try { $clause = new OnClause($conn->link(), 'strong_guy', 'rogue_guy'); $clause->toString(); $runner->fail('Why were we able to join strong_guy directly to rogue_guy?'); } catch (UnableToDetermineOnClauseException $exc) { $runner->pass(); } });
\Murphy\Test::add(function ($runner) { $conn = NULL; \Murphy\Fixture::load(dirname(__FILE__) . '/../on_clause.class.php.murphy/fixture.php')->execute(); \Murphy\Fixture::load(dirname(__FILE__) . '/../query_iterator.class.php.murphy/fixture.php')->execute(function ($aliases) use(&$conn) { $aliases = $aliases['plusql']; $host = $aliases[0]; $username = $aliases[1]; $password = $aliases[2]; $dbname = $aliases[3]; $conn = new Connection($host, $username, $password, $dbname); $conn->connect(); }); $update = new Update($conn); $strong = array('strong_name' => 'This\'s it'); try { $update->strong_guy($strong)->update(); $runner->fail('Why were we able to update the whole table?'); } catch (UnsafeUpdateException $exc) { $runner->pass(); } $update->where(Update::ENTIRE_TABLE)->update(); $sel = new Select($conn); $expected = array('This\'s it', 'This\'s it'); $actual = array(); foreach ($sel->strong_guy->select('*')->run()->strong_guy as $sg) { $actual[] = $sg->strong_name; } if (serialize($expected) !== serialize($actual)) { $runner->fail('Did not manage to update the entire table'); } else { $runner->pass(); } \Murphy\Fixture::load(dirname(__FILE__) . '/../query_iterator.class.php.murphy/fixture.php')->execute(function ($aliases) use(&$conn) { $aliases = $aliases['plusql']; $host = $aliases[0]; $username = $aliases[1]; $password = $aliases[2]; $dbname = $aliases[3]; $conn = new Connection($host, $username, $password, $dbname); $conn->connect(); }); $update = new Update($conn); $strong = array('strong_name' => 'This\'s it'); $update->strong_guy($strong)->where('strong_name = \'Strong 1\'')->update(); $expected = array('This\'s it', 'Strong 2'); $actual = array(); foreach ($sel->strong_guy->select('*')->run()->strong_guy as $sg) { $actual[] = $sg->strong_name; } if (serialize($expected) !== serialize($actual)) { $runner->fail('Did not manage to update Strong Name 1'); } else { $runner->pass(); } });
\Murphy\Test::add(function ($runner) { \Murphy\Fixture::load(dirname(__FILE__) . '/../on_clause.class.php.murphy/fixture.php')->execute(); $conn = new Connection('localhost', 'plusql', 'plusql', 'plusql'); $conn->connect(); $query = new Query('SELECT * FROM strong_guy', $conn->link()); $iterator = new QueryIterator($query, 'strong_guy', 0); $pairs = array('strong_guy_id' => 1); $iterator->constrainKeys($pairs); if ($iterator->strong_name == 'Strongy Strongo') { $runner->pass(); } else { $runner->fail('Could not get value for strong_name from strong_guy iterator'); } $conn = NULL; \Murphy\Fixture::load(dirname(__FILE__) . '/../on_clause.class.php.murphy/fixture.php')->also(dirname(__FILE__) . '/fixture.php')->execute(function ($aliases) use(&$conn) { $deets = $aliases['plusql']; $conn = new Connection($deets[0], $deets[1], $deets[2], $deets[3]); $conn->connect(); }); $expected_output = trim(' Strong 1:Weak 1:Rogue 1:French 1 Strong 1:Weak 2:Rogue 1:French 1 Strong 2:Weak 3:Rogue 1:French 2 Strong 2:Weak 3:Rogue 2:French 2 Strong 2:Weak 4:Rogue 2:French 2 '); $query = new Query('SELECT * FROM strong_guy INNER JOIN weak_guy USING(strong_guy_id) INNER JOIN is_rogue USING(strong_guy_id,weak_guy_id) INNER JOIN rogue_guy USING (rogue_guy_id) INNER JOIN french_guy USING(french_guy_id) ORDER BY strong_guy_id,weak_guy_id,rogue_guy_id', $conn->link()); $iterator = new QueryIterator($query, 'strong_guy', 0); $start = microtime(true); ob_start(); foreach ($iterator as $sg) { foreach ($sg->weak_guy as $wg) { foreach ($wg->rogue_guy as $rg) { echo $sg->strong_name . ':' . $wg->weak_name . ':' . $rg->rogue_name . ':' . $wg->french_guy->french_name . PHP_EOL; } } } $actual = trim(ob_get_clean()); if ($actual == $expected_output) { $runner->pass(); } else { $runner->fail('The output was unexpected. We expected: ' . $expected_output . ' but got: ' . $actual); } });
\Murphy\Test::add(function ($runner) { \Murphy\Fixture::load(dirname(__FILE__) . '/../on_clause.class.php.murphy/fixture.php')->execute(); $to = 'plusql'; $conn = new Connection('localhost', $to, $to, $to); $conn->connect(); $ins = new Insert($conn); //THE ARRAY PASSED SHOULD BE ABLE TO HAVE KEYS THAT DON'T EXIST IN GIVEN ENTITY $ins->weak_guy(array('strong_guy_id' => 1, 'weak_name' => 'Weaky Weakling\'s', 'nothing' => 'Nowhere')); $test = 'INSERT INTO `weak_guy`(`strong_guy_id`,`weak_name`) VALUES(1,\'Weaky Weakling\\\'s\')'; if ($ins->insertSql() == $test) { $runner->pass(); } else { $runner->fail('Insert sql incorrectly rendered: ' . $ins->insertSql()); } //SHOULD BE ABLE TO GENERATE THE SQL A BUNCH OF TIMES if ($ins->insertSql() == $test) { $runner->pass(); } else { $runner->fail('Insert sql was rendered differently second time around'); } $ins = new Insert($conn); $filter = function ($link, $field, $value) { return str_replace('2nd', '3rd', $value); }; $ins->weak_guy(array('strong_guy_id' => 1, 'weak_name' => '\'Winkly Weakling The 2nd\'')); $test = 'INSERT INTO `weak_guy`(`strong_guy_id`,`weak_name`) VALUES(1,\'Winkly Weakling The 3rd\')'; if ($ins->insertSql($filter) === $test) { $runner->pass(); } else { $runner->fail('Unable to provide custom filter'); } });