Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Base / Tags.php
1 <?php
2
3 namespace RedUNIT\Base;
4
5 use RedUNIT\Base as Base;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\RedException as RedException;
8
9 /**
10  * Tags
11  *
12  * Tests RedBeanPHP tagging functionality, should be easy
13  * to tag beans, collect tags and integrate tags in SQL
14  * snippets. Tags automatically result in N-M relations, i.e.
15  * shared lists.
16  *
17  * @file    RedUNIT/Base/Tags.php
18  * @desc    Tests the tagging of beans.
19  * @author  Gabor de Mooij and the RedBeanPHP Community
20  * @license New BSD/GPLv2
21  *
22  * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
23  * This source file is subject to the New BSD/GPLv2 License that is bundled
24  * with this source code in the file license.txt.
25  */
26 class Tags extends Base
27 {
28         /**
29          * Tests tags with SQL.
30          *
31          * @return void
32          */
33         public function testTagsWithSQL()
34         {
35                 R::nuke();
36                 list( $m1, $m2, $m3 ) = R::dispense( 'movie', 3 );
37                 $m1->title = 'Frankenstein';
38                 $m2->title = 'Fall of the House Usher';
39                 $m3->title = 'Sleepy Hollow';
40                 R::tag($m1, 'horror,gothic');
41                 R::tag($m2, 'horror,gothic,short');
42                 R::tag($m3, 'horror,legend');
43                 asrt( count( R::tagged( 'movie', 'horror' ) ), 3);
44                 asrt( count( R::tagged( 'movie', 'horror', ' LIMIT 2' ) ), 2);
45                 asrt( count( R::tagged( 'movie', 'horror', ' LIMIT ?', array( 2 ) ) ), 2);
46                 asrt( count( R::tagged( 'movie', 'horror', ' ORDER BY movie.title DESC LIMIT ?', array( 2 ) ) ), 2);
47                 asrt( count( R::tagged( 'movie', 'horror,gothic', ' ORDER BY movie.title DESC LIMIT ?', array( 1 ) ) ), 1);
48                 asrt( count( R::tagged( 'movie', 'horror,gothic') ), 3 );
49                 asrt( count( R::taggedAll( 'movie', 'horror,gothic') ), 2 );
50                 asrt( count( R::tagged( 'movie', 'horror,gothic', ' LIMIT ? ', array( 2 ) ) ), 2 );
51                 asrt( count( R::taggedAll( 'movie', 'horror,gothic', ' LIMIT ? ', array( 2 ) ) ), 2 );
52                 asrt( count( R::tagged( 'movie', 'horror,gothic', ' LIMIT ? ', array( 1 ) ) ), 1 );
53                 asrt( count( R::taggedAll( 'movie', 'horror,gothic', ' LIMIT ? ', array( 1 ) ) ), 1 );
54                 asrt( count( R::tagged( 'movie', 'horror,legend', ' LIMIT ? ', array( 1 ) ) ), 1 );
55                 asrt( count( R::taggedAll( 'movie', 'horror,legend', ' LIMIT ? ', array( 1 ) ) ), 1 );
56                 asrt( count( R::tagged( 'movie', 'gothic,legend', ' LIMIT ? ', array( 1 ) ) ), 1 );
57                 asrt( count( R::taggedAll( 'movie', 'gothic,legend', ' LIMIT ? ', array( 1 ) ) ), 0 );
58                 asrt( count( R::tagged( 'movie', 'romance', ' LIMIT ? ', array( 1 ) ) ), 0 );
59                 asrt( count( R::taggedAll( 'movie', 'romance', ' LIMIT ? ', array( 1 ) ) ), 0 );
60                 asrt( count( R::tagged( 'movie', 'romance,xmas', ' LIMIT ? ', array( 1 ) ) ), 0 );
61                 asrt( count( R::taggedAll( 'movie', 'romance,xmas', ' LIMIT ? ', array( 1 ) ) ), 0 );
62                 asrt( count( R::tagged( 'movie', 'gothic,short', ' LIMIT ? ', array( 4 ) ) ), 2 );
63                 asrt( count( R::taggedAll( 'movie', 'gothic,short', ' LIMIT ? ', array( 4 ) ) ), 1 );
64                 asrt( count( R::tagged( 'movie', 'gothic,short', ' LIMIT 4 ' ) ), 2 );
65                 asrt( count( R::taggedAll( 'movie', 'gothic,short', ' LIMIT 4 ' ) ), 1 );
66                 asrt( count( R::tagged( 'movie', 'gothic,short', ' ORDER BY movie.id DESC LIMIT 4 ' ) ), 2 );
67                 asrt( count( R::taggedAll( 'movie', 'gothic,short', ' ORDER BY movie.id DESC LIMIT 4 ' ) ), 1 );
68                 asrt( count( R::tagged( 'movie', 'short', ' LIMIT ? ', array( 4 ) ) ), 1 );
69                 asrt( count( R::taggedAll( 'movie', 'short', ' LIMIT ? ', array( 4 ) ) ), 1 );
70                 asrt( count( R::tagged( 'movie', '', ' LIMIT ? ', array( 4 ) ) ), 0 );
71                 asrt( count( R::taggedAll( 'movie', '', ' LIMIT ? ', array( 4 ) ) ), 0 );
72                 asrt( count( R::tagged( 'movie', '', ' LIMIT 4 ' ) ), 0 );
73                 asrt( count( R::taggedAll( 'movie', '', ' LIMIT 4 ' ) ), 0 );
74                 asrt( count( R::tagged( 'movie', '', '' ) ), 0 );
75                 asrt( count( R::taggedAll( 'movie', '', '' ) ), 0 );
76                 asrt( count( R::tagged( 'movie', '' ) ), 0 );
77                 asrt( count( R::taggedAll( 'movie', '' ) ), 0 );
78         }
79
80         /**
81          * Some basic tests.
82          *
83          * @return void
84          */
85         public function testTags()
86         {
87                 list( $c, $d, $e, $f ) = R::dispense( 'coffee', 4 );
88                 R::tag( $c, 'strong,black' );
89                 R::tag( $d, 'black' );
90                 R::tag( $e, 'strong,sweet' );
91                 R::tag( $f, 'black,strong' );
92                 asrt( count( R::taggedAll( 'coffee', 'strong,sweet' ) ), 1 );
93                 asrt( count( R::taggedAll( 'coffee', 'strong' ) ), 3 );
94                 asrt( count( R::taggedAll( 'coffee', '' ) ), 0 );
95                 asrt( count( R::taggedAll( 'coffee', 'sweet' ) ), 1 );
96                 asrt( count( R::taggedAll( 'coffee', 'sweet,strong' ) ), 1 );
97                 asrt( count( R::taggedAll( 'coffee', 'black,strong' ) ), 2 );
98                 asrt( count( R::taggedAll( 'coffee', array( 'black', 'strong' ) ) ), 2 );
99                 asrt( count( R::taggedAll( 'coffee', 'salty' ) ), 0 );
100                 $blog = R::dispense( 'blog' );
101                 $blog->title = 'testing';
102                 $blog->blog  = 'tesing';
103                 R::store( $blog );
104                 $blogpost = ( R::load( "blog", 1 ) );
105                 $post = R::dispense( "post" );
106                 $post->message = "hello";
107                 R::tag( $post, "lousy,smart" );
108                 asrt( implode( ',', R::tag( $post ) ), "lousy,smart" );
109                 R::tag( $post, "clever,smart" );
110                 $tagz = implode( ',', R::tag( $post ) );
111                 asrt( ( $tagz == "smart,clever" || $tagz == "clever,smart" ), TRUE );
112                 R::tag( $blog, array( "smart", "interesting" ) );
113                 asrt( implode( ',', R::tag( $blog ) ), "smart,interesting" );
114                 try {
115                         R::tag( $blog, array( "smart", "interesting", "lousy!" ) );
116                         pass();
117                 } catch ( RedException $e ) {
118                         fail();
119                 }
120                 asrt( implode( ',', R::tag( $blog ) ), "smart,interesting,lousy!" );
121                 R::untag( $blog, array( "smart", "interesting" ) );
122                 asrt( implode( ",", R::tag( $blog ) ), "lousy!" );
123                 asrt( R::hasTag( $blog, array( "lousy!" ) ), TRUE );
124                 asrt( R::hasTag( $blog, array( "lousy!", "smart" ) ), TRUE );
125                 asrt( R::hasTag( $blog, array( "lousy!", "smart" ), TRUE ), FALSE );
126                 R::tag( $blog, FALSE );
127                 asrt( count( R::tag( $blog ) ), 0 );
128                 R::tag( $blog, array( "funny", "comic" ) );
129                 asrt( count( R::tag( $blog ) ), 2 );
130                 R::addTags( $blog, array( "halloween" ) );
131                 asrt( count( R::tag( $blog ) ), 3 );
132                 asrt( R::hasTag( $blog, array( "funny", "commic", "halloween" ), TRUE ), FALSE );
133                 R::unTag( $blog, "funny" );
134                 R::addTags( $blog, "horror" );
135                 asrt( count( R::tag( $blog ) ), 3 );
136                 asrt( R::hasTag( $blog, array( "horror", "commic", "halloween" ), TRUE ), FALSE );
137                 //no double tags
138                 R::addTags( $blog, "horror" );
139                 asrt( R::hasTag( $blog, array( "horror", "commic", "halloween" ), TRUE ), FALSE );
140                 asrt( R::hasTag( $blog, "horror,commic,halloween", TRUE ), FALSE );
141                 asrt( count( R::tag( $blog ) ), 3 );
142                 testpack( "fetch tagged items" );
143         }
144
145         /**
146          * Fetching tagged items.
147          *
148          * @return void
149          */
150         public function fetchTaggedItems()
151         {
152                 $b = R::dispense( "book" );
153                 $b->title = 'horror';
154                 R::store( $b );
155                 $c = R::dispense( "book" );
156                 $c->title = 'creepy';
157                 R::store( $c );
158                 $d = R::dispense( "book" );
159                 $d->title = "chicklit";
160                 R::store( $d );
161                 R::tag( $b, "horror,classic" );
162                 R::tag( $d, "women,classic" );
163                 R::tag( $c, "horror" );
164                 $x = R::tagged( "book", "classic" );
165                 asrt( count( $x ), 2 );
166                 $x = R::tagged( "book", "classic,horror" );
167                 asrt( count( $x ), 3 );
168                 $x = R::tagged( "book", array( "classic", "horror" ) );
169                 asrt( count( $x ), 3 );
170         }
171 }