Пример #1
0
 public function testExample1()
 {
     ob_start();
     $this->assertEqual(TLO::sqlCreate('Blog'), "CREATE TABLE blog (id CHAR(40), title VARCHAR(25), content VARCHAR(25), PRIMARY KEY (id));\n");
     $this->assertEqual(TLO::sqlCreate('Link'), "CREATE TABLE link (id CHAR(40), url VARCHAR(25), attached__key__id CHAR(40), PRIMARY KEY (id));\n");
     $this->assertIsA($db = new PDO('sqlite::memory:'), 'PDO');
     $db->exec(TLO::sqlCreateAll());
     TLO::init();
     $this->assertIsA($blog1 = TLO::newObject($db, 'Blog'), 'Blog');
     $blog1->title = "News sites";
     $blog1->content = "I like reading news websites, attached to\n                                   this blog are some of my favourites";
     $blog1->write();
     foreach (array('http://news.bbc.co.uk', 'http://news.yahoo.com') as $url) {
         /*
          * Create the link
          */
         $this->assertIsA($link = TLO::newObject($db, 'Link'), 'Link');
         $link->url = $url;
         $link->write();
         /*
          * Now create a relationship between the blog
          * and the link
          */
         $this->assertIsA($blog1->newRelMany('Attached', $link), 'Attached');
     }
     $this->assertIsA($blog2 = TLO::newObject($db, 'Blog'), 'Blog');
     $blog2->title = "Social media";
     $blog2->content = "I recently discovered lots of 'social media'\n                                   websites where people link lots of\n                                   interesting articles. See attached for some\n                                   examples of this.";
     $blog2->write();
     foreach (array('http://reddit.com', 'http://news.ycombinator.com/') as $url) {
         /*
          * Create the link
          */
         $this->assertIsA($link = TLO::newObject($db, 'Link'), 'Link');
         $link->url = $url;
         $link->write();
         /*
          * Now create a relationship between the blog
          * and the link
          */
         $this->assertIsA($blog2->newRelMany('Attached', $link), 'Attached');
     }
     foreach (TLO::getObjects($db, 'Blog') as $blog) {
         $this->assertIsA($blog, 'Blog');
         printf("Title: %s<br/>\n", $blog->title);
         printf("Content: %s<br/>\n", $blog->content);
         printf("<ol>Attachments:\n");
         foreach ($blog->getRelsMany('Attached') as $attached) {
             $this->assertIsA($attached, 'Attached');
             $this->assertIsA($link = $attached->getRelation(), 'Link');
             printf("<li>%s</li>\n", $link->url);
         }
         printf("</ol>\n");
     }
     printf("<br/>\n\n");
     $output = ob_get_contents();
     ob_end_clean();
     $this->assertEqual($output, "Title: News sites<br/>\nContent: I like reading news websites, attached to\n                                   this blog are some of my favourites<br/>\n<ol>Attachments:\n<li>http://news.bbc.co.uk</li>\n<li>http://news.yahoo.com</li>\n</ol>\nTitle: Social media<br/>\nContent: I recently discovered lots of 'social media'\n                                   websites where people link lots of\n                                   interesting articles. See attached for some\n                                   examples of this.<br/>\n<ol>Attachments:\n<li>http://reddit.com</li>\n<li>http://news.ycombinator.com/</li>\n</ol>\n<br/>\n\n");
 }
Пример #2
0
 public function __construct()
 {
     parent::__construct();
     TLO::init();
     $this->guid1 = TLO::guid();
     $this->guid2 = TLO::guid();
     $this->guid3 = TLO::guid();
     $this->guid4 = TLO::guid();
     SSql::setup('sqlite::memory:');
     $this->db = SSql::instance();
     $this->db->exec('CREATE TABLE test1 (id CHAR(' . strlen($this->guid1) . ') PRIMARY KEY, foo INTEGER, connected_to__key__id CHAR(' . strlen($this->guid1) . '), connected_to__var__somevar VARCHAR(10), connected_to__var__time DATETIME)');
     $this->db->exec('CREATE TABLE test3 (id CHAR(' . strlen($this->guid1) . ') PRIMARY KEY, parent__key__id CHAR(' . strlen($this->guid1) . '), bar INTEGER, baz INTEGER)');
     $this->db->exec("INSERT INTO test1 VALUES ('{$this->guid1}', 1, '{$this->guid2}', 'foo', datetime('now'))");
     $this->db->exec("INSERT INTO test3 VALUES ('{$this->guid2}', '{$this->guid1}', 2, 3)");
     $this->db->exec("INSERT INTO test1 VALUES ('{$this->guid3}', 1, '{$this->guid2}', 'foo', datetime('now'))");
     $this->db->exec("INSERT INTO test1 VALUES ('{$this->guid4}', 1, '{$this->guid2}', 'foo', datetime('now'))");
 }