      1: <?php
      2:
      3: /**
      4:  *
      5:  * Test case for the TagObject class
      6:  *
      7:  * @package alpha::tests
      8:  * @since 1.0
      9:  * @author John Collins <dev@alphaframework.org>
     10:  * @version $Id: Tag_Test.php 1561 2012-08-03 21:47:25Z alphadevx $
     11:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     12:  * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
     13:  * All rights reserved.
     14:  *
     15:  * <pre>
     16:  * Redistribution and use in source and binary forms, with or
     17:  * without modification, are permitted provided that the
     18:  * following conditions are met:
     19:  *
     20:  * * Redistributions of source code must retain the above
     21:  *   copyright notice, this list of conditions and the
     22:  *   following disclaimer.
     23:  * * Redistributions in binary form must reproduce the above
     24:  *   copyright notice, this list of conditions and the
     25:  *   following disclaimer in the documentation and/or other
     26:  *   materials provided with the distribution.
     27:  * * Neither the name of the Alpha Framework nor the names
     28:  *   of its contributors may be used to endorse or promote
     29:  *   products derived from this software without specific
     30:  *   prior written permission.
     31:  *
     32:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     33:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     34:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     35:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     36:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     37:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     38:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     39:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     40:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     41:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     42:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     43:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     44:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     45:  * </pre>
     46:  *
     47:  */
     48: class Tag_Test extends PHPUnit_Framework_TestCase {
     49:     /**
     50:      * An ArticleObject for testing
     51:      *
     52:      * @var ArticleObject
     53:      * @since 1.0
     54:      */
     55:     private $article;
     56:
     57:     /**
     58:      * Called before the test functions will be executed
     59:      * this function is defined in PHPUnit_TestCase and overwritten
     60:      * here
     61:      *
     62:      * @since 1.0
     63:      */
     64:     protected function setUp() {
     65:         $tag = new TagObject();
     66:         $tag->rebuildTable();
     67:
     68:         $denum = new DEnum();
     69:         $denum->rebuildTable();
     70:
     71:         $item = new DEnumItem();
     72:         $item->rebuildTable();
     73:
     74:         $article = new ArticleObject();
     75:         $article->rebuildTable();
     76:
     77:         $denum = new DEnum('ArticleObject::section');
     78:         $item->set('DEnumID', $denum->getOID());
     79:         $item->set('value', 'Test');
     80:         $item->save();
     81:
     82:         $this->article = $this->createArticleObject('unitTestArticle');
     83:     }
     84:
     85:     /**
     86:      * Called after the test functions are executed
     87:      * this function is defined in PHPUnit_TestCase and overwritten
     88:      * here
     89:      *
     90:      * @since 1.0
     91:      */
     92:     protected function tearDown() {
     93:         $article = new ArticleObject();
     94:         $article->dropTable();
     95:
     96:         $tag = new TagObject();
     97:         $tag->dropTable();
     98:
     99:         $denum = new DEnum();
    100:         $denum->dropTable();
    101:
    102:         $item = new DEnumItem();
    103:         $item->dropTable();
    104:
    105:         unset($this->article);
    106:     }
    107:
    108:     /**
    109:      * Creates an article object for testing
    110:      *
    111:      * @return ArticleObject
    112:      * @since 1.0
    113:      */
    114:     private function createArticleObject($name) {
    115:         $article = new ArticleObject();
    116:         $article->set('title', $name);
    117:         $article->set('description', 'A test article called unitTestArticle with some stop words and the unitTestArticle title twice');
    118:         $article->set('author', 'blah');
    119:         $article->set('content', 'blah');
    120:
    121:         return $article;
    122:     }
    123:
    124:     /**
    125:      * Testing the TagObject::tokenize method returns a tag called "unittestarticle"
    126:      *
    127:      * @since 1.0
    128:      */
    129:     public function testTokenizeForExpectedTag() {
    130:         $tags = TagObject::tokenize($this->article->get('description'), 'ArticleObject', $this->article->getOID());
    131:
    132:         $found = false;
    133:         foreach($tags as $tag) {
    134:             if($tag->get('content') == 'unittestarticle') {
    135:                 $found = true;
    136:                 break;
    137:             }
    138:         }
    139:         $this->assertTrue($found, 'Testing the TagObject::tokenize method returns a tag called "unittestarticle"');
    140:     }
    141:
    142:     /**
    143:      * Testing the TagObject::tokenize method does not return a tag called "a"
    144:      *
    145:      * @since 1.0
    146:      */
    147:     public function testTokenizeForUnexpectedTag() {
    148:         $tags = TagObject::tokenize($this->article->get('description'), 'ArticleObject', $this->article->getOID());
    149:
    150:         $found = false;
    151:         foreach($tags as $tag) {
    152:             if($tag->get('content') == 'a') {
    153:                 $found = true;
    154:                 break;
    155:             }
    156:         }
    157:         $this->assertFalse($found, 'Testing the TagObject::tokenize method does not return a tag called "a"');
    158:     }
    159:
    160:     /**
    161:      * Test to ensure that the duplicated value "unittestarticle" is only converted to a TagObject once by TagObject::tokenize
    162:      *
    163:      * @since 1.0
    164:      */
    165:     public function testTokenizeNoDuplicates() {
    166:         $tags = TagObject::tokenize($this->article->get('description'), 'ArticleObject', $this->article->getOID());
    167:
    168:         $count = 0;
    169:         foreach($tags as $tag) {
    170:             if($tag->get('content') == 'unittestarticle') {
    171:                 $count++;
    172:             }
    173:         }
    174:
    175:         $this->assertEquals(1, $count, 'Test to ensure that the duplicated value "unittestarticle" is only converted to a TagObject once by TagObject::tokenize');
    176:     }
    177:
    178:     /**
    179:      * Testing that when an ArticleObject is created that tags are autogenerated based on the description
    180:      *
    181:      * @since 1.0
    182:      */
    183:     public function testSaveArticleGeneratesDescriptionTags() {
    184:         $this->article->save();
    185:         $tags = $this->article->getPropObject('tags')->getRelatedObjects();
    186:
    187:         $found = false;
    188:         foreach($tags as $tag) {
    189:             if($tag->get('content') == 'unittestarticle') {
    190:                 $found = true;
    191:                 break;
    192:             }
    193:         }
    194:         $this->assertTrue($found, 'Testing the TagObject::tokenize method returns a tag called "unittestarticle"');
    195:     }
    196:
    197:     /**
    198:      * Testing the loadTags() method for accessing the tags on a given object type directly
    199:      *
    200:      * @since 1.0
    201:      */
    202:     public function testLoadTags() {
    203:         $this->article->save();
    204:         $tagsA = $this->article->getPropObject('tags')->getRelatedObjects();
    205:
    206:         $tag = new TagObject();
    207:         $tagsB = $tag->loadTags('ArticleObject', $this->article->getOID());
    208:
    209:         $this->assertEquals(count($tagsA), count($tagsB), 'testing the loadTags() method for accessing the tags on a given object type directly');
    210:     }
    211: }
    212:
    213: ?>