PageRenderTime 56ms CodeModel.GetById 16ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

/propel_16/vendor/propel/test/testsuite/generator/behavior/sluggable/SluggableBehaviorTest.php

http://github.com/eventhorizonpl/forked-php-orm-benchmark
PHP | 427 lines | 349 code | 58 blank | 20 comment | 2 complexity | cde6098acbf20944c405fe21a3671ee9 MD5 | raw file
  1<?php
  2
  3/**
  4 * This file is part of the Propel package.
  5 * For the full copyright and license information, please view the LICENSE
  6 * file that was distributed with this source code.
  7 *
  8 * @license    MIT License
  9 */
 10
 11require_once dirname(__FILE__) . '/../../../../tools/helpers/bookstore/BookstoreTestBase.php';
 12
 13/**
 14 * Tests for SluggableBehavior class
 15 *
 16 * @author     Fran?ois Zaninotto
 17 * @version    $Revision$
 18 * @package    generator.behavior.sluggable
 19 */
 20class SluggableBehaviorTest extends BookstoreTestBase
 21{
 22    public function testParameters()
 23    {
 24        $table13 = Table13Peer::getTableMap();
 25        $this->assertEquals(count($table13->getColumns()), 3, 'Sluggable adds one columns by default');
 26        $this->assertTrue(method_exists('Table13', 'getSlug'), 'Sluggable adds a slug column by default');
 27        $table14 = Table14Peer::getTableMap();
 28        $this->assertEquals(count($table14->getColumns()), 3, 'Sluggable does not add a column when it already exists');
 29        $this->assertTrue(method_exists('Table14', 'getUrl'), 'Sluggable allows customization of slug_column name');
 30        $this->assertTrue(method_exists('Table14', 'getSlug'), 'Sluggable adds a standard getter for the slug column');
 31    }
 32
 33    public function testObjectGetter()
 34    {
 35        $this->assertTrue(method_exists('Table13', 'getSlug'), 'Sluggable adds a getter for the slug column');
 36        $t = new Table13();
 37        $t->setSlug('foo');
 38        $this->assertEquals('foo', $t->getSlug(), 'getSlug() returns the object slug');
 39        $this->assertTrue(method_exists('Table14', 'getSlug'), 'Sluggable adds a getter for the slug column, even if the column does not have the default name');
 40        $t = new Table14();
 41        $t->setUrl('foo');
 42        $this->assertEquals('foo', $t->getSlug(), 'getSlug() returns the object slug');
 43    }
 44
 45    public function testObjectSetter()
 46    {
 47        $this->assertTrue(method_exists('Table13', 'setSlug'), 'Sluggable adds a setter for the slug column');
 48        $t = new Table13();
 49        $t->setSlug('foo');
 50        $this->assertEquals('foo', $t->getSlug(), 'setSlug() sets the object slug');
 51        $this->assertTrue(method_exists('Table14', 'setSlug'), 'Sluggable adds a setter for the slug column, even if the column does not have the default name');
 52        $t = new Table14();
 53        $t->setSlug('foo');
 54        $this->assertEquals('foo', $t->getUrl(), 'setSlug() sets the object slug');
 55    }
 56
 57    public function testObjectCreateRawSlug()
 58    {
 59        $t = new TestableTable13();
 60        $this->assertEquals('n-a', $t->createRawSlug(), 'createRawSlug() returns an empty string for an empty object with no pattern');
 61        $t->setTitle('Hello, World');
 62        $this->assertEquals('hello-world', $t->createRawSlug(), 'createRawSlug() returns the cleaned up object string representation by default');
 63
 64        $t = new TestableTable14();
 65        $this->assertEquals('/foo/n-a/bar', $t->createRawSlug(), 'createRawSlug() returns a slug for an empty object with a pattern');
 66        $t->setTitle('Hello, World');
 67        $this->assertEquals('/foo/hello-world/bar', $t->createRawSlug(), 'createRawSlug() returns a slug based on a pattern');
 68    }
 69
 70    public static function cleanupSlugProvider()
 71    {
 72        return array(
 73            array('', 'n-a'),
 74            array('foo', 'foo'),
 75            array('foo bar', 'foo-bar'),
 76            array('foo  bar', 'foo-bar'),
 77            array('FoO', 'foo'),
 78            array('f?o', 'foo'),
 79            array(' foo ', 'foo'),
 80            array('f/o:o', 'f-o-o'),
 81            array('foo1', 'foo1'),
 82        );
 83    }
 84
 85    /**
 86     * @dataProvider cleanupSlugProvider
 87     */
 88    public function testObjectCleanupSlugPart($in, $out)
 89    {
 90        $t = new TestableTable13();
 91        $this->assertEquals($out, $t->cleanupSlugPart($in), 'cleanupSlugPart() cleans up the slug part');
 92    }
 93
 94    public static function limitSlugSizeProvider()
 95    {
 96        return array(
 97            array('123', '123'),
 98            array(str_repeat('*', 80), str_repeat('*', 80)),
 99            array(str_repeat('*', 97), str_repeat('*', 97)),
100            array(str_repeat('*', 98), str_repeat('*', 97)),
101            array(str_repeat('*', 99), str_repeat('*', 97)),
102            array(str_repeat('*', 100), str_repeat('*', 97)),
103            array(str_repeat('*', 150), str_repeat('*', 97)),
104        );
105    }
106
107    /**
108     * @dataProvider limitSlugSizeProvider
109     */
110    public function testObjectLimitSlugSize($in, $out)
111    {
112        $t = new TestableTable14();
113        $this->assertEquals($out, $t->limitSlugSize($in), 'limitSlugsize() limits the slug size');
114    }
115
116    public function testObjectMakeSlugUnique()
117    {
118        Table13Query::create()->deleteAll();
119        $t = new TestableTable13();
120        $this->assertEquals('', $t->makeSlugUnique(''), 'makeSlugUnique() returns the input slug when the input is empty');
121        $this->assertEquals('foo', $t->makeSlugUnique('foo'), 'makeSlugUnique() returns the input slug when the table is empty');
122        $t->setSlug('foo');
123        $t->save();
124        $t = new TestableTable13();
125        $this->assertEquals('bar', $t->makeSlugUnique('bar'), 'makeSlugUnique() returns the input slug when the table does not contain a similar slug');
126        $t->save();
127        $t = new TestableTable13();
128        $this->assertEquals('foo-1', $t->makeSlugUnique('foo'), 'makeSlugUnique() returns an incremented input when it already exists');
129        $t->setSlug('foo-1');
130        $t->save();
131        $t = new TestableTable13();
132        $this->assertEquals('foo-2', $t->makeSlugUnique('foo'), 'makeSlugUnique() returns an incremented input when it already exists');
133    }
134
135    public function testObjectCreateSlug()
136    {
137        Table13Query::create()->deleteAll();
138        $t = new TestableTable13();
139        $this->assertEquals('n-a', $t->createSlug(), 'createSlug() returns n-a for an empty object');
140        $t->setTitle('Hello, World!');
141        $this->assertEquals('hello-world', $t->createSlug(), 'createSlug() returns a cleaned up slug');
142        $t->setSlug('hello-world');
143        $t->save();
144        $t = new TestableTable13();
145        $t->setTitle('Hello; wOrld');
146        $this->assertEquals('hello-world-1', $t->createSlug(), 'createSlug() returns a unique slug');
147
148        Table14Query::create()->deleteAll();
149        $t = new TestableTable14();
150        $this->assertEquals('/foo/n-a/bar', $t->createSlug(), 'createSlug() returns a slug for an empty object with a pattern');
151        $t->setTitle('Hello, World!');
152        $this->assertEquals('/foo/hello-world/bar', $t->createSlug(), 'createSlug() returns a cleaned up slug');
153        $t->setSlug('/foo/hello-world/bar');
154        $t->save();
155        $t = new TestableTable14();
156        $t->setTitle('Hello; wOrld:');
157        $this->assertEquals('/foo/hello-world/bar/1', $t->createSlug(), 'createSlug() returns a unique slug');
158    }
159
160    public function testObjectPreSave()
161    {
162        Table14Query::create()->deleteAll();
163        $t = new Table14();
164        $t->save();
165        $this->assertEquals('/foo/n-a/bar', $t->getSlug(), 'preSave() sets a default slug for empty objects');
166        $t = new Table14();
167        $t->setTitle('Hello, World');
168        $t->save();
169        $this->assertEquals('/foo/hello-world/bar', $t->getSlug(), 'preSave() sets a cleanued up slug for objects');
170        $t = new Table14();
171        $t->setTitle('Hello, World');
172        $t->save();
173        $this->assertEquals('/foo/hello-world/bar/1', $t->getSlug(), 'preSave() sets a unique slug for objects');
174        $t = new Table14();
175        $t->setTitle('Hello, World');
176        $t->setSlug('/foo/custom/bar');
177        $t->save();
178        $this->assertEquals('/foo/custom/bar', $t->getSlug(), 'preSave() uses the given slug if it exists');
179        $t = new Table14();
180        $t->setTitle('Hello, World');
181        $t->setSlug('/foo/custom/bar');
182        $t->save();
183        $this->assertEquals('/foo/custom/bar/1', $t->getSlug(), 'preSave() uses the given slug if it exists and makes it unique');
184    }
185
186    public function testObjectSlugLifecycle()
187    {
188        Table13Query::create()->deleteAll();
189        $t = new Table13();
190        $t->setTitle('Hello, World');
191        $t->save();
192        $this->assertEquals('hello-world', $t->getSlug(), 'preSave() creates a slug for new objects');
193        $t->setSlug('hello-bar');
194        $t->save();
195        $this->assertEquals('hello-bar', $t->getSlug(), 'setSlug() allows to override default slug');
196        $t->setSlug('');
197        $t->save();
198        $this->assertEquals('hello-world', $t->getSlug(), 'setSlug(null) relaunches the slug generation');
199
200        Table14Query::create()->deleteAll();
201        $t = new Table14();
202        $t->setTitle('Hello, World2');
203        $t->setSlug('hello-bar2');
204        $t->save();
205        $this->assertEquals('hello-bar2', $t->getSlug(), 'setSlug() allows to override default slug, even before save');
206        $t->setSlug('');
207        $t->save();
208        $this->assertEquals('/foo/hello-world2/bar', $t->getSlug(), 'setSlug(null) relaunches the slug generation');
209    }
210
211    public function testObjectSlugAutoUpdate()
212    {
213        Table13Query::create()->deleteAll();
214        $t = new Table13();
215        $t->setTitle('Hello, World');
216        $t->save();
217        $this->assertEquals('hello-world', $t->getSlug(), 'preSave() creates a slug for new objects');
218        $t->setTitle('Hello, My World');
219        $t->save();
220        $this->assertEquals('hello-my-world', $t->getSlug(), 'preSave() autoupdates slug on object change');
221        $t->setTitle('Hello, My Whole New World');
222        $t->setSlug('hello-bar');
223        $t->save();
224        $this->assertEquals('hello-bar', $t->getSlug(), 'preSave() does not autoupdate slug when it was set by the user');
225    }
226
227    public function testObjectSlugAutoUpdatePermanent()
228    {
229        Table14Query::create()->deleteAll();
230        $t = new Table14();
231        $t->setTitle('Hello, World');
232        $t->save();
233        $this->assertEquals('/foo/hello-world/bar', $t->getSlug(), 'preSave() creates a slug for new objects');
234        $t->setTitle('Hello, My World');
235        $t->save();
236        $this->assertEquals('/foo/hello-world/bar', $t->getSlug(), 'preSave() does not autoupdate slug on object change for permanent slugs');
237        $t->setSlug('hello-bar');
238        $t->save();
239        $this->assertEquals('hello-bar', $t->getSlug(), 'setSlug() still works for permanent slugs');
240    }
241
242    public function testQueryFindOneBySlug()
243    {
244        $this->assertTrue(method_exists('Table13Query', 'findOneBySlug'), 'The generated query provides a findOneBySlug() method');
245        $this->assertTrue(method_exists('Table14Query', 'findOneBySlug'), 'The generated query provides a findOneBySlug() method even if the slug column doesnt have the default name');
246
247        Table14Query::create()->deleteAll();
248        $t1 = new Table14();
249        $t1->setTitle('Hello, World');
250        $t1->save();
251        $t2 = new Table14();
252        $t2->setTitle('Hello, Cruel World');
253        $t2->save();
254        $t = Table14Query::create()->findOneBySlug('/foo/hello-world/bar');
255        $this->assertEquals($t1, $t, 'findOneBySlug() returns a single object matching the slug');
256    }
257
258    public function testUniqueViolationWithoutScope()
259    {
260        TableWithScopeQuery::create()->deleteAll();
261        $t = new TableWithScope();
262        $t->setTitle('Hello, World');
263        $t->save();
264        $this->assertEquals('hello-world', $t->getSlug());
265
266        try {
267            $t = new TableWithScope();
268            $t->setTitle('Hello, World');
269            $t->save();
270
271            $this->fail('Exception expected');
272        } catch (Exception $e) {
273           $this->assertTrue(true, 'Exception successfully thrown');
274       }
275    }
276
277    public function testNoUniqueViolationWithScope()
278    {
279        TableWithScopeQuery::create()->deleteAll();
280        $t = new TableWithScope();
281        $t->setTitle('Hello, World');
282        $t->save();
283        $this->assertEquals('hello-world', $t->getSlug());
284
285        try {
286            $t = new TableWithScope();
287            $t->setTitle('Hello, World');
288            $t->setScope(1);
289            $t->save();
290
291            $this->assertEquals('hello-world', $t->getSlug());
292        } catch (Exception $e) {
293            $this->fail($e->getMessage());
294        }
295    }
296
297    public function testPatternNonPermanent()
298    {
299        Table14PatternQuery::create()->deleteAll();
300
301        $t = new Table14Pattern();
302        $t->setTitle('Hello, World');
303        $t->setTags('test,tag,php');
304        $t->save();
305        $this->assertEquals('/foo/hello-world/bar/test-tag-php', $t->getUrl());
306
307        $t->setTags('php,propel');
308        $t->save();
309        $this->assertEquals('/foo/hello-world/bar/php-propel', $t->getUrl());
310
311        $t->setTitle('Title2');
312        $t->save();
313        $this->assertEquals('/foo/title2/bar/php-propel', $t->getUrl());
314    }
315
316    public function testNumberOfQueriesForMakeUniqSlug()
317    {
318        Table13Query::create()->deleteAll();
319        $con = Propel::getConnection(Table13Peer::DATABASE_NAME);
320
321        for ($i=0; $i < 5; $i++) {
322            $nbQuery = $con->getQueryCount();
323
324            $t = new Table13();
325            $t->setTitle('Hello, World');
326            $t->save($con);
327
328            $this->assertLessThanOrEqual(4, $con->getQueryCount() - $nbQuery, 'no more than 4 query to get a slug when it already exist');
329        }
330    }
331
332    public function testSlugRegexp()
333    {
334        Table13Query::create()->deleteAll();
335        $con = Propel::getConnection(Table13Peer::DATABASE_NAME);
336
337        for ($i=0; $i < 3; $i++) {
338            $t = new Table13();
339            $t->setTitle('Hello, World');
340            $t->save($con);
341        }
342        $this->assertEquals('hello-world-2', $t->getSlug());
343
344        $t = new Table13();
345        $t->setTitle('World');
346        $t->save($con);
347
348        $this->assertEquals('world', $t->getSlug());
349
350        $t = new Table13();
351        $t->setTitle('World');
352        $t->save($con);
353
354        $this->assertEquals('world-1', $t->getSlug());
355
356        $t = new Table13();
357        $t->setTitle('Hello, World');
358        $t->save($con);
359
360        $this->assertEquals('hello-world-3', $t->getSlug());
361
362        $t = new Table13();
363        $t->setTitle('World');
364        $t->save($con);
365
366        $this->assertEquals('world-2', $t->getSlug());
367
368        $t = new Table13();
369        $t->setTitle('World 000');
370        $t->save($con);
371
372        $this->assertEquals('world-000', $t->getSlug());
373
374        $t = new Table13();
375        $t->setTitle('World');
376        $t->save($con);
377
378        $this->assertEquals('world-101', $t->getSlug());
379
380        $t = new Table13();
381        $t->setTitle('World');
382        $t->save($con);
383
384        $this->assertEquals('world-102', $t->getSlug());
385    }
386}
387
388class TestableTable13 extends Table13
389{
390    public function createSlug()
391    {
392        return parent::createSlug();
393    }
394
395    public function createRawSlug()
396    {
397        return parent::createRawSlug();
398    }
399
400    public static function cleanupSlugPart($slug, $separator = '-')
401    {
402        return parent::cleanupSlugPart($slug, $separator);
403    }
404
405    public function makeSlugUnique($slug, $separator = '-', $increment = 0)
406    {
407        return parent::makeSlugUnique($slug, $separator, $increment);
408    }
409}
410
411class TestableTable14 extends Table14
412{
413    public function createSlug()
414    {
415        return parent::createSlug();
416    }
417
418    public function createRawSlug()
419    {
420        return parent::createRawSlug();
421    }
422
423    public static function limitSlugSize($slug, $incrementReservedSpace = 3)
424    {
425        return parent::limitSlugSize($slug, $incrementReservedSpace);
426    }
427}