Request::enableHttpMethodParameterOverride(); $app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/../views')); $app->get("/", function () use($app) { return $app['twig']->render('index.html.twig'); }); $app->get("/librarian", function () use($app) { return $app['twig']->render('librarian.html.twig', array('books' => Book::getAll(), 'authors' => Author::getAll())); }); $app->post("/librarian", function () use($app) { $title = $_POST['title']; $book = new Book($title); $book->save(); $name = $_POST['author']; $author = new Author($name); $author->save(); $book->addAuthor($author); $book->addCopy($_POST['copies']); return $app['twig']->render('librarian.html.twig', array('books' => Book::getAll())); }); $app->delete("/book/{id}/delete", function ($id) use($app) { $book = Book::find($id); $book->deleteBook(); return $app['twig']->render('librarian.html.twig', array('books' => Book::getAll(), 'authors' => Author::getAll())); }); $app->get("/book/{id}/edit", function ($id) use($app) { $book = Book::find($id); return $app['twig']->render("edit_book.html.twig", array("book" => $book)); }); $app->patch("/book/{id}", function ($id) use($app) { $book = Book::find($id); if (!empty($_POST['title'])) {
function testGetAuthors() { //Arrange $book_title = "Snow Crash"; $id = 1; $test_book = new Book($book_title, $id); $test_book->save(); $author_name = "Super Dog"; $id2 = 2; $test_author = new Author($author_name, $id2); $test_author->save(); $author_name2 = "Neal Stephenson"; $id3 = 3; $test_author2 = new Author($author_name2, $id3); $test_author2->save(); //Act $test_book->addAuthor($test_author); $test_book->addAuthor($test_author2); $result = $test_book->getAuthors(); //Assert $this->assertEquals($result, [$test_author, $test_author2]); }
function testGetAuthors() { //Arrange $id = null; $name = "A Series of Unfortunate Events"; $test_book = new Book($id, $name); $test_book->save(); $name2 = "Lemony Snicket"; $test_author = new Author($id, $name2); $test_author->save(); $test_book->addAuthor($test_author); $name3 = "J.R.R. Tolkien"; $test_author2 = new Author($id, $name3); $test_author2->save(); $test_book->addAuthor($test_author2); //Act $result = $test_book->getAuthors(); //Assert $this->assertEquals([$test_author, $test_author2], $result); }
function testDelete() { //Arrange $author_name = "Stephen King"; $id = 1; $test_author = new Author($author_name, $id); $test_author->save(); $book_name = "Gattica"; $id = 1; $test_book = new Book($book_name, $id); $test_book->save(); //Act $test_book->addAuthor($test_author); $test_book->deleteOne(); //Assert $this->assertEquals([], $test_author->getBooks()); }
function testGetAuthor() { //Arrange $title = "Grapes of Wrath"; $test_book = new Book($title); $test_book->save(); $first_name = "J.K."; $last_name = "Rowling"; $test_author = new Author($first_name, $last_name); $test_author->save(); $first_name2 = "John"; $last_name2 = "Steinbeck"; $test_author2 = new Author($first_name2, $last_name2); $test_author2->save(); //Act $test_book->addAuthor($test_author); $test_book->addAuthor($test_author2); $result = $test_book->getAuthor(); //Assert $this->assertEquals([$test_author, $test_author2], $result); }
function test_addAuthor_getAuthors() { $title = "Three Blind Mice"; $test_book = new Book($title); $test_book->save(); $title2 = "Chicken Dog"; $test_book2 = new Book($title2); $test_book2->save(); $author_name = "Jimmy Neutron"; $test_author = new Author($author_name); $test_author->save(); $test_book->addAuthor($test_author); $result = $test_book->getAuthors(); $this->assertEquals([$test_author], $result); }
function testGetAuthors() { //Arrange $title = "Eat a Cupcake"; $year_published = 1999; $id = null; $test_book = new Book($title, $year_published, $id); $test_book->save(); $name = "Nathan Young"; $test_author = new Author($name, $id); $test_author->save(); $name2 = "Kyle Pratuch"; $test_author2 = new Author($name2, $id); $test_author2->save(); //Act $test_book->addAuthor($test_author); $test_book->addAuthor($test_author2); //Assert $this->assertEquals([$test_author, $test_author2], $test_book->getAuthors()); }
function test_getAuthors() { //Arrange $title = "dog book"; $test_book = new Book($title); $test_book->save(); $name = "William Wegman"; $test_author = new Author($name); $test_author->save(); $name2 = "Bogus Dogtographer"; $test_author2 = new Author($name2); $test_author2->save(); //Act $test_book->addAuthor($test_author); $test_book->addAuthor($test_author2); //Assert $result = $test_book->getAuthors(); $this->assertEquals([$test_author, $test_author2], $result); }
function test_deleteAllAuthors() { //Arrange $title = "Wesley Pong"; $test_book = new Book($title); $test_book->save(); $name = "History"; $test_author = new Author($name); $test_author->save(); $name2 = "Lit"; $test_author2 = new Author($name2); $test_author2->save(); $test_book->addAuthor($test_author); $test_book->addAuthor($test_author2); //Act $test_book->deleteAllAuthors(); $result = $test_book->getAuthors(); //Assert $this->assertEquals([], $result); }
function test_AddAuthor() { //Arrange $author_name = "Jack London"; $test_author = new Author($author_name); $test_author->save(); $title = "Sea Wolf"; $test_book = new Book($title); $test_book->save(); //Act $result = [$test_author]; $test_book->addAuthor($test_author); //Assert $this->assertEquals($test_book->getAuthors(), $result); }
function testSearchByTitle() { //Arrange $author_first = "J.K."; $author_last = "Rowling"; $test_author = new Author($author_first, $author_last); $test_author->save(); $author_first2 = "Stephen"; $author_last2 = "King"; $test_author2 = new Author($author_first2, $author_last2); $test_author2->save(); $author_first3 = "John"; $author_last3 = "Steinbeck"; $test_author3 = new Author($author_first2, $author_last2); $test_author3->save(); $title = "Grapes of Wrath"; $test_book = new Book($title); $test_book->save(); $test_book->addAuthor($test_author3); $title2 = "Harry Potter"; $test_book2 = new Book($title2); $test_book2->save(); $test_book2->addAuthor($test_author); $title3 = "Evil Wrath"; $test_book3 = new Book($title3); $test_book3->save(); $test_book3->addAuthor($test_author2); $search_string = "Wrath"; //Act $result = Book::searchByTitle($search_string); //Assert $this->assertEquals([$test_book, $test_book3], $result); }
function testAddAuthor() { $book_name = "Yer not a wizard harry"; $test_book = new Book($book_name); $test_book->save(); $author_name = "JK Rowling"; $test_author = new Author($author_name); $test_author->save(); $test_book->addAuthor($test_author); $result = $test_book->getAuthors(); $this->assertEquals($test_author, $result[0]); }
function test_getAuthors() { //Arrange $test_book = new Book("World War Z", "Horror"); $test_book->save(); $test_book2 = new Book("Billy Bartle-Barnaby", "2015-07-09"); $test_book2->save(); $test_author = new Author("High Times", "CHEM420"); $test_author->save(); $test_author2 = new Author("Gavanese Jamelan", "MUSC69"); $test_author2->save(); //Act $test_book->addAuthor($test_author); $test_book->addAuthor($test_author2); $test_book2->addAuthor($test_author2); //Assert $this->assertEquals($test_book->getAuthors(), [$test_author, $test_author2]); }
function testGetAuthors() { $title = "Carrie"; $test_book = new Book($title); $test_book->save(); $name = "Stephen King"; $test_author = new Author($name); $test_author->save(); $name2 = "Joe Hill"; $test_author2 = new Author($name2); $test_author2->save(); $test_book->addAuthor($test_author); $test_book->addAuthor($test_author2); $this->assertEquals([$test_author2, $test_author], $test_book->getAuthors()); }
function test_getAuthors() { //Arrange $name = "Giacomo Bordello"; $test_author = new Author($name); $test_author->save(); $name2 = "Dude Guy"; $test_author2 = new Author($name2); $test_author2->save(); $title = "Clarinet Seduction"; $genre = "Romance"; $test_book = new Book($title, $genre); $test_book->save(); //Act $test_book->addAuthor($test_author); $test_book->addAuthor($test_author2); //Assert $this->assertEquals($test_book->getAuthors(), [$test_author, $test_author2]); }
function testDelete() { //Arrange $title = "Little Cat"; $id = 1; $test_book = new Book($title, $id); $test_book->save(); $name = "Ben"; $id = 1; $test_author = new Author($name, $id); $test_author->save(); //Act $test_book->addAuthor($test_author); $test_book->delete(); //Assert $this->assertEquals([], $test_author->getBooks()); }
function testGetAuthor() { //Arrange $title = "Title"; $id = 1; $test_book = new Book($title, $id); $test_book->save(); $name = "Ping Pong"; $id2 = 1; $test_author = new Author($name, $id2); $test_author->save(); $name2 = "Ding Dong"; $id3 = 2; $test_author2 = new Author($name2, $id3); $test_author2->save(); //Act $test_book->addAuthor($test_author->getId()); $test_book->addAuthor($test_author2->getId()); //Assert $this->assertEquals($test_book->getAuthor(), [$test_author, $test_author2]); }
function test_findByAuthor() { //Arrange $title = "Adventures on Mars"; $test_book = new Book($title); $test_book->save(); $name = "David Foster Wallace"; $test_author = new Author($name); $test_author->save(); $test_book->addAuthor($test_author); //Act $result = Book::findByAuthor("David Foster Wallace"); var_dump($result); //Assert $this->assertEquals($test_book, $result[0]); }
function testGetAuthors() { //Arrange $title = "Harry Potter"; $id = 1; $test_book = new Book($title, $id); $test_book->save(); $name = "JK Rowling"; $id = 1; $test_author = new Author($name, $id); $test_author->save(); $name2 = "George RR Martin"; $id2 = 2; $test_author2 = new Author($name, $id); $test_author2->save(); //Act $test_book->addAuthor($test_author); $test_book->addAuthor($test_author2); $result = $test_book->getAuthors(); //Assert $this->assertEquals([$test_author, $test_author2], $result); }
function testDelete() { //Arrange $book_name = "Intro to Art"; $test_book = new Book($book_name); $test_book->save(); $book_name2 = "Everybody Poops"; $test_book2 = new Book($book_name2); $test_book2->save(); $name = "Arty"; $test_author = new Author($name); $test_author->save(); //Act $test_book->addAuthor($test_author); $test_book->delete(); //Assert $this->assertEquals([], $test_author->getBooks()); }
function testUpdateAuthor() { $name = "Uncle Ben"; $id3 = 3; $test_author = new Author($name, $id3); $test_author->save(); $name2 = "Goof Ball"; $id2 = 2; $test_author2 = new Author($name2, $id2); $test_author2->save(); $title = "Kama Sutra"; $id = 1; $test_book = new Book($title, $id); $test_book->save(); //Act $test_book->addAuthor($test_author); $test_book->updateAuthor($test_author2); //Assert $this->assertEquals($test_book->getAuthors(), [$test_author2]); }
Book::deleteAll(); return $app['twig']->render('main_admin.html.twig', array('books' => Book::getAll())); }); //add new book with author $app->post("/book_added", function () use($app) { // create new book from user entry "add book" $title = $_POST['title']; $new_book = new Book($title); $new_book->save(); // create new author from user entry "add book" // possibly check that the author is already in the database - NOT NOW $name_array = explode(',', $_POST['name']); foreach ($name_array as $name) { $new_author = new Author($name); $new_author->save(); $new_book->addAuthor($new_author); } $new_book->addCopies($_POST['copies']); $books = Book::getAll(); $authors = Author::getAll(); return $app['twig']->render("main_admin.html.twig", array('books' => $books, 'authors' => $authors)); }); //INDIVIDUAL BOOK PAGE - DISPLAYS BOOK INFORMATION $app->get("/book/{id}", function ($id) use($app) { $book = Book::find($id); $book_authors = $book->getAuthors(); return $app['twig']->render("book.html.twig", array('book' => $book, 'authors' => $book_authors, 'copies' => count($book->getCopies()))); }); //ADD AUTHOR TO INDIVIDUAL BOOK PAGE $app->post("/book/{id}/add_author", function ($id) use($app) { $find_book = Book::find($id);
//add symfony debug component use Symfony\Component\Debug\Debug; Debug::enable(); $app = new Silex\Application(); $app['debug'] = true; $server = 'mysql:host=localhost;dbname=library'; $username = '******'; $password = '******'; $DB = new PDO($server, $username, $password); $app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/../views')); $app->get("/", function () use($app) { return $app['twig']->render('index.html.twig', array('books' => Book::getAll())); }); $app->get("/books", function () use($app) { return $app['twig']->render('books.html.twig', array('books' => Book::getAll())); }); // $app->get("/authors", function() use ($app) { // return $app['twig']->render('books.html.twig', array('authors' => Author::getAll())); // }); // $app->post("/books", function () use($app) { $title = $_POST['title']; $Book = new Book($_POST['title']); $Book->save(); $author_name = $_POST['author_name']; $Author = new Author($_POST['author_name']); $Author->save(); $Book->addAuthor($Author); return $app['twig']->render('index.html.twig', array('books' => Book::getAll(), 'author' => $Book->getAuthors())); }); return $app;