PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/category/sfLuceneCategoriesTest.php

https://bitbucket.org/anycode/sfluceneplugin
PHP | 111 lines | 71 code | 27 blank | 13 comment | 1 complexity | 28f3d671af47dbaa1c2d092177e52a3a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the sfLucenePlugin package
  4. * (c) 2007 - 2008 Carl Vondrick <carl@carlsoft.net>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * @package sfLucenePlugin
  11. * @subpackage Test
  12. * @author Carl Vondrick
  13. * @version SVN: $Id: sfLuceneCategoriesTest.php 7108 2008-01-20 07:44:42Z Carl.Vondrick $
  14. */
  15. require dirname(__FILE__) . '/../../bootstrap/unit.php';
  16. $t = new limeade_test(20, limeade_output::get());
  17. $limeade = new limeade_sf($t);
  18. $app = $limeade->bootstrap();
  19. $luceneade = new limeade_lucene($limeade);
  20. $luceneade->configure()->clear_sandbox();
  21. $lucene = sfLucene::getInstance('testLucene', 'en');
  22. $writer = new sfLuceneStorageBlackhole('foo');
  23. $t->diag('testing constructor and initialization');
  24. try {
  25. new sfLuceneCategories('foo');
  26. $t->fail('__construct() must reject invalid search instances');
  27. } catch (Exception $e) {
  28. $t->pass('__construct() must reject invalid search instances');
  29. }
  30. try {
  31. new sfLuceneCategories($lucene);
  32. $t->pass('__construct() must accept an instance of sfLucene');
  33. } catch (Exception $e) {
  34. $t->fail('__construct() must accept an instance of sfLucene');
  35. $e->printStackTrace();
  36. }
  37. try {
  38. new sfLuceneCategories($lucene, 'dd');
  39. $t->fail('__construct() must reject invalid writers');
  40. } catch (Exception $e) {
  41. $t->pass('__construct() must reject invalid writers');
  42. }
  43. try {
  44. $c = new sfLuceneCategories($lucene, $writer);
  45. $t->pass('__construct() must accept valid writers');
  46. } catch (Exception $e) {
  47. $t->fail('__construct() must accept valid writers');
  48. $t->skip('the previous test must pass to continue');
  49. die();
  50. }
  51. $t->is($c->getAllCategories(), array(), '->getAllCategories() returns an empty array in the beginning');
  52. $t->diag('testing ->getCategory()');
  53. $category = $c->getCategory('foo');
  54. $t->ok($category instanceof sfLuceneCategory, '->getCategory() returns an instance of sfLuceneCategory');
  55. $t->is($category->getName(), 'foo', '->getCategory() returns a category with the correct name');
  56. $t->is($category->getCount(), 0, '->getCategory() returns a category with a default score of 0');
  57. $t->ok($category->getHolder() === $c, '->getCategory()->getHolder() returns the same instance as the holder');
  58. $t->ok($category === $c->getCategory('foo'), '->getCategory() returns the same category each time, for a given name');
  59. $t->diag('testing ->save()');
  60. $category->add(10);
  61. $t->ok($c->isModified(), 'modifying a category flags the holder for modification');
  62. $c->save();
  63. $t->is($writer->read(), '$categories = array();$categories[\'foo\'] = 10;', '->save() writes the changes to the writer');
  64. $c->getCategory('bar')->add(2)->getHolder()->save();
  65. $t->is($writer->read(), '$categories = array();$categories[\'foo\'] = 10;$categories[\'bar\'] = 2;', '->save() writes multiple changes to the writer');
  66. $t->ok($c->isModified() == false, '->save() resets the modification flag');
  67. $writer->write('foobarbaz');
  68. $c->save();
  69. $t->is($writer->read(), 'foobarbaz', '->save() does nothing if it is not modified');
  70. $t->diag('testing ->load()');
  71. $writer->write('$categories = array();$categories[\'baz\'] = 4;');
  72. $t->is($c->load()->getCategory('baz')->getCount(), 4, '->load() reloads the categories list from the writer');
  73. $t->is(count($c->getAllCategories()), 1, '->load() removes any old categories');
  74. $writer->write('$foo = array();');
  75. try {
  76. $c->load();
  77. $t->fail('->load() throws an exception with malformed data');
  78. } catch (Exception $e) {
  79. $t->pass('->load() throws an exception with malformed data');
  80. }
  81. $t->diag('testing ->clear()');
  82. $c->clear();
  83. $t->ok($c->isModified(), '->clear() flags the holder for modication');
  84. $t->is(count($c->getAllCategories()), 0, '->clear() removes all categories');