@@ 1547-1643 (lines=97) @@ | ||
1544 | $this->assertSame($expectedJson, $scope->toJson()); |
|
1545 | } |
|
1546 | ||
1547 | public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailablePreviousLink() |
|
1548 | { |
|
1549 | $baseUrl = 'http://example.com'; |
|
1550 | ||
1551 | $this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
|
1552 | ||
1553 | $total = 10; |
|
1554 | $count = 2; |
|
1555 | $perPage = 2; |
|
1556 | $currentPage = 1; |
|
1557 | $lastPage = 5; |
|
1558 | $currentUrl = 'http://example.com/books/?page=1'; |
|
1559 | $nextUrl = 'http://example.com/books/?page=2'; |
|
1560 | $lastUrl = 'http://example.com/books/?page=5'; |
|
1561 | ||
1562 | $paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface'); |
|
1563 | $paginator->shouldReceive('getCurrentPage')->andReturn($currentPage); |
|
1564 | $paginator->shouldReceive('getLastPage')->andReturn($lastPage); |
|
1565 | $paginator->shouldReceive('getTotal')->andReturn($total); |
|
1566 | $paginator->shouldReceive('getCount')->andReturn($count); |
|
1567 | $paginator->shouldReceive('getPerPage')->andReturn($perPage); |
|
1568 | $paginator->shouldReceive('getUrl')->with(1)->andReturn($currentUrl); |
|
1569 | $paginator->shouldReceive('getUrl')->with(2)->andReturn($nextUrl); |
|
1570 | $paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl); |
|
1571 | ||
1572 | $booksData = [ |
|
1573 | [ |
|
1574 | 'id' => 1, |
|
1575 | 'title' => 'Foo', |
|
1576 | 'year' => '1991', |
|
1577 | '_author' => [ |
|
1578 | 'id' => 1, |
|
1579 | 'name' => 'Dave', |
|
1580 | ], |
|
1581 | ], |
|
1582 | [ |
|
1583 | 'id' => 2, |
|
1584 | 'title' => 'Bar', |
|
1585 | 'year' => '1997', |
|
1586 | '_author' => [ |
|
1587 | 'id' => 2, |
|
1588 | 'name' => 'Bob', |
|
1589 | ], |
|
1590 | ], |
|
1591 | ]; |
|
1592 | ||
1593 | $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
|
1594 | $resource->setPaginator($paginator); |
|
1595 | $scope = new Scope($this->manager, $resource); |
|
1596 | ||
1597 | $expected = [ |
|
1598 | 'data' => [ |
|
1599 | [ |
|
1600 | 'type' => 'books', |
|
1601 | 'id' => '1', |
|
1602 | 'attributes' => [ |
|
1603 | 'title' => 'Foo', |
|
1604 | 'year' => 1991, |
|
1605 | ], |
|
1606 | 'links' => [ |
|
1607 | 'self' => 'http://example.com/books/1', |
|
1608 | ], |
|
1609 | ], |
|
1610 | [ |
|
1611 | 'type' => 'books', |
|
1612 | 'id' => '2', |
|
1613 | 'attributes' => [ |
|
1614 | 'title' => 'Bar', |
|
1615 | 'year' => 1997, |
|
1616 | ], |
|
1617 | 'links' => [ |
|
1618 | 'self' => 'http://example.com/books/2', |
|
1619 | ], |
|
1620 | ], |
|
1621 | ], |
|
1622 | 'meta' => [ |
|
1623 | 'pagination' => [ |
|
1624 | 'total' => 10, |
|
1625 | 'count' => 2, |
|
1626 | 'per_page' => 2, |
|
1627 | 'current_page' => 1, |
|
1628 | 'total_pages' => 5 |
|
1629 | ] |
|
1630 | ], |
|
1631 | 'links' => [ |
|
1632 | 'self' => 'http://example.com/books/?page=1', |
|
1633 | 'first' => 'http://example.com/books/?page=1', |
|
1634 | 'next' => 'http://example.com/books/?page=2', |
|
1635 | 'last' => 'http://example.com/books/?page=5' |
|
1636 | ] |
|
1637 | ]; |
|
1638 | ||
1639 | $this->assertSame($expected, $scope->toArray()); |
|
1640 | ||
1641 | $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"}}],"meta":{"pagination":{"total":10,"count":2,"per_page":2,"current_page":1,"total_pages":5}},"links":{"self":"http:\/\/example.com\/books\/?page=1","first":"http:\/\/example.com\/books\/?page=1","next":"http:\/\/example.com\/books\/?page=2","last":"http:\/\/example.com\/books\/?page=5"}}'; |
|
1642 | $this->assertSame($expectedJson, $scope->toJson()); |
|
1643 | } |
|
1644 | ||
1645 | public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailableNextLink() |
|
1646 | { |
|
@@ 1645-1741 (lines=97) @@ | ||
1642 | $this->assertSame($expectedJson, $scope->toJson()); |
|
1643 | } |
|
1644 | ||
1645 | public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailableNextLink() |
|
1646 | { |
|
1647 | $baseUrl = 'http://example.com'; |
|
1648 | ||
1649 | $this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
|
1650 | ||
1651 | $total = 10; |
|
1652 | $count = 2; |
|
1653 | $perPage = 2; |
|
1654 | $currentPage = 5; |
|
1655 | $lastPage = 5; |
|
1656 | $firstUrl = 'http://example.com/books/?page=1'; |
|
1657 | $previousUrl = 'http://example.com/books/?page=4'; |
|
1658 | $lastUrl = 'http://example.com/books/?page=5'; |
|
1659 | ||
1660 | $paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface'); |
|
1661 | $paginator->shouldReceive('getCurrentPage')->andReturn($currentPage); |
|
1662 | $paginator->shouldReceive('getLastPage')->andReturn($lastPage); |
|
1663 | $paginator->shouldReceive('getTotal')->andReturn($total); |
|
1664 | $paginator->shouldReceive('getCount')->andReturn($count); |
|
1665 | $paginator->shouldReceive('getPerPage')->andReturn($perPage); |
|
1666 | $paginator->shouldReceive('getUrl')->with(1)->andReturn($firstUrl); |
|
1667 | $paginator->shouldReceive('getUrl')->with(4)->andReturn($previousUrl); |
|
1668 | $paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl); |
|
1669 | ||
1670 | $booksData = [ |
|
1671 | [ |
|
1672 | 'id' => 1, |
|
1673 | 'title' => 'Foo', |
|
1674 | 'year' => '1991', |
|
1675 | '_author' => [ |
|
1676 | 'id' => 1, |
|
1677 | 'name' => 'Dave', |
|
1678 | ], |
|
1679 | ], |
|
1680 | [ |
|
1681 | 'id' => 2, |
|
1682 | 'title' => 'Bar', |
|
1683 | 'year' => '1997', |
|
1684 | '_author' => [ |
|
1685 | 'id' => 2, |
|
1686 | 'name' => 'Bob', |
|
1687 | ], |
|
1688 | ], |
|
1689 | ]; |
|
1690 | ||
1691 | $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
|
1692 | $resource->setPaginator($paginator); |
|
1693 | $scope = new Scope($this->manager, $resource); |
|
1694 | ||
1695 | $expected = [ |
|
1696 | 'data' => [ |
|
1697 | [ |
|
1698 | 'type' => 'books', |
|
1699 | 'id' => '1', |
|
1700 | 'attributes' => [ |
|
1701 | 'title' => 'Foo', |
|
1702 | 'year' => 1991, |
|
1703 | ], |
|
1704 | 'links' => [ |
|
1705 | 'self' => 'http://example.com/books/1', |
|
1706 | ], |
|
1707 | ], |
|
1708 | [ |
|
1709 | 'type' => 'books', |
|
1710 | 'id' => '2', |
|
1711 | 'attributes' => [ |
|
1712 | 'title' => 'Bar', |
|
1713 | 'year' => 1997, |
|
1714 | ], |
|
1715 | 'links' => [ |
|
1716 | 'self' => 'http://example.com/books/2', |
|
1717 | ], |
|
1718 | ], |
|
1719 | ], |
|
1720 | 'meta' => [ |
|
1721 | 'pagination' => [ |
|
1722 | 'total' => 10, |
|
1723 | 'count' => 2, |
|
1724 | 'per_page' => 2, |
|
1725 | 'current_page' => 5, |
|
1726 | 'total_pages' => 5 |
|
1727 | ] |
|
1728 | ], |
|
1729 | 'links' => [ |
|
1730 | 'self' => 'http://example.com/books/?page=5', |
|
1731 | 'first' => 'http://example.com/books/?page=1', |
|
1732 | 'prev' => 'http://example.com/books/?page=4', |
|
1733 | 'last' => 'http://example.com/books/?page=5' |
|
1734 | ] |
|
1735 | ]; |
|
1736 | ||
1737 | $this->assertSame($expected, $scope->toArray()); |
|
1738 | ||
1739 | $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"}}],"meta":{"pagination":{"total":10,"count":2,"per_page":2,"current_page":5,"total_pages":5}},"links":{"self":"http:\/\/example.com\/books\/?page=5","first":"http:\/\/example.com\/books\/?page=1","prev":"http:\/\/example.com\/books\/?page=4","last":"http:\/\/example.com\/books\/?page=5"}}'; |
|
1740 | $this->assertSame($expectedJson, $scope->toJson()); |
|
1741 | } |
|
1742 | ||
1743 | public function tearDown() |
|
1744 | { |