테스트 및 통합
Coddy PHP 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨. 91개 중 87번째.
챌린지
쉬움완전한 Library Management System을 구축한 것을 축하합니다! 이제 모든 구성 요소를 하나로 모으고 모든 것이 원활하게 작동하는지 확인할 시간입니다. library의 전체 workflow를 실행하는 종합적인 Integration test를 작성하게 됩니다.
코드를 다섯 개의 파일로 구성합니다:
Book.php: readonly properties(isbn,title,author)를 가진Bookclass, private$isAvailableproperty, 그리고 다음 methods:isAvailable(),markAsBorrowed(),markAsReturned().User.php: Book file을 include합니다. readonly properties(id,name)를 가진Userclass, private$borrowedBooksarray,3이라는MAX_BOOKSconstant, 그리고 모든 user methods를 포함합니다.Library.php: User file을 include합니다. book management, borrowing/returning functionality, search methods, 그리고removeBook()method를 포함하는Libraryclass입니다.Admin.php: Library file을 include합니다.User를 확장하고addBookToLibrary()및removeBookFromLibrary()methods를 포함하는Adminclass입니다.main.php: Admin file을 include합니다. 여기에서 Integration test를 실행합니다. 두 개의 input, 즉 user name과 search author keyword를 받습니다.library system을 설정합니다:
Library를 생성합니다.- ID
1및 name"LibraryAdmin"을 사용하여Admin을 생성합니다. - ID
2및 제공된 user name을 사용하여User를 생성합니다.
admin이 다음 세 권의 책을 추가하도록 합니다:
- 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"
그런 다음 이 Integration test sequence를 실행하고, 각 결과를 새 줄에 출력합니다:
- user가
"978-0"을 borrow하도록 합니다. - user가
"978-1"을 borrow하도록 합니다. - 두 번째 input을 사용하여 author로 search하고 count를 출력합니다:
"Books by author: [count]" - available books를 가져오고 출력합니다:
"Available: [count]" - user가
"978-0"을 return하도록 합니다. - available books를 다시 가져오고 출력합니다:
"Available after return: [count]" - admin이
"978-0"을 remove하도록 합니다. - admin이 다시
"978-0"을 remove하려고 시도하도록 합니다.
이 최종 challenge는 전체 system이 함께 작동하는지 검증합니다. 즉, admin이 inventory를 manage하고, user가 limits 내에서 borrow하며, search가 정확한 results를 반환하고, workflow 전체에서 availability가 올바르게 update되는지를 확인합니다.
직접 해보기
<?php
require_once 'Admin.php';
// 입력 읽기
$userName = trim(fgets(STDIN));
$searchAuthor = trim(fgets(STDIN));
// TODO: Library 생성
// TODO: ID 1과 이름 "LibraryAdmin"으로 Admin 생성
// TODO: ID 2와 제공된 사용자 이름으로 User 생성
// TODO: admin이 세 권의 책을 추가하도록 하기:
// - 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: 통합 테스트 순서:
// 1. 사용자가 "978-0"을 대출하도록 하기
// 2. 사용자가 "978-1"을 대출하도록 하기
// 3. Search by author using $searchAuthor and print: "Books by author: [count]"
// 4. 이용 가능한 책을 가져와 출력: "Available: [count]"
// 5. 사용자가 "978-0"을 반납하도록 하기
// 6. Get available books again and print: "Available after return: [count]"
// 7. 관리자가 "978-0"을 제거하게 하세요
// 8. 관리자가 "978-0"을 다시 제거하려고 시도하게 하세요
?>객체 지향 프로그래밍의 모든 레슨
직접 연습해 보세요: 온라인 PHP 컴파일러