-
For this homework, create a directory ~/dynamic_php/itec325/hw02/.
Re-organize our hw01-solution into two parts:
- A file utils.php, which contains the definition of pluralize,
and
- A file utils-test.php, which contains the unit-tests for
pluralize (and, any for other function we add to utils.php).
Of course, this file should require_once the file utils.php.
(5pts) Write a function test : ANY, ANY → void
which determines if the first value (an actual test-result) is equal to the second value
(the desired test-result).
If the two are not equal, it should print a message saying that a test failed.
If the two are equal, it should merely print “.”.
(Note that this is the only functions we'll write all semester,
whose purpose is to print something
rather than return a value.)
We'll use this function to make our testing more convenient.
For example, your tests from
hw01
could be replaced with lines like “test( pluralize(4,"cow"), "4 cows" );”.
This way, only tests that fail get printed,
and we don't need to visually scan through lots of
output about successful tests —
if you see “......” you know you're good.
(And if any test failed, you'll get an error message blaring at you, that you can't miss.)
Optional (for slight extra credit):
These further features make test even more useful:
- (2pts)recommended When testing functions which return html,
it's a bit annoying if a test were to fail due only to whitespace (e.g. our
function returned two spaces where the expected-output only had one space,
or had a newline).
SO: before comparing strings,
collapse all groups of one-or-more whitespace
characters to a single space.
(Hint: use preg_replace('/\s+/', ' ', …),
but only call it your if your two inputs
is_strings.)
You may want to also trim whitespace from the start and end of
each string.
Note that rectifying whitespace is particularly helpful
when you are comparing test-cases-written-on-a-Windows-system
(where a
newline uses two bytes),
and then running the code on php.radford.edu (a UNIX machine, where
any newlines are one byte).
(Alternately, you can also run dos2unix on files
that were originally created in Windows.)
- (2pts)
In the UNIX philosophy, it's nice to only see printed messages when something
goes wrong.
Add a “switch”, so that you can get this behavior if you like:
either a third, optional argument to test,
or define a named-constant
or global-variable
just before your definition of test.
Then, by changing that one boolean value it controls whether or not successful-tests
print nothing, or print “.”.
(Be sure that test always prints something when a test fails, though!)
- (2pts)
Add an optional third parameter: a boolean to decide if you really want to “normalize”
string whitespace, or not.
Its default value should be false.
- (2pts)
You may add further features if you like, such as keep a counter of how many tests
have been made, and then any failing-tests can include the test-number
as well as other details.
(Myself, I made an additional function testResults, which prints those statistics.)
(You don't need to have test-cases for test itself;
since it's a void function that prints, it's not suitable for unit-testing.
This means you need to be extra careful to make sure it really does work as you expect.)
Put this function into utils.php -- it is a utility function-definition.
It doesn't go in
utils-test.php.
because it is not itself testing anything
(it's a function that your unit-tests will call to help them do their job!)
- (5pts)
Move your hw01 test cases into utils-test.php,
and re-write them to call test instead of doing any printing directly.
- (5pts)
Make a file silly.php which:
-
Prints a web-page. A silly, exceedingly short web-page.
Of course, it should be proper (x)html(5),
as one of the standard-requirements for
this class.
Mostly, that means making sure every tag closed (including br
and p tags),
and every attribute should have a value, enclosed in quotes).
-
It must call pluralize, hyperlink, and thumbnail somewhere.
-
It should contain “<?php” exactly once at the very top,
end with “?>”,
and be filled with echo statements in between.
-
(5pts) Make sure silly.php is runnable via the web at
https://php.radford.edu/~yourUserName/itec325/hw02/silly.php.
You'll also be doing this for most all the future homeworks.
- (10pts)
Write a function table_r (“table, aligned right”):
It takes an array of strings, and returns a string that is
HTML for a table with one column;
each row is one of the array's values, right-aligned.
For example,
given an array containing
"out of stock",
"2.75",
and
"n/a",
it would return HTML that would render similar to the table at the right.
Your table doesn't have to look exactly like this, but the entries should be aligned
to the right.
For full credit, include a css class for such a table.
hint:To make the next problem easier, apply the CSS to individual cells,
not the row or entire-table.
- (10pts)
Now, generalize the above function so that
if an array entry's key is a string (rather than numeric),
the corresponding row has two columns — the key, followed by the value.
For example,
given an array containing
"Snickers" => "out of stock",
"Milky Way" => "2.75",
and
"happiness" => "n/a",
it would return HTML that would render similar to the table at the right.
If (while making test cases and thinking about any odd inputs) you want to
either document what odd-but-legal inputs do, or you want to document a pre-condition about
what you consider a requirement for a legal input, please do so.
Note that your previous test-cases from #6 should be unchanged — when passed an
array with numeric indices, table_r should give the same result as before.
(You will just turn in this single, more general version of your function,
not two versions of it.)
- (10pts)
Finally:
Upgrade silly.php so that it is a form.
It should have an input for the user's name, and at least one other
input
plus a submit-button.
After submitting, the user should see a page which addresses them by name,
and prints out the other user-provided piece of information as well.
Do not use var_dump($_POST).
(However, if you like you can pass
$_POST to some other
function you've written which accepts an array of key/value strings.)
Remember that silly.php
(or its handler) is not the same as making unit-test cases.