root/trunk/generator/test/classes/propel/GeneratedObjectLobTest.php

Revision 1082, 9.3 kB (checked in by tony, 3 months ago)

Refs #683: Added support for PHP 5.3 namespaces

Line 
1 <?php
2
3 use bookstore::Peer as PropelPeer;
4 use bookstore::Model as PropelModel;
5
6 /*
7  *  $Id: GeneratedObjectTest.php 928 2008-01-18 12:21:27Z hans $
8  *
9  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
12  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
13  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
14  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
15  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
16  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
17  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
18  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
19  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20  *
21  * This software consists of voluntary contributions made by many individuals
22  * and is licensed under the LGPL. For more information please see
23  * <http://propel.phpdb.org>.
24  */
25
26 require_once 'bookstore/BookstoreTestBase.php';
27
28 /**
29  * Tests the generated Object classes and LOB behavior.
30  *
31  * This test uses generated Bookstore classes to test the behavior of various
32  * object operations.  The _idea_ here is to test every possible generated method
33  * from Object.tpl; if necessary, bookstore will be expanded to accommodate this.
34  *
35  * The database is relaoded before every test and flushed after every test.  This
36  * means that you can always rely on the contents of the databases being the same
37  * for each test method in this class.  See the BookstoreDataPopulator::populate()
38  * method for the exact contents of the database.
39  *
40  * @see        BookstoreDataPopulator
41  * @author     Hans Lellelid <hans@xmpl.org>
42  */
43 class GeneratedObjectLobTest extends BookstoreTestBase {
44
45     /**
46      * Array of filenames pointing to blob/clob files indexed by the basename.
47      *
48      * @var        array string[]
49      */
50     protected $sampleLobFiles = array();
51
52     protected function setUp()
53     {
54         parent::setUp();
55         $this->sampleLobFiles['tin_drum.gif'] = TESTS_BASE_DIR . '/etc/lob/tin_drum.gif';
56         $this->sampleLobFiles['tin_drum.txt'] = TESTS_BASE_DIR . '/etc/lob/tin_drum.txt';
57         $this->sampleLobFiles['propel.gif'] = TESTS_BASE_DIR . '/etc/lob/propel.gif';
58     }
59
60     /**
61      * Gets a LOB filename.
62      *
63      * @param      string $basename Basename of LOB filename to return (if left blank, will choose random file).
64      * @return     string
65      * @throws     Exception - if specified basename doesn't correspond to a registered LOB filename
66      */
67     protected function getLobFile($basename = null)
68     {
69         if ($basename === null) {
70             $basename = array_rand($this->sampleLobFiles);
71         }
72
73         if (isset($this->sampleLobFiles[$basename])) {
74             return $this->sampleLobFiles[$basename];
75         } else {
76             throw new Exception("Invalid base LOB filename: $basename");
77         }
78     }
79
80     /**
81      * Test the LOB results returned in a resultset.
82      */
83     public function testLobResults()
84     {
85
86         $blob_path = $this->getLobFile('tin_drum.gif');
87         $clob_path = $this->getLobFile('tin_drum.txt');
88
89         $book = PropelPeer::BookPeer::doSelectOne(new Criteria());
90
91         $m1 = new PropelModel::Media();
92         $m1->setBook($book);
93         $m1->setCoverImage(file_get_contents($blob_path));
94         $m1->setExcerpt(file_get_contents($clob_path));
95         $m1->save();
96         $m1_id = $m1->getId();
97
98         $m1->reload();
99
100         $img = $m1->getCoverImage();
101         $txt = $m1->getExcerpt();
102
103         $this->assertType('resource', $img, "Expected results of BLOB method to be a resource.");
104         $this->assertType('string', $txt, "Expected results of CLOB method to be a string.");
105
106         $stat = fstat($img);
107         $size = $stat['size'];
108
109         $this->assertEquals(filesize($blob_path), $size, "Expected filesize to match stat(blobrsc)");
110         $this->assertEquals(filesize($clob_path), strlen($txt), "Expected filesize to match clob strlen");
111     }
112
113     /**
114      * Test to make sure that file pointer is not when it is fetched
115      * from the object.
116      *
117      * This is actually a test for correct behavior and does not completely fix
118      * the associated ticket (which was resolved wontfix).
119      *
120      * This does test the rewind-after-save functionality, however.
121      *
122      * @link       http://propel.phpdb.org/trac/ticket/531
123      */
124     public function testLobRepeatRead()
125     {
126         $blob_path = $this->getLobFile('tin_drum.gif');
127         $clob_path = $this->getLobFile('tin_drum.txt');
128
129         $book = PropelPeer::BookPeer::doSelectOne(new Criteria());
130
131         $m1 = new PropelModel::Media();
132         $m1->setBook($book);
133         $m1->setCoverImage(file_get_contents($blob_path));
134         $m1->setExcerpt(file_get_contents($clob_path));
135         $m1->save();
136
137         $img = $m1->getCoverImage();
138
139         // 1) Assert that this resource has been rewound.
140
141         $this->assertEquals(0, ftell($img), "Expected position of cursor in file pointer to be 0");
142
143         // 1) Assert that we've got a valid stream to start with
144
145         $this->assertType('resource', $img, "Expected results of BLOB method to be a resource.");
146
147         // read first 100 bytes
148         $firstBytes = fread($img, 100);
149
150         $img2 = $m1->getCoverImage();
151         $this->assertSame($img, $img2, "Assert that the two resources are the same.");
152
153         // read next 100 bytes
154         $nextBytes = fread($img, 100);
155
156         $this->assertNotEquals(bin2hex($firstBytes), bin2hex($nextBytes), "Expected the first 100 and next 100 bytes to not be identical.");
157     }
158
159     /**
160      * Tests the setting of LOB (BLOB and CLOB) values.
161      */
162     public function testLobSetting()
163     {
164         $blob_path = $this->getLobFile('tin_drum.gif');
165         $blob2_path = $this->getLobFile('propel.gif');
166
167         $clob_path = $this->getLobFile('tin_drum.txt');
168         $book = PropelPeer::BookPeer::doSelectOne(new Criteria());
169
170         $m1 = new PropelModel::Media();
171         $m1->setBook($book);
172         $m1->setCoverImage(file_get_contents($blob_path));
173         $m1->setExcerpt(file_get_contents($clob_path));
174         $m1->save();
175         $m1_id = $m1->getId();
176
177         // 1) Assert that we've got a valid stream to start with
178         $img = $m1->getCoverImage();
179         $this->assertType('resource', $img, "Expected results of BLOB method to be a resource.");
180
181         // 2) Test setting a BLOB column with file contents
182         $m1->setCoverImage(file_get_contents($blob2_path));
183         $this->assertType('resource', $m1->getCoverImage(), "Expected to get a resource back after setting BLOB with file contents.");
184
185         // commit those changes & reload
186         $m1->save();
187
188         // 3) Verify that we've got a valid resource after reload
189         $m1->reload();
190         $this->assertType('resource', $m1->getCoverImage(), "Expected to get a resource back after setting reloading object.");
191
192         // 4) Test isModified() behavior
193         $fp = fopen("php://temp", "r+");
194         fwrite($fp, file_get_contents($blob2_path));
195
196         $m1->setCoverImage($fp);
197         $this->assertTrue($m1->isModified(), "Expected Media object to be modified, despite fact that stream is to same data");
198
199         // 5) Test external modification of the stream (and re-setting it into the object)
200         $stream = $m1->getCoverImage();
201         fwrite($stream, file_get_contents($blob_path)); // change the contents of the stream
202
203         $m1->setCoverImage($stream);
204
205         $this->assertTrue($m1->isModified(), "Expected Media object to be modified when stream contents changed.");
206         $this->assertNotEquals(file_get_contents($blob2_path), stream_get_contents($m1->getCoverImage()));
207
208         $m1->save();
209
210         // 6) Assert that when we call the setter with a stream, that the file in db gets updated.
211
212         $m1->reload(); // start with a fresh copy from db
213
214         // Ensure that object is set up correctly
215         $this->assertNotEquals(file_get_contents($blob_path), stream_get_contents($m1->getCoverImage()), "The object is not correctly set up to verify the stream-setting test.");
216
217         $fp = fopen($blob_path, "r");
218         $m1->setCoverImage($fp);
219         $m1->save();
220         $m1->reload(); // refresh from db
221
222         // Assert that we've updated the db
223         $this->assertEquals(file_get_contents($blob_path), stream_get_contents($m1->getCoverImage()), "Expected the updated BLOB value after setting with a stream.");
224
225         // 7) Assert that 'w' mode works
226
227     }
228
229     public function testLobSetting_WriteMode()
230     {
231         $blob_path = $this->getLobFile('tin_drum.gif');
232         $blob2_path = $this->getLobFile('propel.gif');
233
234         $clob_path = $this->getLobFile('tin_drum.txt');
235         $book = PropelPeer::BookPeer::doSelectOne(new Criteria());
236
237         $m1 = new PropelModel::Media();
238         $m1->setBook($book);
239         $m1->setCoverImage(file_get_contents($blob_path));
240         $m1->setExcerpt(file_get_contents($clob_path));
241         $m1->save();
242
243         PropelPeer::MediaPeer::clearInstancePool();
244
245         // make sure we have the latest from the db:
246         $m2 = PropelPeer::MediaPeer::retrieveByPK($m1->getId());
247
248         // now attempt to assign a temporary stream, opened in 'w' mode, to the db
249
250         $stream = fopen("php://memory", 'w');
251         fwrite($stream, file_get_contents($blob2_path));
252         $m2->setCoverImage($stream);
253         $m2->save();
254         fclose($stream);
255
256         $m2->reload();
257         $this->assertEquals(file_get_contents($blob2_path), stream_get_contents($m2->getCoverImage()), "Expected contents to match when setting stream w/ 'w' mode");
258
259         $stream2 = fopen("php://memory", 'w+');
260         fwrite($stream2, file_get_contents($blob_path));
261         rewind($stream2);
262         $this->assertEquals(file_get_contents($blob_path), stream_get_contents($stream2), "Expecting setup to be correct");
263
264         $m2->setCoverImage($stream2);
265         $m2->save();
266         $m2->reload();
267
268         $this->assertEquals(file_get_contents($blob_path), stream_get_contents($m2->getCoverImage()), "Expected contents to match when setting stream w/ 'w+' mode");
269
270     }
271
272 }
273
274 ?>
275
Note: See TracBrowser for help on using the browser.