} catch (Exception $e) { $t->fail('__construct() must accept valid writers'); $t->skip('the previous test must pass to continue'); die; } $t->is($c->getAllCategories(), array(), '->getAllCategories() returns an empty array in the beginning'); $t->diag('testing ->getCategory()'); $category = $c->getCategory('foo'); $t->ok($category instanceof sfLuceneCategory, '->getCategory() returns an instance of sfLuceneCategory'); $t->is($category->getName(), 'foo', '->getCategory() returns a category with the correct name'); $t->is($category->getCount(), 0, '->getCategory() returns a category with a default score of 0'); $t->ok($category->getHolder() === $c, '->getCategory()->getHolder() returns the same instance as the holder'); $t->ok($category === $c->getCategory('foo'), '->getCategory() returns the same category each time, for a given name'); $t->diag('testing ->save()'); $category->add(10); $t->ok($c->isModified(), 'modifying a category flags the holder for modification'); $c->save(); $t->is($writer->read(), '$categories = array();$categories[\'foo\'] = 10;', '->save() writes the changes to the writer'); $c->getCategory('bar')->add(2)->getHolder()->save(); $t->is($writer->read(), '$categories = array();$categories[\'foo\'] = 10;$categories[\'bar\'] = 2;', '->save() writes multiple changes to the writer'); $t->ok($c->isModified() == false, '->save() resets the modification flag'); $writer->write('foobarbaz'); $c->save(); $t->is($writer->read(), 'foobarbaz', '->save() does nothing if it is not modified'); $t->diag('testing ->load()'); $writer->write('$categories = array();$categories[\'baz\'] = 4;'); $t->is($c->load()->getCategory('baz')->getCount(), 4, '->load() reloads the categories list from the writer'); $t->is(count($c->getAllCategories()), 1, '->load() removes any old categories'); $writer->write('$foo = array();'); try { $c->load();
$t->diag('testing __construct'); try { new sfLuceneCategory('foo', 'bar'); $t->fail('__construct() must reject invalid holders'); } catch (Exception $e) { $t->pass('__construct() must reject invalid holders'); } try { $c = new sfLuceneCategory($holder, 'bar', 5); $t->pass('__construct() must accept valid holders'); } catch (Exception $e) { $t->fail('__construct() must accept valid holders'); } $t->diag('testing initialization parameters'); $t->is($c->getCount(), 5, '->getCount() returns default count'); $t->is($c->getName(), 'bar', '->getName() returns the correct name'); $t->ok($c->getHolder() === $holder, '->getHolder() returns the same holder'); $t->diag('testing ->add() and ->subtract()'); $t->is($c->add()->getCount(), 6, '->add() adds one to the count'); $t->is($c->add(5)->getCount(), 11, '->add() can add more than one to the count'); $t->is($c->subtract()->getCount(), 10, '->subtract() subtracts one from the count'); $t->is($c->subtract(3)->getCount(), 7, '->subtract() can subtract more than one from the count'); $t->is($c->setCount(0)->getCount(), 0, '->setCount() can explicitly change the count'); $t->ok($holder->isModified(), 'changing the count flags the holder for modication'); $t->diag('testing saving methods'); $t->ok(!$c->worthSaving(), '->worthSaving() returns false if the count is 0'); $c->setCount(8); $t->ok($c->worthSaving(), '->worthSaving() returns true if the count is greater than 0'); $t->is($c->getPhp(), '$categories[\'bar\'] = 8;', '->getPhp() returns valid PHP to save'); $t->diag('testing magic methods'); $t->is($c->__toString(), 'bar', '__toString() returns the name');