Anda di halaman 1dari 7

Assignment 1

Author: Rune Hjelsvold


Semester: Fall 2018
Submission deadline: Sunday 9 September at 23:59
Last update: 31.08.18 11:30

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.

Ensure that your submission fulfils these requirements:


• Format: Upload a short report, in PDF, to Blackboard. The report should briefly describe
what you have implemented and should contain the sequence diagrams you are asked to
develop in Part III, Task 3 and Task 4. The report shall include a reference to a Git repo where
you keep the source files. Please make sure that the lecturer and the teaching assistants have
access to your repo. Our user names on bitbucket are rhjelsvold, bdrmahesh, simenano, and
Zohaib194.
• Naming Convention: The report that you upload on Blackboard should be named
Assignment1_groupname.pdf, using the Blackboard group name as groupname.
• Program Code: Your program code should follow the coding conventions defined in
http://www.tutorialspoint.com/php/php_coding_standard.htm. Your code must pass all tests
included in the project – acceptance tests as well as unit tests.

Part I – Getting Familiar with MySQL/MariaDB and


phpMyAdmin
This part is intended to help you get started on MySQL/MariaDB. The database created in this part
should be exported to an SQL file. The SQL file must be submitted – ideally as part of the submitted
source code.

Task 1 – Setting up the Development Environment


The first you need is to install the development environment. We recommend installing
MySQL/MariaDB and phpMyAdmin on your own computer/laptop. The easiest way to set up your
own environment is to download and install xampp, which can install MariaDB, PHP, and
phpMyAdmin on Windows, Linux, and OS X. You will need to run the apache server on a different
port if another web server is already running on your computer. You then need to include the port
number after server address when accessing the server (http://localhost:8080/ or http://127.0.0.1:8080/
if apache is running on port 8080).

Task 2 – Getting Familar with phpMyAdmin


You should now be able to visit the phpMyAdmin page on your server (for instance, by visiting
http://localhost/phpmyadmin/ or http://127.0.0.1/phpmyadmin/ if you installed xampp). Spend time
getting to know the user interface and the tools offered by phpMyAdmin. We recommend that you
complete the SiteGround’s phpMyAdmin Tutorials.

Task 3 – Creating a Database

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):

Title Author Description


Jungle Book R. Kipling A classic book.
Moonwalker J. Walker
PHP & MySQL for Dummies J. Valade Written by some smart gal.

Task 4 – Testing SQL Queries


