public function testOnDeleteManyToMany() { $this->loadFixtures(['Books', 'Genres', 'BookGenres']); $genre = Genre::find(8); $this->assertEquals(15, $genre->book_count); // Delete a record to go down to 14 $book = Book::find(1); $book->addBehavior($this->object->track('Genres', 'book_count')); $book->delete(); $genre = Genre::find(8); $this->assertEquals(14, $genre->book_count); // Delete multiple records $book = new Book(); $book->addBehavior($this->object->track('Genres', 'book_count')); $book->query(Query::DELETE)->where('series_id', 1)->save(); $genre = Genre::find(8); $this->assertEquals(10, $genre->book_count); }
public function testUpdateWithManyToMany() { $this->loadFixtures(['Genres', 'Books', 'BookGenres']); $book = Book::find(5); $this->assertEquals(new Book(['id' => 5, 'series_id' => 1, 'name' => 'A Dance with Dragons', 'isbn' => '0-553-80147-3', 'released' => '2011-07-19']), $book); $genres = $book->Genres; $this->assertEquals(new ModelCollection([new Genre(['id' => 3, 'name' => 'Action-Adventure', 'book_count' => 8, 'junction' => new Entity(['id' => 14, 'book_id' => 5, 'genre_id' => 3])]), new Genre(['id' => 5, 'name' => 'Horror', 'book_count' => 5, 'junction' => new Entity(['id' => 15, 'book_id' => 5, 'genre_id' => 5])]), new Genre(['id' => 8, 'name' => 'Fantasy', 'book_count' => 15, 'junction' => new Entity(['id' => 13, 'book_id' => 5, 'genre_id' => 8])])]), $genres); // Update the book, a genre, and add a new genre $book->name = 'ADWD'; $genre1 = $genres[0]; $genre1->name = 'Action/Adventure'; $book->link($genre1); $genre2 = new Genre(); $genre2->name = 'Drama'; $book->link($genre2); $this->assertEquals(5, $book->save(['validate' => false])); $this->assertEquals(new Book(['id' => 5, 'series_id' => 1, 'name' => 'ADWD', 'isbn' => '0-553-80147-3', 'released' => '2011-07-19', 'Genres' => new ModelCollection([new Genre(['id' => 3, 'name' => 'Action/Adventure', 'book_count' => 8, 'junction' => new Entity(['id' => 14, 'book_id' => 5, 'genre_id' => 3])]), new Genre(['id' => 5, 'name' => 'Horror', 'book_count' => 5, 'junction' => new Entity(['id' => 15, 'book_id' => 5, 'genre_id' => 5])]), new Genre(['id' => 8, 'name' => 'Fantasy', 'book_count' => 15, 'junction' => new Entity(['id' => 13, 'book_id' => 5, 'genre_id' => 8])]), new Genre(['id' => 12, 'name' => 'Drama', 'book_count' => 0, 'junction' => new Entity(['id' => 46, 'book_id' => 5, 'genre_id' => 12])])])]), Book::select()->with('Genres')->where('id', 5)->first()); }
/** * Test reading data through select(). */ public function testRead() { $this->loadFixtures('Books'); // Single $this->assertEquals(new Book(['series_id' => 1, 'name' => 'A Game of Thrones', 'isbn' => '0-553-10354-7', 'released' => '1996-08-02']), Book::select('series_id', 'name', 'isbn', 'released')->orderBy('_id', 'asc')->first()); // Multiple $this->assertEquals(new ModelCollection([new Book(['series_id' => 3, 'name' => 'The Fellowship of the Ring', 'isbn' => '', 'released' => '1954-07-24']), new Book(['series_id' => 3, 'name' => 'The Two Towers', 'isbn' => '', 'released' => '1954-11-11']), new Book(['series_id' => 3, 'name' => 'The Return of the King', 'isbn' => '', 'released' => '1955-10-25'])]), Book::select('series_id', 'name', 'isbn', 'released')->where('series_id', 3)->orderBy('_id', 'asc')->all()); }
public function testUnlinkManyArray() { $book = new Book(['name' => 'A Game Of Thrones']); $genre1 = new Genre(['name' => 'Horror']); $genre2 = new Genre(['name' => 'Action']); $book->unlinkMany([$genre1, $genre2]); $this->assertEquals(new ModelCollection([$genre1, $genre2]), $book->getRelation('Genres')->getUnlinked()); }