CWIS Developer Documentation
Classification--Test.php
Go to the documentation of this file.
1 <?PHP
2 
3 class Classification_Test extends PHPUnit\Framework\TestCase
4 {
5  protected static $TestFieldIds;
6  protected static $TestFields;
7 
14  public static function setUpBeforeClass()
15  {
16  # construct the schema object
17  $Schema = new MetadataSchema(
19 
20  self::$TestFieldIds = array();
21 
22  # outline fields to be created
23  self::$TestFields = array(
24  "ClassificationTestField" => MetadataSchema::MDFTYPE_TREE,
25  );
26 
27  # create the fields
28  foreach (self::$TestFields as $FieldName => $FieldType)
29  {
30  $TmpField = $Schema->GetItemByName($FieldName);
31  if ($TmpField === NULL)
32  {
33  $TmpField = $Schema->AddField($FieldName, $FieldType);
34  }
35  $TmpField->IsTempItem(FALSE);
36  self::$TestFieldIds[$FieldName] = $TmpField->Id();
37  }
38 
39  }
40 
45  public static function tearDownAfterClass()
46  {
47  # construct the schema object
48  $Schema = new MetadataSchema(
50  $Database = new Database();
51 
52  # drop all of the test fields
53  foreach (self::$TestFieldIds as $FieldName => $FieldId)
54  {
55  $Schema->DropField($FieldId);
56  }
57  }
58 
59 
60  public function testClassification()
61  {
62  $MyId = self::$TestFieldIds['ClassificationTestField'];
63 
64  $TestParent = Classification::Create(
65  "TestParent", $MyId);
66 
67  $this->assertInstanceOf(
68  Classification::class, $TestParent);
69  $this->assertEquals(
70  $TestParent->FieldId(), $MyId);
71 
72 
73  # attempt to create a duplicate w/o setting a parentid
74  try
75  {
77  "TestParent", $MyId);
78  $this->fail(
79  "Exception not thrown on creation of duplicate Classification");
80  }
81  catch (Exception $e)
82  {
83  $this->assertEquals(
84  $e->getMessage(),
85  "Duplicate name specified for new classification "
86  ."(TestParent).");
87  }
88 
89  # attempt to create a duplicate top-level class
90  try
91  {
93  "TestParent", $MyId, Classification::NOPARENT);
94  $this->fail(
95  "Exception not thrown on creation of duplicate Classification");
96  }
97  catch (Exception $e)
98  {
99  $this->assertEquals(
100  $e->getMessage(),
101  "Duplicate name specified for new classification "
102  ."(TestParent).");
103  }
104 
105  # attempt to create a child with an invalid parent
106  try
107  {
109  "TestBadChild", $MyId, -10);
110  $this->fail(
111  "Exception not thrown when creating with invalid ParentId");
112  }
113  catch (Exception $e)
114  {
115  $this->assertInstanceOf(
116  InvalidArgumentException::class, $e);
117  $this->assertEquals(
118  $e->getMessage(),
119  "Invalid parent ID specified (-10).");
120  }
121 
122  # attempt to create a child, specifying the parent
123  $TestFirstChild = Classification::Create(
124  "FirstChild", $MyId, $TestParent->Id() );
125  $this->assertInstanceOf(
126  Classification::class, $TestFirstChild);
127  $this->assertEquals(
128  $TestFirstChild->ParentId(),
129  $TestParent->Id() );
130 
131  # attempt to create a child, not specifying the parent
132  $TestSecondChild = Classification::Create(
133  "TestParent -- SecondChild", $MyId);
134  $this->assertInstanceOf(
135  Classification::class, $TestSecondChild);
136  $this->assertEquals(
138 
139  # test the various ways to retrieve our name
140  $this->assertEquals(
141  $TestSecondChild->Name(),
142  "TestParent -- SecondChild");
143 
144  $this->assertEquals(
145  $TestSecondChild->FullName(),
146  "TestParent -- SecondChild");
147 
148  $this->assertEquals(
149  $TestSecondChild->SegmentName(),
150  "SecondChild");
151 
152  $this->assertNull(
153  $TestSecondChild->VariantName());
154 
155  # test recalculating
156  $IdsUpdated = $TestSecondChild->RecalcResourceCount();
157 
158  $Expected = array(
159  $TestSecondChild->Id(),
160  $TestParent->Id() );
161 
162  sort($IdsUpdated);
163  sort($Expected);
164 
165  $this->assertEquals($IdsUpdated, $Expected);
166 
167  # Test initial Depth values
168  $this->assertEquals(
169  $TestSecondChild->Depth(), 1);
170  $this->assertEquals(
171  $TestParent->Depth(), 0);
172 
173 
174  # test recalculating depth
175  $TestParent->RecalcDepthAndFullName();
176 
177  # verify that names and depths remain correct after recalc
178  $this->assertEquals(
179  $TestSecondChild->FullName(),
180  "TestParent -- SecondChild");
181  $this->assertEquals(
182  $TestSecondChild->SegmentName(),
183  "SecondChild");
184 
185  $this->assertEquals(
186  $TestSecondChild->Depth(), 1);
187  $this->assertEquals(
188  $TestParent->Depth(), 0);
189 
190  $this->assertEquals(
191  $TestSecondChild->FullResourceCount(), 0);
192 
193 
194  # test listing children
195  $ChildIds = $TestParent->ChildList();
196  $Expected = array(
197  $TestFirstChild->Id(),
198  $TestSecondChild->Id() );
199  sort($ChildIds);
200  sort($Expected);
201  $this->assertEquals($ChildIds, $Expected);
202 
203  # test deleting
204  $DelCount = $TestSecondChild->Delete();
205  $this->assertEquals(
206  $DelCount, 1);
207 
208  # test deletions that eat our parents
209  $DelCount = $TestFirstChild->Delete(TRUE);
210  $this->assertEquals(
211  $DelCount, 2);
212 
213  }
214 }
static SegmentsCreated()
Get number of new segments (Classifications) generated when creating a new Classification with a full...
Metadata schema (in effect a Factory class for MetadataField).
SQL database abstraction object with smart query caching.
Definition: Database.php:22
static Create($Name, $FieldId, $ParentId=NULL)
Add new classification to the hierarchy.
const NOPARENT
Parent value for classifications with no parent.
static tearDownAfterClass()
After to running the tests, this function is run.
static setUpBeforeClass()
Prior to running any of the tests, this function is run.