Run the following CRUD (Create, Retrieve, Update, Delete) queries (rerun query #1 after queries #3,
#4, and #5 to study the effects of these) by copying the following SQL statements into the SQL panel
of phpMyAdmin.

1. Retrieve data about all books in the database, ordered by id:


SELECT id, title, author, description
FROM Book
ORDER BY id

IMT2571 Assignment 1 - 2018 2


2. Retrieve data about one specific book (book with id=3 in this example) in the database:
SELECT id, title, author, description
FROM Book
WHERE id=3

3. Create a new row:


INSERT INTO Book(title, author, description)
VALUES('Some title', 'Some author', 'Some description')

4. Update the newly inserted row (assuming id=4):


UPDATE Book
SET title='New title',
author='New author',
description='New description'
WHERE id=4

5. Delete the newly inserted and updated row (assuming id=4):


DELETE FROM Book
WHERE id=4

Part II – Getting Familiar with HTML, PHP and PDO


This part gives you an introduction to HTML, PHP, and PDO. There is no need to submit anything
from this part.

Task 1 – Getting Familiar with HTML


Work your way through the W3C HTML/Training wiki. You may skip Week 4 in the tutorial.

Task 2 – Getting Familiar with PHP


Work your way through the Tutorialpoint PHP Tutorial. You may skip the following chapters:

• PHP – Cookies
• PHP – Sessions
• PHP - Sending Emails

In the advanced PHP section, you only need to visit:

• PHP - Predefined Variables


• PHP - Error Handling
• PHP - Bugs Debugging
• PHP - Date & Time
• PHP - Object Oriented
• PHP - For C Developers

Task 3 – Getting Familiar with the Model View Controller Architecture


Work through the php-html.net tutorial, Model View Controller (MVC) in PHP.

IMT2571 Assignment 1 - 2018 3


Task 4 – Getting Familiar with the PHP Data Object (PDO)
You will be using PDO as the API for accessing MySQL/MariaDB. Please be warned that quite a few
MySQL code samples on Internet is based on the native MySQL API. Please do not be tempted to use
the MySQL API. The hashPHP PDO-tutorial for MySQL will give you a brief introduction to PDO.
You may also enroll in Web in Action’s free MySQL/PDO Tutorial for a more extensive tutorial.

Task 5 – Getting Familiar with Codeception


Get familiar with Codeception by reading the introduction (https://codeception.com/docs/01-
Introduction) and the next four chapters in the Codeception documentation. Please consult the
W3schools CSS Selectors Reference (https://www.w3schools.com/cssref/css_selectors.asp) to see how
CSS can be used to find elements on an HTML page. You may also use their CSS Selector Tester to
get familiar with the different selector options.

Part III – Creating a Database Application


In this part, you will gain experience in developing simple database applications. You will find a small
MVC app on Git/Bitbucket (https://bitbucket.org/rhjelsvold/imt2571_1-2018). You need to fork the
project and work on your fork.

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.

Task 1 – Downloading the Current App


Download the app from https://bitbucket.org/rhjelsvold/imt2571_1-2018. You should store the source
code in a development directory. Then you should create a directory named IMT2571 with a
subdirectory named assignment1 in the document root directory of your web server
(xampp/htdocs) and copy the files and subdirectories (except the tests directory) of the src
directory in the downloaded app to this directory. Check that the application is properly installed by
opening the http://localhost/IMT2571/Assignment1/index.php page on your web server, if running the
app on your local web server, otherwise, replace localhost with the address of your web server.
Use 127.0.0.1 if localhost does not work; include port number if apache is not running on the
standard port. (You may also copy the doc directory and all its content to the web server. You will
then find phpDocumentor-generated documentation for the application under
http://localhost/IMT2571/Assignment1/doc/index.html. NOTE: There is currently no doc directory!)

IMT2571 Assignment 1 - 2018 4


Task 2 – Setting up the Testing Framework and Run the existing Tests
We will be using Codeception (https://codeception.com/) as the testing framework. To install the
testing framework, you first need to install the PHP dependency manager, Composer
(https://getcomposer.org/). A step-by-step procedure for setting up the test framework – including the
composer:

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

IMT2571 Assignment 1 - 2018 5


10. Run the current version of the acceptance tests:
.\vendor\bin\codecept --no-colors run acceptance

Task 3 – Completing the Acceptance Test Suite


Only some of the acceptance tests are currently implemented. In this task you are to familiarize
yourself with the tests that are already implemented and you are to complete the acceptance test suites
by completing the remaining tests.

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:

1. Using phpMyAdmin, create a database called test.


2. Draw a UML sequence diagram that shows the calls between the test, Codeception, the
relevant model classes, and the database server for the
BookCollectionTest::testAddBook().
3. Complete the implementation of the unit tests:
Note: You need to modify the database user name and password in the
BookCollectionTest.php file (in the tests/unit folder) to set values required by
your database.
4. Complete the implementation of the DBModel and test against the newly developed unit tests:
.\vendor\bin\codecept --no-colors run unit
5. Run the acceptance test suite to verify that the system is working as expected even after the
session-based model has been replaced.

IMT2571 Assignment 1 - 2018 6


Hint: Remember that you need to change the database connection parameters used by
DBModel to use the test database rather than your application database.
.\vendor\bin\codecept --no-colors run acceptance

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.

IMT2571 Assignment 1 - 2018 7

Anda mungkin juga menyukai