CWIS Developer Documentation
Item--Test.php
Go to the documentation of this file.
1 <?PHP
2 
7  class MockItem extends Item
8 {
9  public function __construct($MockItemId)
10  {
11  parent::__construct($MockItemId);
12  }
13 
14  public static function Create()
15  {
16  $MockItemValues = array(
17  "MockItemName" => "Fake Item",
18  "DateCreated" => "1989-01-23 12:34:56",
19  "CreatedBy" => 1,
20  "DateLastModified" => "1998-07-06 05:04:32",
21  "LastModifiedBy" => 1);
22 
23  $MockItem = parent::CreateWithValues($MockItemValues);
24 
25  return $MockItem;
26  }
27 }
28 
32 class MockItemTwo extends Item { }
33 
34 class Item_Test extends PHPUnit\Framework\TestCase
35 {
36  function testAll()
37  {
38  # construct MockItem with invalid Id
39  try
40  {
41  $Mock = new MockItem(PHP_INT_MAX);
42  $this->assertTrue(FALSE, "Exception not thrown on invalid Id");
43  }
44  catch (Exception $E)
45  {
46  $this->assertEquals(get_class($E), "InvalidArgumentException",
47  "Testing constructing MockItem with invalid Id");
48  }
49 
50  $Mock = MockItem::Create();
51 
52  $this->assertEquals($Mock->Id(), 1, "Testing Id()");
53  $this->assertEquals($Mock->GetCanonicalId($Mock->Id()), 1,
54  "Testing GetCanonicalId()");
55 
56  $this->assertEquals($Mock->Name(), "Fake Item", "Testing getting Name()");
57  $this->assertEquals($Mock->Name("New Name"), "New Name",
58  "Testing setting Name()");
59 
60  $this->assertEquals($Mock->DateCreated(), "1989-01-23 12:34:56",
61  "Testing getting DateCreated()");
62  $this->assertEquals($Mock->DateCreated("1901-12-31 12:59:45"),
63  "1901-12-31 12:59:45", "Testing setting DateCreated()");
64 
65  $this->assertEquals($Mock->CreatedBy(), 1, "Testing getting CreatedBy()");
66  $this->assertEquals($Mock->CreatedBy(2), 2, "Testing setting CreatedBy()");
67 
68  $DateNow = date(StdLib::SQL_DATE_FORMAT);
69  $this->assertEquals($Mock->DateLastModified(), "1998-07-06 05:04:32",
70  "Testing getting DateLastModified()");
71  $this->assertEquals($Mock->DateLastModified($DateNow),
72  $DateNow, "Testing setting DateLastModified()");
73 
74  $this->assertEquals($Mock->LastModifiedBy(), 1,
75  "Testing getting LastModifiedBy()");
76  $this->assertEquals($Mock->LastModifiedBy(2), 2,
77  "Testing setting LastModifiedBy()");
78 
79  $this->assertEquals($Mock->ItemExists(NULL), FALSE,
80  "Testing ItemExists() with input NULL");
81  $this->assertEquals($Mock->ItemExists($Mock->Id()), TRUE,
82  "Testing ItemExists() with valid Id");
83  $this->assertEquals($Mock->ItemExists($Mock->Id() + 1), FALSE,
84  "Testing ItemExists() with invalid Id");
85  $this->assertEquals($Mock->ItemExists($Mock), TRUE,
86  "Testing ItemExists() with ItemMock object");
87 
88  # test ItemExists() for a non-Item object
89  try
90  {
91  $Directory = dir("objects/tests");
92  MockItem::ItemExists($Directory);
93  $this->assertTrue(FALSE,
94  "Exception not thrown on invalid ItemExists() call");
95  }
96  catch (Exception $E)
97  {
98  $this->assertEquals(get_class($E), "Exception",
99  "Testing ItemExists() in ItemMock with Directory object");
100  }
101 
102  # test ItemExists() for another Item child type
103  try
104  {
106  $this->assertTrue(FALSE,
107  "Exception not thrown on invalid ItemExists() call");
108  }
109  catch (Exception $E)
110  {
111  $this->assertEquals(get_class($E), "Exception",
112  "Testing ItemExists() in ItemMockTwo with ItemMock object");
113  }
114 
115  $MockItemId = $Mock->Id();
116  $Mock->Delete();
117  $this->assertEquals($Mock->ItemExists($MockItemId), FALSE,
118  "Testing ItemExists() with deleted object");
119  }
120 
124  static function setUpBeforeClass()
125  {
126  $DB = new Database();
127  $DB->Query("CREATE TABLE MockItems (
128  MockItemId INT NOT NULL AUTO_INCREMENT,
129  MockItemName TEXT,
130  DateCreated DATETIME,
131  CreatedBy INT,
132  DateLastModified DATETIME,
133  LastModifiedBy INT,
134  INDEX Index_I (MockItemId)
135  );"
136  );
137 
138  $DB->Query("CREATE TABLE MockItemTwos (
139  MockItemTwoId INT NOT NULL AUTO_INCREMENT,
140  MockItemTwoName TEXT,
141  DateCreated DATETIME,
142  CreatedBy INT,
143  DateLastModified DATETIME,
144  LastModifiedBy INT,
145  INDEX Index_I (MockItemTwoId)
146  );"
147  );
148 
149  }
150 
154  static function tearDownAfterClass()
155  {
156  $DB = new Database();
157  $DB->Query("DROP TABLE MockItems");
158  $DB->Query("DROP TABLE MockItemTwos");
159  }
160 }
static tearDownAfterClass()
Destroy tables created for testing.
Definition: Item--Test.php:154
SQL database abstraction object with smart query caching.
Definition: Database.php:22
static Create()
Definition: Item--Test.php:14
Create a MockItem extending Item for testing because Item is an abstract class.
Definition: Item--Test.php:7
$DB
Definition: Item.php:210
Dummy Item child class.
Definition: Item--Test.php:32
Common base class for persistent items store in database.
Definition: Item.php:13
__construct($MockItemId)
Definition: Item--Test.php:9
const SQL_DATE_FORMAT
Format to feed to date() to get SQL-compatible date/time string.
Definition: StdLib.php:913
static setUpBeforeClass()
Create tables for MockItem before testing.
Definition: Item--Test.php:124
static ItemExists($Id)
Check whether an item exists with the specified ID.
Definition: Item.php:162