source: branches/1.4/test/tools/helpers/bookstore/BookstoreDataPopulator.php @ 1249

Revision 1249, 6.3 KB checked in by francois, 11 months ago (diff)

[1.4] Improving test speed thanks to transactions

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id Rev Date Author HeadURL Revision
Line 
1<?php
2/*
3 *  $Id$
4 *
5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 *
17 * This software consists of voluntary contributions made by many individuals
18 * and is licensed under the LGPL. For more information please see
19 * <http://propel.phpdb.org>.
20 */
21
22define('_LOB_SAMPLE_FILE_PATH', dirname(__FILE__) . '/../../../etc/lob');
23
24/**
25 * Populates data needed by the bookstore unit tests.
26 *
27 * This classes uses the actual Propel objects to do the population rather than
28 * inserting directly into the database.  This will have a performance hit, but will
29 * benefit from increased flexibility (as does anything using Propel).
30 *
31 * @author     Hans Lellelid <hans@xmpl.org>
32 */
33class BookstoreDataPopulator
34{
35
36        public static function populate($con = null)
37        {
38                if($con === null)
39                {
40                        $con = Propel::getConnection(BookPeer::DATABASE_NAME);
41                }
42                $con->beginTransaction();
43               
44                // Add publisher records
45                // ---------------------
46
47                $scholastic = new Publisher();
48                $scholastic->setName("Scholastic");
49                // do not save, will do later to test cascade
50
51                $morrow = new Publisher();
52                $morrow->setName("William Morrow");
53                $morrow->save($con);
54                $morrow_id = $morrow->getId();
55
56                $penguin = new Publisher();
57                $penguin->setName("Penguin");
58                $penguin->save();
59                $penguin_id = $penguin->getId();
60
61                $vintage = new Publisher();
62                $vintage->setName("Vintage");
63                $vintage->save($con);
64                $vintage_id = $vintage->getId();
65
66                $rowling = new Author();
67                $rowling->setFirstName("J.K.");
68                $rowling->setLastName("Rowling");
69                // no save()
70
71                $stephenson = new Author();
72                $stephenson->setFirstName("Neal");
73                $stephenson->setLastName("Stephenson");
74                $stephenson->save($con);
75                $stephenson_id = $stephenson->getId();
76
77                $byron = new Author();
78                $byron->setFirstName("George");
79                $byron->setLastName("Byron");
80                $byron->save($con);
81                $byron_id = $byron->getId();
82
83                $grass = new Author();
84                $grass->setFirstName("Gunter");
85                $grass->setLastName("Grass");
86                $grass->save($con);
87                $grass_id = $grass->getId();
88
89                $phoenix = new Book();
90                $phoenix->setTitle("Harry Potter and the Order of the Phoenix");
91                $phoenix->setISBN("043935806X");
92                $phoenix->setAuthor($rowling);
93                $phoenix->setPublisher($scholastic);
94                $phoenix->setPrice(10.99);
95                $phoenix->save($con);
96                $phoenix_id = $phoenix->getId();
97
98                $qs = new Book();
99                $qs->setISBN("0380977427");
100                $qs->setTitle("Quicksilver");
101                $qs->setPrice(11.99);
102                $qs->setAuthor($stephenson);
103                $qs->setPublisher($morrow);
104                $qs->save($con);
105                $qs_id = $qs->getId();
106
107                $dj = new Book();
108                $dj->setISBN("0140422161");
109                $dj->setTitle("Don Juan");
110                $dj->setPrice(12.99);
111                $dj->setAuthor($byron);
112                $dj->setPublisher($penguin);
113                $dj->save($con);
114                $dj_id = $dj->getId();
115
116                $td = new Book();
117                $td->setISBN("067972575X");
118                $td->setTitle("The Tin Drum");
119                $td->setPrice(13.99);
120                $td->setAuthor($grass);
121                $td->setPublisher($vintage);
122                $td->save($con);
123                $td_id = $td->getId();
124
125                $r1 = new Review();
126                $r1->setBook($phoenix);
127                $r1->setReviewedBy("Washington Post");
128                $r1->setRecommended(true);
129                $r1->setReviewDate(time());
130                $r1->save($con);
131                $r1_id = $r1->getId();
132
133                $r2 = new Review();
134                $r2->setBook($phoenix);
135                $r2->setReviewedBy("New York Times");
136                $r2->setRecommended(false);
137                $r2->setReviewDate(time());
138                $r2->save($con);
139                $r2_id = $r2->getId();
140
141                $blob_path = _LOB_SAMPLE_FILE_PATH . '/tin_drum.gif';
142                $clob_path =  _LOB_SAMPLE_FILE_PATH . '/tin_drum.txt';
143
144                $m1 = new Media();
145                $m1->setBook($td);
146                $m1->setCoverImage(file_get_contents($blob_path));
147                $m1->setExcerpt(file_get_contents($clob_path));
148                $m1->save($con);
149
150                // Add book list records
151                // ---------------------
152                // (this is for many-to-many tests)
153
154                $blc1 = new BookClubList();
155                $blc1->setGroupLeader("Crazyleggs");
156                $blc1->setTheme("Happiness");
157
158                $brel1 = new BookListRel();
159                $brel1->setBook($phoenix);
160
161                $brel2 = new BookListRel();
162                $brel2->setBook($dj);
163
164                $blc1->addBookListRel($brel1);
165                $blc1->addBookListRel($brel2);
166
167                $bemp1 = new BookstoreEmployee();
168                $bemp1->setName("John");
169                $bemp1->setJobTitle("Manager");
170
171                $bemp2 = new BookstoreEmployee();
172                $bemp2->setName("Pieter");
173                $bemp2->setJobTitle("Clerk");
174                $bemp2->setSupervisor($bemp1);
175                $bemp2->save($con);
176
177                $role = new AcctAccessRole();
178                $role->setName("Admin");
179
180                $bempacct = new BookstoreEmployeeAccount();
181                $bempacct->setBookstoreEmployee($bemp1);
182                $bempacct->setAcctAccessRole($role);
183                $bempacct->setLogin("john");
184                $bempacct->setPassword("johnp4ss");
185                $bempacct->save($con);
186
187                // Add bookstores
188
189                $store = new Bookstore();
190                $store->setStoreName("Amazon");
191                $store->setPopulationServed(5000000000); // world population
192                $store->setTotalBooks(300);
193                $store->save($con);
194
195                $store = new Bookstore();
196                $store->setStoreName("Local Store");
197                $store->setPopulationServed(20);
198                $store->setTotalBooks(500000);
199                $store->save($con);
200               
201                $con->commit();
202        }
203
204        public static function depopulate($con = null)
205        {
206                if($con === null)
207                {
208                        $con = Propel::getConnection(BookPeer::DATABASE_NAME);
209                }
210                $con->beginTransaction();
211                AuthorPeer::doDeleteAll($con);
212                BookstorePeer::doDeleteAll($con);
213                BookstoreContestPeer::doDeleteAll($con);
214                BookstoreContestEntryPeer::doDeleteAll($con);
215                BookstoreEmployeePeer::doDeleteAll($con);
216                BookstoreEmployeeAccountPeer::doDeleteAll($con);
217                BookstoreSalePeer::doDeleteAll($con);
218                BookClubListPeer::doDeleteAll($con);
219                BookOpinionPeer::doDeleteAll($con);
220                BookReaderPeer::doDeleteAll($con);
221                BookListRelPeer::doDeleteAll($con);
222                BookPeer::doDeleteAll($con);
223                ContestPeer::doDeleteAll($con);
224                CustomerPeer::doDeleteAll($con);
225                MediaPeer::doDeleteAll($con);
226                PublisherPeer::doDeleteAll($con);
227                ReaderFavoritePeer::doDeleteAll($con);
228                ReviewPeer::doDeleteAll($con);
229                $con->commit();
230        }
231
232}
Note: See TracBrowser for help on using the repository browser.