function insert_authors() { $author = new \AuthorModel(); $author_data = json_decode($this->f3->read('app/cortex/bench/user.json'), true); foreach ($author_data as $record) { $author->copyfrom($record); $author->save(); $author->reset(); } $this->test->message($this->getTime() . " imported 20 Users"); }
function run($db, $type) { $test = new \Test(); // clear existing data \AuthorModel::setdown(); \TagModel::setdown(); \NewsModel::setdown(); \ProfileModel::setdown(); // setup models \AuthorModel::setup(); \TagModel::setup(); \NewsModel::setup(); \ProfileModel::setup(); // setup Author /////////////////////////////////// $author_id = array(); $author = new \AuthorModel(); $ac = $author::resolveConfiguration(); $author_pk = is_int(strpos($type, 'sql')) ? $ac['primary'] : '_id'; $author->name = 'Johnny English'; $author->save(); $author_id[] = $author->_id; $author->reset(); $author->name = 'Ridley Scott'; $author->save(); $author_id[] = $author->_id; $author->reset(); $author->name = 'James T. Kirk'; $author->save(); $author_id[] = $author->_id; $author->reset(); $allauthors = $author->find()->castAll(); $allauthors = $this->getResult($allauthors); $test->expect(json_encode($allauthors) == '[{"name":"Johnny English"},{"name":"Ridley Scott"},{"name":"James T. Kirk"}]', $type . ': all AuthorModel items created'); // setup Tags /////////////////////////////////// $tag_id = array(); $tag = new \TagModel(); $tc = $tag::resolveConfiguration(); $tag_pk = is_int(strpos($type, 'sql')) ? $tc['primary'] : '_id'; $tag->title = 'Web Design'; $tag->save(); $tag_id[] = $tag->_id; $tag->reset(); $tag->title = 'Responsive'; $tag->save(); $tag_id[] = $tag->_id; $tag->reset(); $tag->title = 'Usability'; $tag->save(); $tag_id[] = $tag->_id; $tag->reset(); $allTags = $this->getResult($tag->find()); $test->expect(json_encode($allTags) == '[{"title":"Web Design"},{"title":"Responsive"},{"title":"Usability"}]', $type . ': all TagModel items created'); // setup News /////////////////////////////////// $news_id = array(); $news = new \NewsModel(); $nc = $news::resolveConfiguration(); $news_pk = is_int(strpos($type, 'sql')) ? $nc['primary'] : '_id'; $news->title = 'Responsive Images'; $news->text = 'Lorem Ipsun'; $news->save(); $news_id[] = $news->_id; $news->reset(); $news->title = 'CSS3 Showcase'; $news->text = 'News Text 2'; $news->save(); $news_id[] = $news->_id; $news->reset(); $news->title = 'Touchable Interfaces'; $news->text = 'Lorem Foo'; $news->save(); $news_id[] = $news->_id; $news->reset(); $allnews = $this->getResult($news->find(null, array('order' => 'title'))); $test->expect(count($allnews) == 3 && $allnews[0]['title'] == 'CSS3 Showcase' && $allnews[1]['title'] == 'Responsive Images' && $allnews[2]['title'] == 'Touchable Interfaces', $type . ': all NewsModel items created'); // belongs-to author relation /////////////////////////////////// $author->load(); $news->load(array($news_pk . ' = ?', $news_id[0])); $news->author = $author; $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[0])); $test->expect($news->author->name == 'Johnny English', $type . ': belongs-to-one: author relation created'); $news->author = NULL; $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[0])); $test->expect(empty($news->author), $type . ': belongs-to-one: author relation released'); $news->author = $author->_id; $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[0])); $test->expect($news->author->name == 'Johnny English', $type . ': belongs-to-one: relation created by raw id'); // belongs-to-many tag relation /////////////////////////////////// $tag1 = new \TagModel(); $tag1->load(array($tag_pk . ' = ?', $tag_id[0])); $tag2 = new \TagModel(); $tag2->load(array($tag_pk . ' = ?', $tag_id[1])); $news->tags = array($tag1, $tag2); $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[0])); $test->expect($news->tags[0]->title == 'Web Design' && $news->tags[1]->title == 'Responsive', $type . ': belongs-to-many: relations created with array of mapper objects'); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[1])); $news->tags = array($tag_id[1], $tag_id[2]); $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[1])); $test->expect(count($news->tags) == 2 && $news->tags[0]->title == 'Responsive' && $news->tags[1]->title == 'Usability', $type . ': belongs-to-many: relations created with array of IDs'); $news->tags = null; $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[1])); $test->expect(empty($news->tags), $type . ': belongs-to-many: relations released'); $tag->reset(); $news->load(array($news_pk . ' = ?', $news_id[1])); $news->tags = $tag->load(array($tag_pk . ' != ?', $tag_id[0])); $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[1])); $test->expect($news->tags[0]->title == 'Responsive' && $news->tags[1]->title == 'Usability', $type . ': belongs-to-many: relations created with hydrated mapper'); $news->reset(); $tag->reset(); $news->load(array($news_pk . ' = ?', $news_id[2])); $news->tags = $tag_id[0] . ';' . $tag_id[2]; $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[2])); $test->expect($news->tags[0]->title == 'Web Design' && $news->tags[1]->title == 'Usability', $type . ': belongs-to-many: relations created with split-able string'); $test->expect(is_object($news->tags) && $news->tags instanceof \DB\CortexCollection, $type . ': belongs-to-many: result is collection'); // has-one relation /////////////////////////////////// $profile = new ProfileModel(); $pc = $profile::resolveConfiguration(); $profile_pk = is_int(strpos($type, 'sql')) ? $pc['primary'] : '_id'; $profile->message = 'Hello World'; $profile->author = $author->load(array($author_pk . ' = ?', $author_id[0])); $profile->save(); $profile_id = $profile->_id; $profile->reset(); $author->reset(); $author->load(array($author_pk . ' = ?', $author_id[0])); $profile->load(array($profile_pk . ' = ?', $profile_id)); $test->expect($author->profile->message == 'Hello World' && $profile->author->name == "Johnny English", $type . ': has-one: relation assigned'); $profile->reset(); $profile->message = 'I\'m feeling lucky'; $profile->image = 'lolcat.jpg'; $author->reset(); $author->load(array($author_pk . ' = ?', $author_id[1])); $author->profile = $profile; $author->save(); $profile->reset(); $author->reset(); $author->load(array($author_pk . ' = ?', $author_id[1])); $test->expect($author->profile->message == 'I\'m feeling lucky', $type . ': has-one: inverse relation'); // has-many relation /////////////////////////////////// $author->load(array($author_pk . ' = ?', $author_id[0])); $result = $this->getResult($author->news); $test->expect($result[0]['title'] == "Responsive Images" && $result[0]['tags'][0]['title'] == 'Web Design' && $result[0]['tags'][1]['title'] == 'Responsive', $type . ': has-many inverse relation'); // many to many relation /////////////////////////////////// $news->load(array($news_pk . ' = ?', $news_id[0])); $news->tags2 = array($tag_id[0], $tag_id[1]); $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[0])); $test->expect($news->tags2[0]['title'] == 'Web Design' && $news->tags2[1]['title'] == 'Responsive', $type . ': many-to-many relation created'); $test->expect(is_object($news->tags2) && $news->tags2 instanceof \DB\CortexCollection, $type . ': many-to-many: result is collection'); $tag3 = $tag->load(array($tag_pk . ' = ?', $tag_id[2])); $news->tags2[] = $tag3; $news->save(); $a = count($news->tags2); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[0])); $test->expect(count($news->tags2) == 3 && $a == 3, $type . ': many-to-many relation added implicitly'); $news->load(array($news_pk . ' = ?', $news_id[0])); $news->tags2 = NULL; $news->save(); $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[0])); $test->expect(is_null($news->tags2), $type . ': many-to-many relation released'); $news->reset(); $news->title = 'Can it run Crysis?'; $news->text = 'XOXO'; $news->tags2 = array($tag_id[0]); $news->save(); $news_id[] = $news->_id; $news->reset(); $news->load(array($news_pk . ' = ?', $news_id[3])); $a = count($news->tags2); $tag1 = $tag->find(array($tag_pk . ' = ?', $tag_id[0])); $b = count($tag1[0]->news); $c = $tag1[0]->news[0]->title == 'Can it run Crysis?'; $news->erase(); $tag1 = $tag->find(array($tag_pk . ' = ?', $tag_id[0])); $d = count($tag1[0]->news); $test->expect($a == 1 && $b == 1 && $c && $d == 0, $type . ': many-to-many relation cleaned by erase cascade'); $news->load(array($news_pk . ' = ?', $news_id[0])); $all = $news->find(); $test->expect($all[1]->tags2 === NULL && $all[2]->author === NULL, $type . ': empty relations are NULL'); $arr = $news->cast(); $test->expect(is_array($arr['tags']), $type . ': collection becomes array in casted model'); if ($type == 'mongo') { $test->expect(is_string($arr['_id']), $type . ': id becomes string in casted model'); } /////////////////////////////////// return $test->results(); }
function run($db, $type) { $test = new \Test(); // setup /////////////////////////////////// $author = new \AuthorModel(); $news = new \NewsModel(); $profile = new \ProfileModel(); $tag = new \TagModel(); $ac = $author::resolveConfiguration(); $author_pk = is_int(strpos($type, 'sql')) ? $ac['primary'] : '_id'; $nc = $news::resolveConfiguration(); $news_pk = is_int(strpos($type, 'sql')) ? $nc['primary'] : '_id'; $tc = $tag::resolveConfiguration(); $tag_pk = is_int(strpos($type, 'sql')) ? $tc['primary'] : '_id'; $authorIDs = $author->find()->getAll('_id'); $all = $news->find(); $newsIDs = $all->getAll('_id'); $profileIDs = $profile->find()->getAll('_id'); $tagIDs = $tag->find()->getAll('_id'); // add another relation $news->load(array('title = ?', 'CSS3 Showcase')); $news->author = $author->load(array($author_pk . ' = ?', $authorIDs[0])); $news->save(); $news->reset(); $author->reset(); // has-filter on belongs-to relation /////////////////////////////////// $result = $author->has('news', array('title like ?', '%Image%'))->afind(); $test->expect(count($result) == 1 && $result[0]['name'] == 'Johnny English', $type . ': has filter on many-to-one field'); $test->expect(count($result[0]['news']) == 2 && $result[0]['news'][0]['title'] == 'Responsive Images' && $result[0]['news'][1]['title'] == 'CSS3 Showcase', $type . ': has filter does not prune relation set'); $result = $news->has('author', array('name = ?', 'Johnny English'))->afind(); $test->expect(count($result) == 2 && $result[0]['title'] == 'Responsive Images' && $result[1]['title'] == 'CSS3 Showcase', $type . ': has filter on one-to-many field'); // add another profile $profile->message = 'Beam me up, Scotty!'; $profile->author = $authorIDs[2]; $profile->save(); $profile->reset(); $result = $author->has('profile', array('message LIKE ?', '%Scotty%'))->afind(); $test->expect(count($result) == 1 && $result[0]['name'] == 'James T. Kirk' && $result[0]['profile']['message'] == 'Beam me up, Scotty!', $type . ': has filter on one-to-one field'); $result = $profile->has('author', array('name LIKE ?', '%Kirk%'))->afind(); $test->expect(count($result) == 1 && $result[0]['message'] == 'Beam me up, Scotty!' && $result[0]['author']['name'] == 'James T. Kirk', $type . ': has filter on one-to-one field, inverse'); // add mm tags $news->load(array('title = ?', 'Responsive Images')); $news->tags2 = array($tagIDs[0], $tagIDs[1]); $news->save(); $news->load(array('title = ?', 'CSS3 Showcase')); $news->tags2 = array($tagIDs[1], $tagIDs[2]); $news->save(); $news->reset(); $result = $news->has('tags2', array('title like ?', '%Design%'))->find(); $test->expect(count($result) == 1 && $result[0]['title'] == 'Responsive Images', $type . ': has filter on many-to-many field'); $result = $news->has('tags2', array('title = ?', 'Responsive'))->find(); $test->expect(count($result) == 2 && $result[0]['title'] == 'Responsive Images' && $result[1]['title'] == 'CSS3 Showcase', $type . ': has filter on many-to-many field, additional test'); $result = $tag->has('news', array('title = ?', 'Responsive Images'))->find(); $test->expect(count($result) == 2 && $result[0]['title'] == 'Web Design' && $result[1]['title'] == 'Responsive', $type . ': has filter on many-to-many field, inverse'); // add another tag $news->load(array('title = ?', 'Touchable Interfaces')); $news->tags2 = array($tagIDs[1]); $news->save(); $news->reset(); $tag->has('news', array('text LIKE ? and title LIKE ?', '%Lorem%', '%Interface%')); $result = $tag->find(); $test->expect(count($result) == 1 && $result[0]['title'] == 'Responsive', $type . ': has filter with multiple conditions'); $news->has('tags2', array('title = ? OR title = ?', 'Usability', 'Web Design')); $result = $news->afind(array('text = ?', 'Lorem Ipsun')); $test->expect(count($result) == 1 && $result[0]['title'] == 'Responsive Images', $type . ': find with condition and has filter'); $news->load(array('title = ?', 'Responsive Images')); $news->author = $authorIDs[1]; $news->save(); $news->reset(); $news->has('tags2', array('title = ? OR title = ?', 'Usability', 'Web Design')); $news->has('author', array('name = ?', 'Ridley Scott')); $result = $news->afind(); $test->expect(count($result) == 1 && $result[0]['title'] == 'Responsive Images', $type . ': find with multiple has filters on different relations'); // add another news to author 2 $news->load(array($news_pk . ' = ?', $newsIDs[2])); $news->author = $authorIDs[1]; $news->save(); $news->reset(); $news->has('author', array('name = ?', 'Ridley Scott')); $news->load(); $res = array(); while (!$news->dry()) { $res[] = $news->title; $news->next(); } $test->expect(count($res) == 2 && $res[0] == 'Responsive Images' && $res[1] == 'Touchable Interfaces', $type . ': has filter in load context'); $news->reset(); $news->fields(array('title')); $news->load(); $test->expect(!empty($news->title) && empty($news->author) && empty($news->text) && empty($news->tags) && empty($news->tags2), $type . ': use a whitelist to restrict fields'); unset($news); $news = new \NewsModel(); $news->fields(array('title', 'tags', 'tags2', 'author'), true); $news->load(); $test->expect(empty($news->title) && empty($news->author) && !empty($news->text) && empty($news->tags) && empty($news->tags2), $type . ': use a blacklist to restrict fields'); unset($news); $news = new \NewsModel(); $news->fields(array('tags.title')); $news->load(); $test->expect(!empty($news->tags[0]->title) && empty($news->tags[0]->news), $type . ': set restricted fields to related mappers'); $news->filter('tags2', null, array('order' => 'title ASC')); $news->load(array('title = ?', 'Responsive Images')); $test->expect($news->tags2[0]->title == 'Responsive' && $news->tags2[1]->title == 'Web Design', $type . ': filter with sorting of related records'); // get all tags sorted by their usage in news articles $tag->reset(); $tag->countRel('news'); $result = $tag->find(null, array('order' => 'count_news DESC, title'))->castAll(0); $test->expect($result[0]['title'] == 'Responsive' && $result[0]['count_news'] == 3 && $result[1]['title'] == 'Usability' && $result[1]['count_news'] == 1 && $result[2]['title'] == 'Web Design' && $result[2]['count_news'] == 1, $type . ': count and sort on many-to-many relation'); // get all authors sorted by the amount of news they have written $author->reset(); $author->countRel('news'); $result = $author->find(null, array('order' => 'count_news DESC'))->castAll(0); $test->expect($result[0]['name'] == 'Ridley Scott' && $result[0]['count_news'] == 2 && $result[1]['name'] == 'Johnny English' && $result[1]['count_news'] == 1 && $result[2]['name'] == 'James T. Kirk' && $result[2]['count_news'] == null, $type . ': count and sort on one-to-many relation'); $tag->reset(); $tag->countRel('news'); $result = $tag->find(null, array('order' => 'count_news DESC, title DESC', 'limit' => 1, 'offset' => 1))->castAll(0); $test->expect($result[0]['title'] == 'Web Design' && $result[0]['count_news'] == 1, $type . ': apply limit and offset on aggregated collection'); $author->reset(); $author->countRel('news'); $author->has('news', array('text like ?', '%Lorem%')); $result = $author->find()->castAll(0); $test->expect(count($result) == 1 && $result[0]['name'] == 'Ridley Scott' && $result[0]['count_news'] == 2, $type . ': has-filter and 1:M relation counter'); $author->reset(); $id = $author->load()->next()->_id; $tag->reset(); $tag->countRel('news'); $tag->has('news', array('author = ?', $id)); $result = $tag->find(null, array('order' => 'count_news desc'))->castAll(0); $test->expect(count($result) == 2 && $result[0]['title'] == 'Responsive' && $result[0]['count_news'] == 3 && $result[1]['title'] == 'Web Design' && $result[1]['count_news'] == 1, $type . ': has-filter and M:M relation counter'); /////////////////////////////////// return $test->results(); }