Odyssey
CalendarNotifyTest.php
1 <?php
2 
3 use PHPUnit\Framework\TestCase;
4 
5 /**
6  * @var CalendarNotifyTest
7  * Tests the functions in sCalendarNotify.i.
8  */
9 class CalendarNotifyTest extends TestCase {
10 
11  /**
12  * function setup()
13  * Sets up the includes for the functions.
14  */
15  function setup() {
16  $dir = "/var/www/html/system/library";
17  require_once("$dir/sCalendarNotify.i");
18  }
19 
20  /**
21  * function test_DoICare()
22  * Tests the function DoICare for determining if the scheduled job should run.
23  */
24  function test_DoICare() {
25  $currentDate = null;
26  $results = DoICare($currentDate);
27  $this->assertEquals($results["error"], "Please enter the current date.");
28 
29  $currentDate = "invalid date";
30  $results = DoICare($currentDate);
31  $this->assertEquals($results["error"], "Current date is invalid.");
32 
33  $currentDate = "2018-09-01";
34  $results = DoICare($currentDate);
35  $this->assertEquals($results["status"], "000");
36  $this->assertEquals($results["doICare"], false);
37 
38  $currentDate = "2018-11-01";
39  $results = DoICare($currentDate);
40  $this->assertEquals($results["status"], "000");
41  $this->assertEquals($results["doICare"], true);
42 
43  $currentDate = "2018-12-25";
44  $results = DoICare($currentDate);
45  $this->assertEquals($results["status"], "000");
46  $this->assertEquals($results["doICare"], true);
47  }
48 
49  /**
50  * function test_ShouldISendEmail()
51  * Tests the function ShouldISendEmail for determining if the scheduled job should sent email(s) to the Credit Union.
52  */
53  function test_ShouldISendEmail() {
54  $currentDate = null;
55  $lastNotifiedDate = null;
56  $results = ShouldISendEmail($currentDate, $lastNotifiedDate);
57  $this->assertEquals($results["error"], "Please enter the current date.");
58 
59  $currentDate = "invalid date";
60  $results = ShouldISendEmail($currentDate, $lastNotifiedDate);
61  $this->assertEquals($results["error"], "Current date is invalid.");
62 
63  $currentDate = "2018-11-03";
64  $results = ShouldISendEmail($currentDate, $lastNotifiedDate);
65  $this->assertEquals($results["status"], "000");
66  $this->assertEquals($results["shouldISendEmail"], true); // Logic is in the do I care function. This function is AFTER consulting the database for the last notified date.
67 
68  $lastNotifiedDate = "invalid date";
69  $results = ShouldISendEmail($currentDate, $lastNotifiedDate);
70  $this->assertEquals($results["error"], "Last notified date is invalid.");
71 
72  $lastNotifiedDate = "2018-11-04"; // After the current date
73  $results = ShouldISendEmail($currentDate, $lastNotifiedDate);
74  $this->assertEquals($results["error"], "Last notified date cannot be after the current date.");
75 
76  $lastNotifiedDate = "2018-11-03";
77  $results = ShouldISendEmail($currentDate, $lastNotifiedDate);
78  $this->assertEquals($results["status"], "000");
79  $this->assertEquals($results["shouldISendEmail"], false);
80 
81  $lastNotifiedDate = "2018-11-01";
82  $results = ShouldISendEmail($currentDate, $lastNotifiedDate);
83  $this->assertEquals($results["status"], "000");
84  $this->assertEquals($results["shouldISendEmail"], false);
85 
86  $lastNotifiedDate = "2018-10-19";
87  $results = ShouldISendEmail($currentDate, $lastNotifiedDate);
88  $this->assertEquals($results["status"], "000");
89  $this->assertEquals($results["shouldISendEmail"], true);
90 
91  $lastNotifiedDate = "2018-10-20"; // Two weeks before.
92  $results = ShouldISendEmail($currentDate, $lastNotifiedDate);
93  $this->assertEquals($results["status"], "000");
94  $this->assertEquals($results["shouldISendEmail"], true);
95 
96  $lastNotifiedDate = "2018-10-21";
97  $results = ShouldISendEmail($currentDate, $lastNotifiedDate);
98  $this->assertEquals($results["status"], "000");
99  $this->assertEquals($results["shouldISendEmail"], false);
100 
101  $currentDate = "2019-12-23";
102  $lastNotifiedDate = "2019-12-16"; // One week before.
103  $results = ShouldISendEmail($currentDate, $lastNotifiedDate);
104  $this->assertEquals($results["status"], "000");
105  $this->assertEquals($results["shouldISendEmail"], true);
106 
107  $lastNotifiedDate = "2019-12-15";
108  $results = ShouldISendEmail($currentDate, $lastNotifiedDate);
109  $this->assertEquals($results["status"], "000");
110  $this->assertEquals($results["shouldISendEmail"], true);
111 
112  $lastNotifiedDate = "2019-12-17";
113  $results = ShouldISendEmail($currentDate, $lastNotifiedDate);
114  $this->assertEquals($results["status"], "000");
115  $this->assertEquals($results["shouldISendEmail"], false);
116 
117  $lastNotifiedDate = "2018-10-14";
118  $currentDate = "2018-11-14";
119  $results = ShouldISendEmail($currentDate, $lastNotifiedDate);
120  $this->assertEquals($results["status"], "000");
121  $this->assertEquals($results["shouldISendEmail"], true);
122  }
123 
124  /**
125  * function test_VerifyOpts()
126  * This tests the function VerifyOpts for determining if the options from the command line are correct.
127  */
128  function test_VerifyOpts() {
129  $commandOptions = false;
130  $results = VerifyOpts($commandOptions);
131  $this->assertEquals($results["error"], "getopt is invalid.");
132 
133  $commandOptions = array("help" => true);
134  $results = VerifyOpts($commandOptions);
135  $this->assertEquals($results["error"], "Help is set.");
136 
137  $commandOptions = array();
138  $results = VerifyOpts($commandOptions);
139  $this->assertEquals($results["status"], "000");
140  $this->assertEquals($results["opts"]["date"], date("Y-m-d"));
141 
142  $commandOptions = array("d" => "2019-03-03");
143  $results = VerifyOpts($commandOptions);
144  $this->assertEquals($results["status"], "000");
145  $this->assertEquals($results["opts"]["date"], "2019-03-03");
146  $this->assertEquals($results["opts"]["dryrun"], false);
147 
148  $commandOptions = array("d" => "2019-03-09", "dryrun" => false); // According to the documentation, options that don't have a value are set to false.
149  $results = VerifyOpts($commandOptions);
150  $this->assertEquals($results["status"], "000");
151  $this->assertEquals($results["opts"]["date"], "2019-03-09");
152  $this->assertEquals($results["opts"]["dryrun"], true);
153  }
154 }