Testing and Integration
Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 87 of 91.
Challenge
EasyCongratulations on building a complete Library Management System! Now it's time to bring all your components together and verify that everything works seamlessly. You'll create a comprehensive integration test that exercises the full workflow of your library.
You'll organize your code across five files:
Book.php— YourBookclass with readonly properties (isbn,title,author), a private$isAvailableproperty, and methods:isAvailable(),markAsBorrowed(), andmarkAsReturned().User.php— Include the Book file. YourUserclass with readonly properties (id,name), a private$borrowedBooksarray, aMAX_BOOKSconstant of3, and all the user methods.Library.php— Include the User file. YourLibraryclass with book management, borrowing/returning functionality, search methods, and theremoveBook()method.Admin.php— Include the Library file. YourAdminclass extendingUserwithaddBookToLibrary()andremoveBookFromLibrary()methods.main.php— Include the Admin file. This is where you'll run your integration test. You'll receive two inputs: a user name and a search author keyword.Set up your library system:
- Create a
Library - Create an
Adminwith ID1and name"LibraryAdmin" - Create a
Userwith ID2and the provided user name
Have the admin add these three books:
- ISBN:
"978-0", Title:"PHP Fundamentals", Author:"Sarah Johnson" - ISBN:
"978-1", Title:"OOP Patterns", Author:"Mike Chen" - ISBN:
"978-2", Title:"Web Security", Author:"Sarah Johnson"
Then run through this integration test sequence, printing each result on a new line:
- Have the user borrow
"978-0" - Have the user borrow
"978-1" - Search by author using the second input and print the count:
"Books by author: [count]" - Get available books and print:
"Available: [count]" - Have the user return
"978-0" - Get available books again and print:
"Available after return: [count]" - Have the admin remove
"978-0" - Have the admin try to remove
"978-0"again
- Create a
This final challenge validates that your entire system works together — admins managing inventory, users borrowing within limits, searches returning accurate results, and availability updating correctly throughout the workflow.
Try it yourself
<?php
require_once 'Admin.php';
// Read inputs
$userName = trim(fgets(STDIN));
$searchAuthor = trim(fgets(STDIN));
// TODO: Create a Library
// TODO: Create an Admin with ID 1 and name "LibraryAdmin"
// TODO: Create a User with ID 2 and the provided user name
// TODO: Have the admin add three books:
// - ISBN: "978-0", Title: "PHP Fundamentals", Author: "Sarah Johnson"
// - ISBN: "978-1", Title: "OOP Patterns", Author: "Mike Chen"
// - ISBN: "978-2", Title: "Web Security", Author: "Sarah Johnson"
// TODO: Integration test sequence:
// 1. Have the user borrow "978-0"
// 2. Have the user borrow "978-1"
// 3. Search by author using $searchAuthor and print: "Books by author: [count]"
// 4. Get available books and print: "Available: [count]"
// 5. Have the user return "978-0"
// 6. Get available books again and print: "Available after return: [count]"
// 7. Have the admin remove "978-0"
// 8. Have the admin try to remove "978-0" again
?>All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe $this KeywordMethodsPropertiesConstructor (__construct)Destructor (__destruct)Recap - Simple Calculator4Inheritance
Basic InheritanceThe parent:: KeywordMethod OverridingThe final KeywordAbstract ClassesRecap - Employee Hierarchy7Encapsulation
Public, Protected, PrivateAccess Modifiers in DepthGetters and SettersInformation HidingConstructor Promotion (8.0)Recap - Student Records System2Namespaces & Autoloading
Introduction to NamespacesThe use KeywordPSR-4 Autoloading StandardComposer AutoloaderRecap - Organized Project5Interfaces & Contracts
Introduction to InterfacesImplementing InterfacesMultiple Interface ImplementInterface vs Abstract ClassType Hinting with InterfacesRecap - Shape Calculator8Magic Methods
Magic Methods Introduction__toString & __debugInfo__get, __set, __isset, __unset__call & __callStatic__clone & Object Cloning__serialize & __unserializeRecap - Custom Collection11Type System & Error Handling
Type DeclarationsNullable TypesUnion & Intersection TypesException ClassesCustom Exception HierarchyTry, Catch, FinallyRecap - Form Validator14Project: Library Management
Project OverviewBook and User Classes3Class Properties
Instance vs Static PropertiesConstants in ClassesStatic Methods & PropertiesPrivate & Protected PropertiesReadonly Properties (PHP 8.1)Recap - Bank Account Manager6Polymorphism
Method Overriding RevisitedPolymorphism via InterfacesType Hinting & Union TypesLate Static BindingRecap - Payment Processor9Traits
Introduction to TraitsUsing Multiple TraitsTrait Conflict ResolutionAbstract Methods in TraitsTraits vs Inheritance12Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern