Objective
The objective of this assignment is to become familiar with MySQL/MariaDB and
phpMyAdmin and to get experience in developing and testing database applications
implemented on top of MySQL/MariaDB and PHP/PDO.
Please be aware that setting up the necessary environment for this assignment will take some
time. Be prepared for some obstacles before you can start programming and that
programming in a new and unfamiliar environment may take more time – and cause more
frustration – than you are used to when programming. You should therefore start working on
the assignment early in this three-week period. Please contact the lecturer or the teaching
assistants early to get help passing the obstacles.
Submission Requirements
You may work in teams of up to three students – or you may decide to work completely by yourself.
In either case, you need to enrol before you can submit your report.
Use phpMyAdmin to create a database for the assignment. The database must contain a table book
for storing information about books. The table should contain four columns:
• id: holding integer values; should be defined as PRIMARY KEY with auto_increment
• title: a variable length text string of up to 500 utf8 (utf8_danish_ci) characters; should be
defined as mandatory, i.e., should not be nullable.
• author: a variable length text string of up to 250 utf8 characters (utf8_danish_ci); should be
defined as mandatory, i.e., should not be nullable
• description: a variable length text string of up to 2000 utf8 characters (utf8_danish_ci);
should be defined as nullable.
Add three rows to your book table (id’s will be created automatically):
• PHP – Cookies
• PHP – Sessions
• PHP - Sending Emails
The application is based on the MVC tutorial you worked on in Part II, but where functionality has
been added for inserting new and for modifying or deleting existing information about books in the
collection. Book information is only kept in the session object in the current version of the application.
Your assignment is to replace the existing model with one that stores data in MariaDB/MySQL and to
add functionality for checking data submitted by the user. An empty DBModel.php files is provided in
the Model directory. You need to complete the implementation of the methods in this file.
1. Make sure that the proper version of PHP is in your path by running php --version in a
shell/command window. You should have version 7 or later.
Windows hints: Add <progRoot>\xampp\php (where <progRoot>\ is the directory
where you installed xampp, for instance c:\extras) to the PATH environment variable if
php is not recognised as a command. See
https://www.computerhope.com/issues/ch000549.htm, if you never set the path before.
Mac OS X hint: See this: https://stackoverflow.com/questions/27011941/mac-osx-php-and-
xampp-path-issue for a description of how to get the xampp php to be the selected version. See
https://www.architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/, if
you never set the path before.
2. Install the Composer as explained here: https://getcomposer.org/download/.
3. Go to the root directory of the assignment code base (see Task 1).
4. Install Codeception by using the Composer and initialize:
Windows hints: Remember to use \ as the path separator character.
composer require codeception/codeception --dev
./vendor/bin/codecept bootstrap
./vendor/bin/codecept generate:cest acceptance BookCollection
./vendor/bin/codecept generate:test unit BookCollection
5. Replace the newly created tests/acceptance/BookCollectionCest.php and
tests/unit/BookCollectionTest.php with the ones in the src/testInit
directory of the repository
6. Copy src/testInit/assignment1.sql to the tests/_data directory
7. Create a database named test in MySQL
8. Set your application’s url in tests/acceptance.suite.yml:
url: http://127.0.0.1/IMT2571/assignment1/
9. Add a db entry to each of the files tests/acceptance.suite.yml and
tests/unit.suite.yml where yourUserName and yourPassword are replaced
with your own database user name and password
- Db:
dsn: 'mysql:host=localhost;dbname=test'
user: 'yourUserName'
password: 'yourPassword'
dump: 'tests/_data/assignment1.sql'
populate: true # run populator before all tests
cleanup: true # run populator before each test
1. Draw a UML sequence diagram that shows the calls between the test, Codeception, the
relevant model, view, and control classes, and the database server for the
BookCollectionCest::successfulAddBookTest()
2. Complete the implementation of the acceptance tests.
Task 4 – Replacing the Session-based Model with one using a Database for
Storage and Introduce Unit Testing for the Model
The next thing to do is to replace the implementation of the model so that it reads and writes data to
your database rather than to the session object. This means that you need to implement the public
functions in the DBModel class.
• __construct()
• getBookList()
• getBookById()
• addBook()
• modifyBook()
• deleteBook()
To be able to see the application running on MySQL in your web browser, you also need to modify the
controller to create a DBModel rather than a Model as the application model (see comments in the
Controller code).
We recommend you to develop the DBModel using a test-driven development approach. This means
that you should first develop a unit test, and then you develop the corresponding DBModel code. The
code for testing addBook when valid data are passed is already implemented. Your work is to:
Coding requirements:
• Adhere to the PSR-2 coding style guide (http://www.php-fig.org/psr/psr-2/).
• You MUST use PDO (implementations using the mysql or mysqli libraries will not pass).
• You MUST get the latest book id from the database (PDO::lastInsertId()) and assign
it to the book object after a book is added to the database.
• You MUST use prepared statements in PDO and bind all user-provided data to the prepared
statement parameters.