<?php

namespace Gaia\Stockpile;

use Gaia\DB\Transaction;
use Gaia\Test\Tap;
$user_id = uniqueUserID();
// test transaction in/out of txn reads.
$item_id = uniqueNumber(1, 1000000);
Transaction::reset();
Transaction::claimStart();
stockpile($app, $user_id)->add($item_id, 10);
// now we should see it.
Transaction::commit();
$total = stockpile($app, $user_id)->get($item_id);
Tap::is(quantify($total), 10, 'after it is committed we can still see the value we added');
$user_id2 = uniqueUserID();
$item_id = uniqueNumber(1, 10000000);
Transaction::claimStart();
$stockpile1 = stockpile($app, $user_id1);
$stockpile2 = stockpile($app, $user_id2);
$start = microtime(TRUE);
$total1 = $stockpile1->add($item_id);
$total2 = $stockpile2->add($item_id);
$elapsed = microtime(TRUE) - $start;
Transaction::commit();
Tap::cmp_ok($elapsed, '<', 1, 'two users adding the same item doesnt create deadlock. took ' . number_format($elapsed, 2) . ' seconds');
$user_id = uniqueUserID();
$item_id = uniqueNumber(1, 10000000);
$stockpile = stockpile($app, $user_id);
$q = $stockpile->quantity(2);
$q->set($stockpile->newId(), array('event' => 'summer2010'));
$q->set($stockpile->newId(), array('event' => 'fall2010'));
$total = $stockpile->add($item_id, $q);
$total = $stockpile->subtract($item_id, 3);
Tap::is(quantify($total), 1, 'after adding a tally of 2 and 2 serials for a total of 4, then subtracting 3, my total is now 1');
$user_id = uniqueUserID();
$item_id = uniqueNumber(1, 10000000);
$stockpile = stockpile($app, $user_id);
$stockpile->add($item_id, 3);
$total = $stockpile->convert($item_id, 2);
Tap::is($total->tally(), 1, 'After adding 3 quantity items and then converting 2 into serials, the tally is 1');
Tap::is(count($total->serials()), 2, 'The serial count is 2');
$total = $stockpile->convert($item_id, $total->grab(2));
Tap::is($total->tally(), 2, 'after grabbing 2 and converting them into tally, tally is now at 2');
Tap::is(count($total->serials()), 1, 'serial count is now 1');
//print "\n";
Ejemplo n.º 3
0
$each = array();
while (list($k, $v) = $c->each()) {
    $each[$k] = $v;
}
Tap::is($c->all(), $each, 'each loop returns all the data in the container');
Tap::is(array_keys($input), $c->keys(), 'keys returns all the keys passed to input');
Tap::is(array_keys($input, 'a'), $c->keys('a'), 'search for a key');
Tap::is($c->pop(), $v = array_pop($input), 'popped off an element, same as input');
Tap::is($c->push($v), array_push($input, $v), 'pushed an element back onto the container');
Tap::is($c->all(), $input, 'after pop and push, input matches container');
Tap::is($c->shift(), $v = array_shift($input), 'shifted off an element, same as input');
Tap::is($c->unshift($v), array_unshift($input, $v), 'unshift an element back onto the container');
Tap::is($c->all(), $input, 'after shift and unshift, input matches container');
@asort($input);
@$c->sort();
Tap::is($c->all(), $input, 'after sorting, matches sorted input');
ksort($input);
$c->ksort();
Tap::is($c->all(), $input, 'after key sorting, matches sorted input');
krsort($input);
Tap::is($c->all(), $input, 'after reverse key sorting, matches sorted input');
$c->flush();
Tap::is($c->all(), array(), 'flush removes everything from the container');
$c->load($input);
Tap::is($c->all(), $input, 'load puts it all back in again');
$c->push(0);
$c->push(NULL);
array_push($input, 0);
array_push($input, NULL);
Tap::is($c->keys(NULL, TRUE), array_keys($input, NULL, TRUE), 'strict match works');
Tap::ok($conn1->start(), 'started a transaction on conn1');
Tap::ok($conn2->start(), 'started a transaction on conn2');
$rs = $conn1->execute("insert into {$table} values (3)");
Tap::ok($rs, 'inserted a row into test table from conn1');
//if( ! $rs ) Tap::debug( $conn1 );
$rs = $conn2->execute("insert into {$table} values(4)");
Tap::ok($rs, 'inserted a row into test table from conn2');
//if( ! $rs ) Tap::debug( $conn2 );
Tap::ok($conn1->commit(), 'committed inserted row on conn1');
Tap::ok($conn2->commit(), 'committed inserted row on conn2');
Tap::ok(Transaction::rollback(), 'rolled back the transaction at the global level');
Tap::ok($rs = $dbmain->execute("select id from {$table}"), 'selected all rows from the table');
$ct = $rs->affected();
Tap::is($ct, 2, '2 rows in the table, new rows rolled back');
$rs = $conn1->execute("select id from {$table}");
Tap::is($rs, FALSE, 'after rolling back, new queries fail on rolled back db object');
$dbmain->execute("drop table {$table}");
$db = $newconn();
$raw = file_get_contents(__DIR__ . '/../sample/i_can_eat_glass.txt');
$lines = explode("\n", $raw);
$lines = array_slice($lines, 0, 10) + array_slice($lines, 100, 10) + array_slice($lines, 200, 10) + array_slice($lines, 200, 10);
$raw = implode("\n", $lines);
$sql = "CREATE TEMPORARY TABLE t1utf8 (`i` INT UNSIGNED NOT NULL PRIMARY KEY, `line` VARCHAR(5000) ) ENGINE=InnoDB DEFAULT CHARACTER SET utf8";
$db->execute($sql);
foreach ($lines as $i => $line) {
    //$lines[ $i ] = $line = mb_convert_encoding($line, 'UTF-8', 'auto');
    $db->execute('INSERT INTO t1utf8 (`i`, `line`) VALUES (%i, %s)', $i, $line);
}
$rs = $db->execute('SELECT * FROM t1utf8');
$readlines = array();
while ($row = $rs->fetch()) {
Ejemplo n.º 5
0
<?php

include_once __DIR__ . '/../common.php';
use Gaia\Test\Tap;
use Gaia\Store\KVP;
Tap::plan(6);
$c = new KVP();
foreach (array('result_set', 'result_get', 'result_isset', 'result_unset') as $key) {
    ${$key} = array();
}
if (!isset($input) || !is_array($input)) {
    $input = array();
}
foreach ($input as $k => $v) {
    $result_set[$k] = $c->{$k} = $v;
    $result_isset[$k] = isset($c->{$k});
    $result_get[$k] = $c->{$k};
    unset($c->{$k});
    $result_unset[$k] = $c->{$k};
}
Tap::is($input, $result_set, 'set works properly');
Tap::is($input, $result_get, 'get works properly');
Tap::is(array_fill_keys(array_keys($input), TRUE), $result_isset, 'isset works properly');
Tap::is(array_fill_keys(array_keys($input), NULL), $result_unset, 'unset works properly');
Tap::is($c->non_existent, NULL, 'non-existent variables are null');
$c->load($input);
Tap::is($c->get(array_keys($input)), $input, 'multi-get works properly');
<?php

namespace Gaia\Stockpile;

use Gaia\DB\Transaction;
use Gaia\Test\Tap;
$user_id = uniqueUserID();
$item_id = uniqueNumber(1, 1000000);
stockpile($app, $user_id)->add($item_id, 5);
// cant subtract zero
$e = NULL;
try {
    stockpile($app, $user_id)->subtract($item_id, 0);
} catch (\Exception $e) {
}
Tap::ok($e instanceof \Exception && preg_match('/cannot subtract/', $e->getMessage()), 'cant subtract zero');
// subtract more than we have.
$starting_total = stockpile($app, $user_id)->get($item_id);
$e = NULL;
try {
    $stockpile = stockpile($app, $user_id);
    $stockpile->subtract($item_id, quantify($stockpile->get($item_id)) + 1);
} catch (\Exception $e) {
}
Tap::ok($e instanceof \Exception && preg_match('/not enough/', $e->getMessage()), 'wont allow to subtract more than you have');
// after failed subtraction, amount is still correct.
$total = stockpile($app, $user_id)->get($item_id);
Tap::is($total, $starting_total, 'after failed subtract, amount is still the same');
<?php

namespace Gaia\Stockpile;

use Gaia\DB\Transaction;
use Gaia\Test\Tap;
$user_id = uniqueUserID();
$item_id = uniqueNumber(1, 1000000);
// test transaction support.
Transaction::claimStart();
$total = stockpile($app, $user_id)->add($item_id);
Tap::is(quantify($total), 4, 'add inside a transaction');
// revert the transaction
Transaction::rollback();
$total = stockpile($app, $user_id)->get($item_id);
Tap::is(quantify($total), 3, 'after txn rollback, the value we added isnt there');
// add inside a transaction and commit it.
Transaction::claimStart();
$total = stockpile($app, $user_id)->add($item_id);
Tranaction::commit();
$total = stockpile($app, $user_id)->get($item_id);
Tap::is(quantify($total), 4, 'add inside of a transaction and commit it. now we can see it!');
<?php

namespace Gaia\Stockpile;

use Gaia\DB\Transaction;
use Gaia\Test\Tap;
$user_id = uniqueUserID();
// test with a big item id number.
$big_item_id = bcsub(bcpow(2, 32), 1);
stockpile($app, $user_id)->add($big_item_id);
$total = stockpile($app, $user_id)->get($big_item_id);
Tap::is(quantify($total), 1, 'big item id works just fine');
Tap::ok(!$cache->replace($k, 1, 10), 'replace fails after key deletion');
Tap::ok($cache->add($k, 1, 10), 'add works after key deletion');
Tap::ok($cache->replace($k, 1, 10), 'replace works after key is added');
$k = 'gaia/cache/test/' . microtime(TRUE) . '/' . mt_rand(1, 10000);
Tap::ok($cache->get($k) === NULL, 'cache get on a non-existent key returns NULL (not false)');
$k = 'gaia/cache/test/' . microtime(TRUE) . '/' . mt_rand(1, 10000);
Tap::is($cache->increment($k, 1), FALSE, 'increment a new key returns (bool) FALSE');
Tap::is($cache->decrement($k, 1), FALSE, 'decrement a new key returns (bool) FALSE');
Tap::cmp_ok($cache->set($k, 'test'), '===', TRUE, 'setting a key returns bool TRUE');
Tap::cmp_ok($cache->replace($k, 'test1'), '===', TRUE, 'replacing a key returns bool TRUE');
Tap::cmp_ok($cache->{$k} = '11', '===', '11', 'setting using the magic method property approach returns value');
unset($cache->{$k});
Tap::cmp_ok($cache->add($k, 'fun'), '===', TRUE, 'adding a key returns (bool) TRUE');
Tap::is($cache->set($k, NULL), TRUE, 'setting a key to null returns true');
Tap::cmp_ok($cache->get(array($k)), '===', array(), 'after setting the key to null, key is deleted');
Tap::is($cache->set($k, $v = '0'), TRUE, 'setting a key to zero returns true');
Tap::cmp_ok($cache->get($k), '===', $v, 'after setting the key to 0, get returns zero value');
Tap::cmp_ok($cache->get(array($k)), '===', array($k => $v), 'multi-get returns the key with zero value');
Tap::ok($cache->set($k, 1, $ttl = 3600 * 24 * 30), 'setting with a huge timeout');
Tap::cmp_ok(strval($cache->get($k)), '===', '1', 'get returns correct value');
$incr = 1000000;
Tap::ok($cache->increment($k, $incr), 'incrementing with a large number');
Tap::cmp_ok(strval($cache->get($k)), '===', strval($incr + 1), 'get returns correct value');
Tap::ok($cache->decrement($k, $incr), 'decrementing with a large number');
Tap::cmp_ok(intval($cache->get($k)), '===', 1, 'get returns correct value');
$huge_number = 9.223372036854776E+18;
if (!is_int($huge_number)) {
    $huge_number = 2147483646;
}
Tap::Debug("testing with {$huge_number}");
Tap::ok($cache->set($k, $v = $huge_number), 'setting a huge number');
Ejemplo n.º 10
0
Tap::is(quantify($total), 1, 'read the item we added');
// add multiple items
$total = stockpile($app, $user_id)->add($item_id, 2);
Tap::is(quantify($total), 3, 'add multiple items to the account');
// make sure we can find those in the db.
$total = stockpile($app, $user_id)->get($item_id);
Tap::is(quantify($total), 3, 'read the items we added');
// test transaction support.
Transaction::claimStart();
$total = stockpile($app, $user_id)->add($item_id);
Tap::is(quantify($total), 4, 'add inside a transaction');
// revert the transaction
Transaction::rollback();
$total = stockpile($app, $user_id)->get($item_id);
Tap::is(quantify($total), 3, 'after txn rollback, the value we added isnt there');
// add inside a transaction and commit it.
Transaction::claimStart();
$total = stockpile($app, $user_id)->add($item_id);
Transaction::commit();
$total = stockpile($app, $user_id)->get($item_id);
Tap::is(quantify($total), 4, 'add inside of a transaction and commit it. now we can see it!');
// set the value back to zero
$total = stockpile($app, $user_id)->set($item_id, 0);
Tap::is(quantify($total), 0, 'set the value back to zero');
// confirm
$total = stockpile($app, $user_id)->get($item_id);
Tap::is(quantify($total), 0, 'read confirms the value is zero');
// confirm
$total = stockpile($app, $user_id)->add($item_id, 4);
Tap::is(quantify($total), 4, 'set the value again');
// test app name uppercase
$e = NULL;
try {
    stockpile('TEST', $user_id);
} catch (\Exception $e) {
}
Tap::ok($e instanceof \Exception && preg_match('/invalid app/', $e->getMessage()), 'cant use uppercase app name');
// test app name with a dash
$e = NULL;
try {
    stockpile('t-est', $user_id);
} catch (\Exception $e) {
}
Tap::ok($e instanceof \Exception && preg_match('/invalid app/', $e->getMessage()), 'cant use dash in app name');
// user returns same value as passed in
Tap::is(stockpile($app, $user_id)->user(), $user_id, 'user returns same value as passed in');
// make sure user is a number
$e = NULL;
try {
    stockpile($app, 'abc');
} catch (\Exception $e) {
}
Tap::ok($e instanceof \Exception && preg_match('/invalid user/', $e->getMessage()), 'user id must be a number');
$e = NULL;
try {
    stockpile($app, -1);
} catch (\Exception $e) {
}
Tap::ok($e instanceof \Exception && preg_match('/invalid user/', $e->getMessage()), 'user id must be a positive int');
$e = NULL;
try {
}
if (!isset($buyer_id)) {
    $buyer_id = uniqueUserId();
}
if (!isset($item_id)) {
    $item_id = uniqueNumber(1, 100000);
}
Transaction::reset();
Connection::reset();
Transaction::start();
$souk = souk($app, $seller_id);
$listing = $souk->auction(array('price' => 10, 'bid' => 0, 'item_id' => $item_id));
$read = $souk->get($listing->id);
Tap::is($listing, $read, 'new auction is readable before transaction is committed, while in the transaction');
Transaction::rollback();
Transaction::reset();
Connection::reset();
$read = $souk->get($listing->id);
Tap::is($read, NULL, 'after transaction rollback, no entry found');
Transaction::reset();
Transaction::start();
$souk = souk($app, $seller_id);
$listing = $souk->auction(array('price' => 10, 'bid' => 0, 'item_id' => $item_id));
Time::offset(86400 * 15);
$listing = $souk->close($listing->id);
Transaction::commit();
Tap::is($listing->closed, 1, 'creating and closing a listing works inside a transaction');
unset($seller_id);
unset($buyer_id);
unset($item_id);
//print "\n\n";
$oldname = $name;
$name = 'name' . microtime(TRUE);
Tap::ok($identifier->store($id, $name), 'able to overwrite with the a new name');
Tap::is($identifier->byName($name), $id, 'found the id by new name');
Tap::is($identifier->byName($oldname), NULL, 'oldname doesnt match anything now');
$oldid = $id;
$id = strval(mt_rand(1, 100000000));
Tap::ok($identifier->store($id, $name), 'able to overwrite name with the a new id');
Tap::is($identifier->byId($id), $name, 'found the name by new id');
Tap::is($identifier->byID($oldid), NULL, 'oldid doesnt match anything now');
$e = null;
try {
    $identifier->store(strval(mt_rand(1, 100000000)), $name, $strict = TRUE);
} catch (Exception $e) {
    $e = $e->__toString();
}
Tap::like($e, '#name-taken#i', 'trying to map name to new id with strict clause thrown in fails');
Tap::ok($identifier->delete($id, $name), 'deleted the pairing');
Tap::is($identifier->byName($name), NULL, 'after deleting, no name lookup');
Tap::is($identifier->byId($id), NULL, 'after deleting, no id lookup');
Transaction::start();
$identifier = $create_identifier();
Tap::ok($identifier->store($id, $name), 'store the name/id pairing with txn');
Tap::is($identifier->byName($name), $id, 'found the id by name');
Tap::is($identifier->byId($id), $name, 'found the name by id');
Transaction::rollback();
Transaction::reset();
$identifier = $create_identifier();
Tap::is($identifier->byName($name), NULL, 'after rollback, no id found by name');
Tap::is($identifier->byId($id), NULL, 'after rollback, no name found by id');
Ejemplo n.º 14
0
try {
    new Transfer($trade, $other);
} catch (\Exception $e) {
}
Tap::ok($e instanceof \Exception && preg_match('/transfer/i', $e->getMessage()), 'no nesting of transfers');
$e = NULL;
Transaction::claimStart();
try {
    new Transfer($core, $core);
} catch (\Exception $e) {
}
Tap::ok($e instanceof \Exception && preg_match('/need\\stwo/i', $e->getMessage()), 'enforce two different parties to trade');
$e = NULL;
Transaction::reset();
try {
    new Transfer(stockpile($app, $other_id), stockpile($app, $other_id));
} catch (\Exception $e) {
}
Tap::ok($e instanceof \Exception && preg_match('/transaction/i', $e->getMessage()), 'blow up when no transaction');
$e = NULL;
try {
    new Transfer(stockpile($app, $other_id), stockpile($app, $other_id));
} catch (\Exception $e) {
}
Tap::ok($e instanceof \Exception && preg_match('/transaction/i', $e->getMessage()), 'blow up when neither has a transaction');
Transaction::reset();
Transaction::claimStart();
$trade = new Transfer(stockpile($app, $user_id), stockpile('test2', $user_id));
Tap::is(quantify($trade->subtract($item_id)), 2, 'move items for same user to different app (escrow example with transaction)');
Tap::is(quantify($trade->add($item_id)), 3, 'add it back from other app to main');
Tap::ok(Transaction::commit(), 'transaction commits successfully');
Ejemplo n.º 15
0
<?php

// test transaction in/out of txn reads.
namespace Gaia\Stockpile;

use Gaia\DB\Transaction;
use Gaia\Test\Tap;
$user_id = uniqueUserID();
$item_id = uniqueNumber(1, 1000000);
Transaction::reset();
Transaction::claimStart();
stockpile($app, $user_id)->add($item_id, 10);
// read the value outside of the transaction ... shouldn't be able to see it yet.
$total = stockpile($app, $user_id)->get($item_id);
Tap::is(quantify($total), 10, 'get outside of txn sees the value we added - it is in the cache optimistically');
// now it should go away
Transaction::rollback();
$total = stockpile($app, $user_id)->get($item_id);
Tap::is(quantify($total), 0, 'after it is rolled back the value disappears from the cache');
Transaction::claimStart();
stockpile($app, $user_id)->add($item_id, 10);
$stockpike = stockpile($app, $user_id);
$stockpile->forceRefresh(TRUE);
$total = $stockpile->get($item_id);
Tap::is(quantify($total), 0, 'when force-refreshing the cache, item disappears that we added but didnt commit');
Transaction::rollback();
<?php

namespace Gaia\Stockpile;

use Gaia\DB\Transaction;
use Gaia\Test\Tap;
$user_id = uniqueUserID();
$item_id = uniqueNumber(1, 1000000);
$total = stockpile($app, $user_id)->add($item_id, 4);
// add a large amount
if (!isset($large_number)) {
    $large_number = bcsub(bcpow(2, 62), 1);
}
$previous_total = $total;
$total = stockpile($app, $user_id)->add($item_id, $large_number);
Tap::is(quantify($total), bcadd(quantify($large_number), quantify($previous_total)), 'add a large quantity');
// read it.
$total = stockpile($app, $user_id)->get($item_id);
Tap::is(quantify($total), bcadd(quantify($large_number), quantify($previous_total)), 'read back the large quantity we added');
// subtract it.
$total = stockpile($app, $user_id)->subtract($item_id, $large_number);
Tap::is(quantify($total), quantify($previous_total), 'subtract a large quantity');
// back where we started
$total = stockpile($app, $user_id)->get($item_id);
Tap::is(quantify($total), 4, 'after subtract, amount is correct');
// remove the item completely
Tap::is(quantify(stockpile($app, $user_id)->subtract($item_id, $total)), 0, 'remove everything');
Tap::is(quantify(stockpile($app, $user_id)->get($item_id)), 0, 'get returns 0');
Tap::is(stockpile($app, $user_id)->get(array($item_id)), array(), 'multi-get returns empty array - deleted item not in there.');
Tap::is(quantify(stockpile($app, $user_id)->add($item_id, $total)), quantify($total), 'add it back, as we were');
Ejemplo n.º 17
0
Tap::is(print_r($res, TRUE), print_r($expected, TRUE), 'search sort by high_price returns results sorted by price from high to low');
$res = array();
foreach (souk($app)->fetch(souk($app)->search(array('sort' => 'expires_soon_delay', 'item_id' => $item_id, 'seller' => $seller_id))) as $id => $listing) {
    $res[] = $listing->expires;
}
$expected = $res;
sort($expected, SORT_NUMERIC);
Tap::is($res, $expected, 'search sort by expired_soon_delay sorted by expires');
Tap::cmp_ok(min($res), '>', Time::now() + 500, 'all of the results expire after now by more than 500 secs');
$res = array();
foreach (souk($app)->fetch(souk($app)->search(array('item_id' => $item_id, 'seller' => $seller_id, 'floor' => 4, 'ceiling' => 6))) as $id => $listing) {
    $res[$listing->price] = TRUE;
}
$res = array_keys($res);
sort($res);
Tap::is($res, array(4, 5, 6), 'setting floor and ceiling limits the result set to prices in the correct range');
$search = souk($app)->search(array('item_id' => $item_id, 'seller' => $seller_id, 'closed' => 0));
$id = array_shift($search);
souk($app)->close($id);
$ids = souk($app)->search(array('item_id' => $item_id, 'seller' => $seller_id, 'closed' => 0));
Tap::ok(!in_array($id, $ids, TRUE), 'after closing an item, it no longer appears in the list of unclosed items');
$res = souk($app)->fetch(souk($app)->search(array('item_id' => $item_id, 'seller' => $seller_id, 'closed' => 0, 'only' => 'bid')));
$bidonly = TRUE;
foreach ($res as $listing) {
    if (!$listing->step || $listing->price) {
        $bidonly = FALSE;
    }
}
Tap::ok($bidonly, 'searching with only=>bid param returns results that are all bid only items, no prices');
$res = souk($app)->fetch(souk($app)->search(array('item_id' => $item_id, 'seller' => $seller_id, 'closed' => 0, 'only' => 'buy')));
$buynow = TRUE;
Ejemplo n.º 18
0
<?php

use Gaia\Test\Tap;
use Gaia\affiliate;
Tap::plan(14);
Tap::ok($affiliate instanceof affiliate\Iface, 'object implements the affiliate interface');
$identifiers = array('a' . microtime(TRUE) . '.' . mt_rand(), 'b' . microtime(TRUE) . '.' . mt_rand(), 'c' . microtime(TRUE) . '.' . mt_rand());
$res = $affiliate->join($identifiers);
$affiliate_id = $res[$identifiers[0]];
Tap::is($res, array_fill_keys($identifiers, $affiliate_id), 'ran the join command and got back a key list of identifiers mapping to my affiliate id');
Tap::ok(ctype_digit($affiliate_id), 'affiliate id is a string of digits');
Tap::is($affiliate->related($identifiers), $res, 'affiliate::related() returns same response as join did');
$identifiers[] = $last = 'd' . microtime(TRUE) . '.' . mt_rand();
$res = $affiliate->join($identifiers);
Tap::is($res, array_fill_keys($identifiers, $affiliate_id), 'join a new identifier to the group');
Tap::is($affiliate->related($identifiers), $res, 'affiliate::related() returns same response as join did');
Tap::is($affiliate->identifiers(array($affiliate_id)), array($affiliate_id => $identifiers), 'affiliate::get returns identifiers mapped to the affiliate id');
$unrelated = 'd' . microtime(TRUE) . '.' . mt_rand();
$res = $affiliate->join(array($unrelated));
$new_affiliate_id = $res[$unrelated];
Tap::is($affiliate->identifiers(array($new_affiliate_id)), array($new_affiliate_id => array($unrelated)), 'joining a single identifer, creates an orphaned new affiliateid');
Tap::isnt($affiliate_id, $new_affiliate_id, 'old and new affiliate ids are different');
$res = $affiliate->join(array($last, $unrelated));
Tap::is($res, array_fill_keys(array_merge($identifiers, array($unrelated)), $affiliate_id), 'joined unrelated to the other identifiers');
Tap::is($affiliate->related($identifiers), array_fill_keys(array_merge($identifiers, array($unrelated)), $affiliate_id), 'the new identifer is now related to the rest');
$affiliate->delete($identifiers);
Tap::is($affiliate->related($identifiers), array_fill_keys($identifiers, NULL), 'after deleting the identifiers, no associations');
Tap::is($affiliate->related(array($unrelated)), array_fill_keys(array($unrelated), $affiliate_id), 'the one identifier we didnt delete still remains');
$affiliate->delete(array($unrelated));
Tap::is($affiliate->related(array($unrelated)), array_fill_keys(array($unrelated), NULL), 'deleted the last identifier');
Ejemplo n.º 19
0
$e = NULL;
try {
    $listing = $souk->buy($listing->id);
} catch (Exception $e) {
    $e = $e->getMessage();
}
Tap::is($e, 'bid only', 'when buying bid-only, fails');
DB\Transaction::reset();
DB\Connection::reset();
$listing = $souk->bid($listing->id, 1);
Tap::is($listing->bid, 1, 'bid works fine, tho');
$listing = Souk($app, $seller_id)->auction(array('bid' => 0, 'reserve' => 5, 'item_id' => $item_id));
$listing = Souk($app, $buyer_id)->bid($listing->id, 5);
Tap::is($listing->bid, 5, 'without enable_proxy, bid is set, not stepped');
$listing = Souk($app)->close($listing->id);
Tap::is($listing->buyer, 0, 'when reserve isnt met, bidder doesnt win listing');
Tap::is($listing->closed, 1, 'even tho reserve wasnt met, closing still ends the bidding.');
unset($seller_id);
unset($buyer_id);
unset($item_id);
Time::offset(86400 * 30);
$id = 0;
$ct = 0;
while ($listings = Souk($app)->pending(0, 5, $id)) {
    foreach ($listings as $id) {
        $ct++;
        //Tap::debug('pending: ' . $id );
    }
}
Tap::cmp_ok($ct, '>=', 1, "found at least 1 item in pending: {$ct} found");
//print "\n\n";
Ejemplo n.º 20
0
$q = $stockpile->get($item_id);
Tap::is(lastElement($q->all()), array(), 'empty properties before callback validator is attached');
$stockpile = new QuantityInspector($stockpile, 'Gaia\\Stockpile\\sanitizeQuantity');
$q = $stockpile->get($item_id);
Tap::is(lastElement($q->all()), array('xp' => 1), 'callback function populated xp into read quantity object');
$stockpile = new QuantityInspector(stockpile($app, $user_id), 'Gaia\\Stockpile\\sanitizeQuantity');
$item_id = uniqueNumber(1, 10000000);
$res = $stockpile->add($item_id, 1);
Tap::is(lastElement($res->all()), array('xp' => 1), 'callback function populated xp into the quantity on add');
$user_id = uniqueUserID();
$item_id = uniqueNumber(1, 10000000);
$stockpile = stockpile($app, $user_id);
$total = $stockpile->add($item_id, 3);
Tap::is($stockpile->set($item_id, $total), $total, 'When calling set with the same set of serials, no items created or subtracted');
$new = $total->grab(array(lastElement($total->serials())));
Tap::is($stockpile->set($item_id, $new), $new, 'When removing  a serial, everything matches up');
Tap::is($stockpile->set($item_id, $total), $total, 'When adding back a serial, everything matches up');
$user_id1 = uniqueUserID();
$user_id2 = uniqueUserID();
$item_id = uniqueNumber(1, 10000000);
Transaction::reset();
Transaction::claimStart();
$stockpile1 = stockpile($app, $user_id1);
$stockpile2 = stockpile($app, $user_id2);
$start = microtime(TRUE);
$total1 = $stockpile1->add($item_id);
$total2 = $stockpile2->add($item_id);
Transaction::commit();
$elapsed = microtime(TRUE) - $start;
Tap::cmp_ok($elapsed, '<', 1, 'two users adding the same item doesnt create deadlock. took ' . number_format($elapsed, 2) . ' seconds');
Tap::isnt($total1, $total2, 'the items created for these two users are different');
Ejemplo n.º 21
0
Tap::is(convertResultToScalar($stockpile->all()), array(13 => 2, 15 => 1, 14 => 1), 'when adding to an existing item that one pops to the top');
$stockpile->sort(array(14, 15));
Tap::is(convertResultToScalar($stockpile->all()), array(14 => 1, 15 => 1, 13 => 2), 'custom sorting layers over the top');
$user_id = uniqueUserId();
$stockpile = new FirstAddedSorter(stockpile($app, $user_id));
foreach (range(13, 15) as $item_id) {
    $stockpile->add($item_id);
    advanceCurrentTime(1);
}
Tap::is(convertResultToScalar($stockpile->all()), array_fill_keys(range(15, 13), 1), 'firstaddedsorter sorts keys by time in reverse order when first adding');
$stockpile->add(13);
Tap::is(convertResultToScalar($stockpile->all()), array(15 => 1, 14 => 1, 13 => 2), 'when adding to an existing item that one stays where it is');
$stockpile->sort(array(14, 15));
Tap::is(convertResultToScalar($stockpile->all()), array(14 => 1, 15 => 1, 13 => 2), 'custom sorting layers over the top');
$user_id = uniqueUserId();
$stockpile = new OldestSorter(stockpile($app, $user_id));
foreach (range(15, 13) as $item_id) {
    $stockpile->add($item_id);
    advanceCurrentTime(1);
}
Tap::is(convertResultToScalar($stockpile->all()), array(15 => 1, 14 => 1, 13 => 1), 'oldestsorter sorts keys by time in order of add');
$stockpile->add(13);
Tap::is(convertResultToScalar($stockpile->all()), array(15 => 1, 14 => 1, 13 => 2), 'when adding to an existing item that one stays where it is');
$stockpile->sort(array(14, 15));
Tap::is(convertResultToScalar($stockpile->all()), array(14 => 1, 15 => 1, 13 => 2), 'custom sorting layers over the top');
$stockpile->sort(array(13, 15));
Tap::is(convertResultToScalar($stockpile->all()), array(13 => 2, 15 => 1, 14 => 1), 'adding a different layering of custom filtering over the top');
$stockpile->subtract(15);
$stockpile->add(15);
Tap::is(convertResultToScalar($stockpile->all()), array(13 => 2, 14 => 1, 15 => 1), 'after deleting an item and adding it again, it shows up at the end of the list');
$expected_sum = 0;
foreach (array_slice(array_reverse($batch, TRUE), 5) as $data) {
    $expected_sum = bcadd($expected_sum, $data['foo']);
}
Tap::is($ct, 6, 'filter descending with start_after iterated the correct number of rows');
Tap::is($sum, $expected_sum, 'sum from filter with start_after arrived at the correct amount');
$generated_ids = $post_processed_ids = array();
$generate = function (array $params) use($skein, &$generated_ids, &$post_processed_ids) {
    $return = array();
    $ids = $skein->ids($params);
    foreach ($ids as $id) {
        $generated_ids[] = $id;
        if ($id % 2 == 0) {
            $post_processed_ids[] = $return[] = $id;
        }
    }
    return $return;
};
$processed_ids = array();
$process = function ($id, $data) use(&$processed_ids) {
    $processed_ids[] = $id;
};
$skein->filter(array('process' => $process, 'generate' => $generate));
Tap::is($generated_ids, $ids, 'generate filter gets all the ids');
Tap::is($processed_ids, $post_processed_ids, 'process filter gets only the ids returned by generate');
$shard = mt_rand(1, 100);
$id = $skein->add($data = array('foo' => mt_rand(1, 1000000000)), $shard);
$parts = Skein\Util::parseId($id);
Tap::is($parts[0], $shard, 'created a new entry, using a custom shard');
Tap::is($skein->get($id), $data, 'read back the entry with custom shard');
    $res = $stratum->store($key, $value);
}
$res = $stratum->query();
asort($pairs, TRUE);
Tap::is($res, $pairs, 'stored a bunch of pairs ... query matches what I stored');
//Tap::debug( $pairs );
//Tap::debug( $res );
Tap::is($stratum->query(array('limit' => '0,1')), array_slice($pairs, 0, 1, TRUE), 'queried the first in the list');
Tap::is($stratum->query(array('limit' => 2, 'sort' => 'desc')), array_slice($pairs, -2, NULL, TRUE), 'queried the last two in the list');
$middle = array_slice($pairs, 5, 1, TRUE);
list($key, $value) = each($middle);
Tap::is($stratum->query(array('min' => $value, 'limit' => 2)), array_slice($pairs, 5, 2, TRUE), 'queried from the middle of the list');
Tap::is($stratum->query(array('max' => $value, 'limit' => 2, 'sort' => 'DESC')), array_reverse(array_slice($pairs, 4, 2, TRUE), TRUE), 'queried from the middle of the list in reverse');
Tap::is($stratum->query(array('search' => array_values(array_slice($pairs, 2, 3, TRUE)))), array_slice($pairs, 2, 3, TRUE), 'search by value matches correct result set');
$expected = $pairs;
ksort($expected);
$res = $stratum->batch();
Tap::is(count($res), count($pairs), 'batch call returns correct result count');
if ($use_bin_constraint) {
    $expected = $res;
    Tap::ok(TRUE, 'skipping first sort order because we cant do exact match of mysql binary ordering');
} else {
    Tap::is($res, $expected, 'batch call returns result set sorted by key');
}
Tap::is($stratum->batch(array('limit' => '0,1')), array_slice($expected, 0, 1, TRUE), 'batch the first in the list');
Tap::is($stratum->batch(array('limit' => 2, 'sort' => 'desc')), array_slice(array_reverse($expected, TRUE), 0, 2, TRUE), 'batch the last two in the list');
Tap::is($stratum->batch(array('start_after' => min(array_keys($expected)))), array_slice($expected, 1, count($expected), TRUE), 'batch starting after the first one');
$expected = array_reverse($expected, TRUE);
Tap::is($stratum->batch(array('sort' => 'DESC')), $expected, 'batch call returns result set sorted by key and sort desc works');
Tap::ok($stratum->delete($key), 'successfully deleted a constraint');
//Tap::debug($pairs);
$item_id = uniqueNumber(1, 1000000);
// add several items to the account.
$items = array($item_id, uniqueNumber(1, 1000000), uniqueNumber(1, 1000000));
sort($items, SORT_NUMERIC);
Transaction::claimStart();
$stockpile = stockpile($app, $user_id);
foreach ($items as $id) {
    $stockpile->add($id);
}
Tap::ok(Transaction::commit(), 'add many items in a transaction');
// grab all the data at once
$all = $stockpile->all();
Tap::is(array_keys($all), $items, 'got back all the items we put in');
// test multi-get
$some = $stockpile->get($some_keys = array($items[0], $items[1]));
Tap::is(array_keys($some), $some_keys, 'multi-get only grabs the items we specified');
// test multi-get with an item that isn't in the list.
$stockpile = stockpile($app, $user_id);
do {
    $not_in_list = uniqueNumber(1, 100000);
} while (in_array($not_in_list, $items));
$res = $stockpile->get($not_in_list);
Tap::is(quantify($res), 0, 'get for an item that doesnt exist in user inventory returns zero');
$res = $stockpile->get(array($not_in_list));
Tap::is($res, array(), 'multi-get for an item that doesnt exist in user inventory returns empty array');
// search for partial
$stockpile->add($item_id);
$some_in_list = array($not_in_list, $item_id);
$res = $stockpile->get($some_in_list);
Tap::is(array_keys($res), array($item_id), 'multi-get for partial match returns just the found results');