<?php /** * dbname * plusql_one * plusql_two */ Murphy\Fixture::add(function ($row) { if (!($mysql_root = Args::get('mysql_root', Args::argv))) { die('You need to pass in mysql_root'); } mysql_connect('localhost', 'root', $mysql_root) or die(mysql_error()); $dbname = $row['dbname']; $dbuser = $dbname; mysql_query('DROP DATABASE IF EXISTS `' . mysql_real_escape_string($dbname) . '`'); mysql_query('CREATE DATABASE `' . mysql_real_escape_string($dbname) . '`') or die(mysql_error()); mysql_query('GRANT ALL ON `' . mysql_real_escape_string($dbname) . '`.* TO \'' . mysql_real_escape_string($dbuser) . '\'@\'localhost\' IDENTIFIED BY \'' . mysql_real_escape_string($dbuser) . '\'') or die(mysql_error()); mysql_select_db($dbname); mysql_query(' CREATE TABLE `strong_guy` ( `strong_guy_id` int(10) NOT NULL AUTO_INCREMENT, `strong_name` varchar(20) DEFAULT NULL, PRIMARY KEY(strong_guy_id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1') or die(mysql_error()); });
<?php /** * user_id | user_email | group_id | group_name * 1 | iain@workingsoftware.com.au | 2 | Users * 2 | iaindooley@gmail.com | 3 | Staff */ Murphy\Fixture::add(function ($data) { echo 'Non db fixture 2' . PHP_EOL; });
<?php /** * @database test_fixture2 * @tables user,group,user_in_group * user_id | username | group_id | group_name * 1 | iain@workingsoftware.com.au | 2 | Users * 2 | iaindooley@gmail.com | 3 | Staff */ Murphy\Fixture::add(function ($data) { $this->link->query('INSERT INTO `group`(group_id,group_name) VALUES(' . (int) $data['group_id'] . ',\'' . mysqli::real_escape_string($data['group_name']) . '\')') or die(mysqli_error($this->link)); $this->link->query('INSERT INTO `user`(user_id,username) VALUES(' . (int) $data['user_id'] . ',\'' . mysqli::real_escape_string($data['username']) . '\')') or die(mysqli_error($this->link)); $this->link->query('INSERT INTO user_in_group(user_id,group_id) VALUES(' . (int) $data['user_id'] . ',' . (int) $data['group_id'] . ')') or die(mysqli_error($this->link)); });
Murphy\Fixture::add(function ($row) { if (!($mysql_root = Args::get('mysql_root', Args::argv))) { die('You need to pass in mysql_root'); } mysql_connect('localhost', 'root', $mysql_root) or die(mysql_error()); $dbname = $row['dbname']; $dbuser = $dbname; mysql_query('DROP DATABASE IF EXISTS `' . mysql_real_escape_string($dbname) . '`'); mysql_query('CREATE DATABASE `' . mysql_real_escape_string($dbname) . '`') or die(mysql_error()); mysql_query('GRANT ALL ON `' . mysql_real_escape_string($dbname) . '`.* TO \'' . mysql_real_escape_string($dbuser) . '\'@\'localhost\' IDENTIFIED BY \'' . mysql_real_escape_string($dbuser) . '\'') or die(mysql_error()); mysql_select_db($dbname); //create tables with composite keys so that: // //strong_guy -supports-> weak_guy //weak_guy -has foreign-> french_guy //weak_guy -many to many-> rogue_guy (with a joining table is_rogue) mysql_query(' CREATE TABLE `strong_guy` ( `strong_guy_id` int(10) NOT NULL AUTO_INCREMENT, `strong_name` varchar(20) DEFAULT NULL, PRIMARY KEY(strong_guy_id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1') or die(mysql_error()); mysql_query(' CREATE TABLE `weak_guy` ( `strong_guy_id` int(10) NOT NULL DEFAULT \'0\', `weak_guy_id` int(10) NOT NULL AUTO_INCREMENT, `weak_name` varchar(20) DEFAULT NULL, `french_guy_id` int(10) NOT NULL DEFAULT \'0\', PRIMARY KEY(strong_guy_id,weak_guy_id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1') or die(mysql_error()); mysql_query(' CREATE TABLE `french_guy` ( `french_guy_id` int(10) NOT NULL AUTO_INCREMENT, `french_name` varchar(20) DEFAULT NULL, PRIMARY KEY(french_guy_id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1') or die(mysql_error()); mysql_query(' CREATE TABLE `rogue_guy` ( `rogue_guy_id` int(10) NOT NULL AUTO_INCREMENT, `rogue_name` varchar(20) DEFAULT NULL, PRIMARY KEY(rogue_guy_id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1') or die(mysql_error()); mysql_query(' CREATE TABLE `is_rogue` ( `strong_guy_id` int(10) NOT NULL DEFAULT \'0\', `weak_guy_id` int(10) NOT NULL DEFAULT \'0\', `rogue_guy_id` int(10) NOT NULL DEFAULT \'0\', PRIMARY KEY(strong_guy_id,weak_guy_id,rogue_guy_id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1') or die(mysql_error()); mysql_query('INSERT INTO strong_guy(strong_name) VALUES(\'Strongy Strongo\')'); $strong_guy_id = mysql_insert_id(); mysql_query('INSERT INTO french_guy(french_name) VALUES(\'Franco Phone\')'); $french_guy_id = mysql_insert_id(); mysql_query('INSERT INTO weak_guy(strong_guy_id,weak_name,french_guy_id) VALUES(' . (int) $strong_guy_id . ',\'Weaky Weakling\',' . (int) $french_guy_id . ')'); $weak_guy_id = mysql_insert_id(); mysql_query('INSERT INTO rogue_guy(rogue_name) VALUES(\'John McEnroe\',' . (int) $french_guy_id . ')'); $rogue_guy_id = mysql_insert_id(); mysql_query('INSERT INTO is_rogue(strong_guy_id,weak_guy_id,rogue_guy_id) VALUES(' . $strong_guy_id . ',' . $weak_guy_id . ',' . $rogue_guy_id . ')'); });
*/ Murphy\Fixture::add(function ($row) { if (!($mysql_root = Args::get('mysql_root', Args::argv))) { die('You need to pass in mysql_root'); } mysql_connect('localhost', 'root', $mysql_root) or die(mysql_error()); $dbname = $row['dbname']; $dbuser = $dbname; mysql_query('DROP DATABASE IF EXISTS `' . mysql_real_escape_string($dbname) . '`'); mysql_query('CREATE DATABASE `' . mysql_real_escape_string($dbname) . '`') or die(mysql_error()); mysql_query('GRANT ALL ON `' . mysql_real_escape_string($dbname) . '`.* TO \'' . mysql_real_escape_string($dbuser) . '\'@\'localhost\' IDENTIFIED BY \'' . mysql_real_escape_string($dbuser) . '\'') or die(mysql_error()); mysql_select_db($dbname); //CREATE A TABLE WITH A BUNCH OF DIFFERENT FIELD TYPES mysql_query(' CREATE TABLE `type_test` ( `int_auto_field` int(10) NOT NULL AUTO_INCREMENT, `varchar_field_default_null` varchar(200) DEFAULT NULL, `varchar_field_default_something` varchar(200) NOT NULL DEFAULT \'something\', `int_field_default_10` int(10) NOT NULL DEFAULT \'10\', `int_field_default_null` int(10) DEFAULT NULL, `float_field_default_null` float DEFAULT NULL, `float_field_default_2point5` float NOT NULL DEFAULT 2.5, `double_field_default_null` double DEFAULT NULL, `double_field_default_2point555` double NOT NULL DEFAULT 2.555, `decimal_field_default_null` decimal(10,2) DEFAULT NULL, `decimal_field_default_10point2` decimal(10,2) NOT NULL DEFAULT 10.2, `datetime_field_default_null` datetime DEFAULT NULL, `datetime_field_default_something` datetime NOT NULL DEFAULT \'2012-01-01\', PRIMARY KEY(int_auto_field) ) ENGINE=MyISAM DEFAULT CHARSET=latin1') or die(mysql_error()); });
<?php /** * database_name | dbuser * plusql | plusql * plusql_dev | plusql */ Murphy\Fixture::add(function ($row) { if (!($mysql_root = Args::get('mysql_root', Args::argv))) { die('You need to pass in mysql_root'); } mysql_connect('localhost', 'root', $mysql_root) or die(mysql_error()); $dbname = $row['database_name']; $dbuser = $row['dbuser']; mysql_query('DROP DATABASE IF EXISTS `' . mysql_real_escape_string($dbname) . '`'); mysql_query('CREATE DATABASE `' . mysql_real_escape_string($dbname) . '`') or die(mysql_error()); mysql_query('GRANT ALL ON `' . mysql_real_escape_string($dbname) . '`.* TO \'' . mysql_real_escape_string($dbuser) . '\'@\'localhost\' IDENTIFIED BY \'' . mysql_real_escape_string($dbuser) . '\'') or die(mysql_error()); mysql_select_db($dbname); mysql_query(' CREATE TABLE `fixture_data` ( `field_value` text DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1'); mysql_query('INSERT INTO fixture_data VALUES(\'' . mysql_real_escape_string($dbname) . ' fixture value\')'); });
<?php /** * Test pushing and popping the database connection stack */ Murphy\Test::add(function ($runner) { //run the fixture which will create our databases and populate them //with some sample data Murphy\Fixture::load(dirname(__FILE__) . '/fixture.php')->execute(); //connect using live/dev credentials Plusql::credentials('live', array('localhost', 'plusql', 'plusql', 'plusql')); Plusql::credentials('dev', array('localhost', 'plusql', 'plusql', 'plusql_dev')); //verify that we can connect to both dev and live if (Plusql::connect('live')->query('SELECT * FROM fixture_data')->fixture_data->field_value == 'plusql fixture value') { $runner->pass(); } else { $runner->fail('The fixture value present in the live database is incorrect'); } if (Plusql::connect('dev')->query('SELECT * FROM fixture_data')->fixture_data->field_value == 'plusql_dev fixture value') { $runner->pass(); } else { $runner->fail('The fixture value present in the dev database is incorrect'); } //verify that we can switch back and forth as required if (Plusql::connect('live')->query('SELECT * FROM fixture_data')->fixture_data->field_value == 'plusql fixture value') { $runner->pass(); } else { $runner->fail('I was unable to switch back to the live database after querying dev'); } if (Plusql::connect('dev')->query('SELECT * FROM fixture_data')->fixture_data->field_value == 'plusql_dev fixture value') { $runner->pass();