1
+ <?php
2
+ /*=========================================================================
3
+ MIDAS Server
4
+ Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
5
+ 69328 Lyon, FRANCE.
6
+
7
+ See Copyright.txt for details.
8
+ This software is distributed WITHOUT ANY WARRANTY; without even
9
+ the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10
+ PURPOSE. See the above copyright notices for more information.
11
+ =========================================================================*/
12
+ /** AssetstoreModelTest*/
13
+ class AssetstoreModelTest extends DatabaseTestCase
14
+ {
15
+ /** init test*/
16
+ public function setUp ()
17
+ {
18
+ $ this ->setupDatabase (array ('default ' ));
19
+ $ this ->_models = array ('Assetstore ' );
20
+ $ this ->_daos = array ();
21
+ parent ::setUp ();
22
+ }
23
+
24
+
25
+
26
+ /**
27
+ * helper function, will save an Assetstore, using either the passed in
28
+ * assetstoreDao or creating a new one, will set the three parameter
29
+ * values before saving.
30
+ * @param type $name
31
+ * @param type $path
32
+ * @param type $type
33
+ * @param AssetstoreDao $assetstoreDao
34
+ * @return AssetstoreDao
35
+ */
36
+ protected function validSaveTestcase ($ name , $ path , $ type , $ assetstoreDao = null )
37
+ {
38
+ if (empty ($ assetstoreDao ))
39
+ {
40
+ $ assetstoreDao = new AssetstoreDao ();
41
+ }
42
+ $ assetstoreDao ->setName ($ name );
43
+ $ assetstoreDao ->setPath ($ path );
44
+ $ assetstoreDao ->setType ($ type );
45
+ $ this ->Assetstore ->save ($ assetstoreDao );
46
+ return $ assetstoreDao ;
47
+ }
48
+
49
+ /**
50
+ * helper function, attempts to save an Assetstore, using either the passed in
51
+ * assetstoreDao or creating a new one, will set the three parameter
52
+ * values before saving, expects that the save will fail, and asserts
53
+ * that an exception has been thrown by the Assetstore Model.
54
+ * @param type $name
55
+ * @param type $path
56
+ * @param type $type
57
+ * @param AssetstoreDao $assetstoreDao
58
+ */
59
+ protected function invalidSaveTestcase ($ name , $ path , $ type , $ assetstoreDao = null )
60
+ {
61
+ if (empty ($ assetstoreDao ))
62
+ {
63
+ $ assetstoreDao = new AssetstoreDao ();
64
+ }
65
+ $ assetstoreDao ->setName ($ name );
66
+ $ assetstoreDao ->setPath ($ path );
67
+ $ assetstoreDao ->setType ($ type );
68
+ try
69
+ {
70
+ $ this ->Assetstore ->save ($ assetstoreDao );
71
+ $ this ->fail ('Expected an exception saving assetstoreDao, but did not get one. ' );
72
+ }
73
+ catch (Zend_Exception $ ze )
74
+ {
75
+ // if we got here, this is the correct behavior
76
+ $ this ->assertTrue (true );
77
+ }
78
+ }
79
+
80
+ /** test the save method*/
81
+ public function testSave ()
82
+ {
83
+
84
+ // expect that there will be one assetstore to start out, Default
85
+ // this will probably need to be expanded once we start adding implementations
86
+ // for the other two types of assetstore
87
+ // for now, a local Default assetstore is expected
88
+ $ all = $ this ->Assetstore ->getAll ();
89
+ $ this ->assertEquals (sizeof ($ all ), 1 );
90
+ $ default = $ all [0 ];
91
+ $ this ->assertEquals ('Default ' , $ default ->getName ());
92
+ // Correct Create Test
93
+
94
+
95
+ // create new ones with a different path from existing ones
96
+
97
+ $ assetstoreDao1 = $ this ->validSaveTestcase ('test_assetstore_1 ' , '/testassetstore1/path ' , '0 ' );
98
+ $ assetstoreDao2 = $ this ->validSaveTestcase ('test_assetstore_2 ' , '/testassetstore2/path ' , '1 ' );
99
+ $ assetstoreDao3 = $ this ->validSaveTestcase ('test_assetstore_3 ' , '/testassetstore3/path ' , '2 ' );
100
+
101
+ // make sure one got saved
102
+ $ found = $ this ->Assetstore ->findBy ('name ' , 'test_assetstore_3 ' );
103
+ $ this ->assertNotEmpty ($ found );
104
+ $ savedDao = $ found [0 ];
105
+ $ this ->assertTrue ($ this ->Assetstore ->compareDao ($ assetstoreDao3 , $ savedDao ));
106
+
107
+ // Incorrect Create Tests
108
+
109
+ // create a new one with empty path, should fail
110
+ $ this ->invalidSaveTestcase ('test_assetstore_4 ' , '' , '0 ' );
111
+
112
+ // create a new one with same path as existing ones, should fail
113
+ $ this ->invalidSaveTestcase ('test_assetstore_4 ' , '/testassetstore1/path ' , '0 ' );
114
+
115
+ // create a new one with empty name, should fail
116
+ $ this ->invalidSaveTestcase ('' , '/testassetstore4/path ' , '0 ' );
117
+
118
+ // create a new one with same path as existing ones, should fail
119
+ $ this ->invalidSaveTestcase ('test_assetstore_3 ' , '/testassetstore4/path ' , '0 ' );
120
+
121
+ // Incorrect Edit&Save Tests
122
+
123
+ // take existing, try to save with empty name
124
+ $ savedName = $ assetstoreDao1 ->getName ();
125
+ $ this ->invalidSaveTestcase ('' , $ assetstoreDao1 ->getPath (), $ assetstoreDao1 ->getType (), $ assetstoreDao1 );
126
+ $ assetstoreDao1 ->setName ($ savedName );
127
+
128
+ // take existing, try to save with empty path
129
+ $ savedPath = $ assetstoreDao1 ->getPath ();
130
+ $ this ->invalidSaveTestcase ($ assetstoreDao1 ->getName (), '' , $ assetstoreDao1 ->getType (), $ assetstoreDao1 );
131
+ $ assetstoreDao1 ->setPath ($ savedPath );
132
+
133
+ // take existing, try to save with a colliding name
134
+ $ savedName = $ assetstoreDao1 ->getName ();
135
+ $ this ->invalidSaveTestcase ($ assetstoreDao2 ->getName (), $ assetstoreDao1 ->getPath (), $ assetstoreDao1 ->getType (), $ assetstoreDao1 );
136
+ $ assetstoreDao1 ->setName ($ savedName );
137
+
138
+ // take existing, try to save with a colliding path
139
+ $ savedPath = $ assetstoreDao1 ->getPath ();
140
+ $ this ->invalidSaveTestcase ($ assetstoreDao1 ->getName (), $ assetstoreDao2 ->getPath (), $ assetstoreDao1 ->getType (), $ assetstoreDao1 );
141
+ $ assetstoreDao1 ->setPath ($ savedPath );
142
+
143
+ // Correct Edit&Save Tests
144
+
145
+ // take existing, try to save with a non-colliding name
146
+ $ savedName = $ assetstoreDao1 ->getName ();
147
+ $ newName = 'noncollidingname1 ' ;
148
+ $ this ->validSaveTestcase ($ newName , $ assetstoreDao1 ->getPath (), $ assetstoreDao1 ->getType (), $ assetstoreDao1 );
149
+
150
+ // check that the new value is saved
151
+ $ found = $ this ->Assetstore ->findBy ('name ' , $ newName );
152
+ $ this ->assertNotEmpty ($ found );
153
+ $ foundDao = $ found [0 ];
154
+ $ this ->assertTrue ($ this ->Assetstore ->compareDao ($ foundDao , $ assetstoreDao1 ));
155
+ $ this ->assertEquals ($ foundDao ->getName (), $ newName );
156
+ $ assetstoreDao1 ->setName ($ savedName );
157
+
158
+
159
+ // take existing, try to save with a non-colliding path
160
+ $ savedPath = $ assetstoreDao1 ->getPath ();
161
+ $ newPath = 'noncolliding/path ' ;
162
+ $ this ->validSaveTestcase ($ assetstoreDao1 ->getName (), $ newPath , $ assetstoreDao1 ->getType (), $ assetstoreDao1 );
163
+
164
+ // check that the new value is saved
165
+ $ found = $ this ->Assetstore ->findBy ('path ' , $ newPath );
166
+ $ this ->assertNotEmpty ($ found );
167
+ $ foundDao = $ found [0 ];
168
+ $ this ->assertTrue ($ this ->Assetstore ->compareDao ($ foundDao , $ assetstoreDao1 ));
169
+ $ this ->assertEquals ($ foundDao ->getPath (), $ newPath );
170
+ $ assetstoreDao1 ->setPath ($ savedPath );
171
+
172
+ // delete the newly added ones to clean up
173
+ $ this ->Assetstore ->delete ($ assetstoreDao1 );
174
+ $ this ->Assetstore ->delete ($ assetstoreDao2 );
175
+ $ this ->Assetstore ->delete ($ assetstoreDao3 );
176
+ }
177
+
178
+ }
0 commit comments