Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Postgres / Bigint.php
1 <?php
2
3 namespace RedUNIT\Postgres;
4
5 use RedUNIT\Postgres as Postgres;
6 use RedBeanPHP\Facade as R;
7
8 /**
9  * Bigint
10  *
11  * Tests handling of bigint type columns for primary key IDs,
12  * can we use bigint primary keys without issues ?
13  * These tests should be able to detect incorrect intval
14  * casts for instance.
15  *
16  * @file    RedUNIT/Postgres/Bigint.php
17  * @desc    Tests support for BIGINT primary keys.
18  * @author  Gabor de Mooij and the RedBeanPHP Community
19  * @license New BSD/GPLv2
20  *
21  * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
22  * This source file is subject to the New BSD/GPLv2 License that is bundled
23  * with this source code in the file license.txt.
24  */
25 class Bigint extends Postgres
26 {
27         /**
28          * Test BIG INT primary key support.
29          *
30          * @return void
31          */
32         public function testBigIntSupport()
33         {
34                 R::nuke();
35
36                 $createPageTableSQL = '
37                         CREATE TABLE
38                         page
39                         (
40                                 id BIGSERIAL PRIMARY KEY,
41                                 book_id BIGSERIAL,
42                                 magazine_id BIGSERIAL,
43                                 title VARCHAR(255)
44                         )';
45
46                 $createBookTableSQL = '
47                         CREATE TABLE
48                         book
49                         (
50                                 id BIGSERIAL PRIMARY KEY,
51                                 title VARCHAR(255)
52                         )';
53
54                 $createPagePageTableSQL = '
55                         CREATE TABLE
56                         page_page
57                         (
58                                 id BIGSERIAL PRIMARY KEY,
59                                 page_id BIGSERIAL,
60                                 page2_id BIGSERIAL
61                         ) ';
62
63                 R::exec( $createBookTableSQL );
64                 R::exec( $createPageTableSQL );
65                 R::exec( $createPagePageTableSQL );
66
67                 //insert some records
68
69                 $book1ID     = '2223372036854775808';
70                 $book2ID     = '2223372036854775809';
71                 $page1ID     = '1223372036854775808';
72                 $page2ID     = '1223372036854775809';
73                 $page3ID     = '1223372036854775890';
74                 $pagePage1ID = '3223372036854775808';
75
76                 R::exec("ALTER SEQUENCE book_id_seq RESTART WITH $book1ID");
77                 R::exec("ALTER SEQUENCE page_id_seq RESTART WITH $page1ID");
78                 R::exec("ALTER SEQUENCE page_page_id_seq RESTART WITH $pagePage1ID");
79
80                 $insertBook1SQL = "
81                         INSERT INTO book (title) VALUES( 'book 1' );
82                 ";
83
84                 $insertBook2SQL = "
85                         INSERT INTO book (title) VALUES( 'book 2' );
86                 ";
87
88                 $insertPage1SQL = "
89                         INSERT INTO page (id, book_id, title, magazine_id) VALUES( '$page1ID', '$book1ID', 'page 1 of book 1', '$book2ID' );
90                 ";
91
92                 $insertPage2SQL = "
93                         INSERT INTO page (id, book_id, title) VALUES( '$page2ID', '$book1ID', 'page 2 of book 1' );
94                 ";
95
96                 $insertPage3SQL = "
97                         INSERT INTO page (id, book_id, title) VALUES( '$page3ID', '$book2ID', 'page 1 of book 2' );
98                 ";
99
100                 $insertPagePage1SQL = "
101                         INSERT INTO page_page (id, page_id, page2_id) VALUES( '$pagePage1ID', '$page2ID', '$page3ID' );
102                 ";
103
104                 R::exec( $insertBook1SQL );
105                 R::exec( $insertBook2SQL );
106                 R::exec( $insertPage1SQL );
107                 R::exec( $insertPage2SQL );
108                 R::exec( $insertPage3SQL );
109                 R::exec( $insertPagePage1SQL );
110
111                 //basic tour of basic functions....
112                 $book1 = R::load( 'book', $book1ID );
113
114                 asrt( $book1->id, $book1ID );
115                 asrt( $book1->title, 'book 1' );
116
117                 $book2 = R::load( 'book', $book2ID );
118
119                 asrt( $book2->id, $book2ID );
120                 asrt( $book2->title, 'book 2' );
121
122                 asrt( count( $book1->ownPage ), 2 );
123                 asrt( count( $book1->fresh()->with( 'LIMIT 1' )->ownPage ), 1 );
124                 asrt( count( $book1->fresh()->withCondition( ' title = ? ', array('page 2 of book 1'))->ownPage ), 1 );
125
126                 asrt( count($book2->ownPage), 1 );
127                 asrt( $book2->fresh()->countOwn( 'page' ), 1 );
128
129                 $page1 = R::load( 'page', $page1ID );
130                 asrt( count( $page1->sharedPage ), 0 );
131                 asrt( $page1->fetchAs( 'book' )->magazine->id, $book2ID );
132
133                 $page2 = R::load( 'page', $page2ID );
134                 asrt( count($page2->sharedPage), 1 );
135                 asrt( $page2->fresh()->countShared( 'page' ), 1 );
136
137                 $page3 = R::findOne( 'page', ' title = ? ', array( 'page 1 of book 2' ) );
138                 asrt( $page3->id, $page3ID );
139                 asrt( $page3->book->id, $book2ID );
140         }
141 }