mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
Mocking Paginator #5236
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @matiux on GitHub (Aug 31, 2016).
Hi,
I need to test a service that accepts a
Doctrine\ORM\Tools\Pagination\Paginatorobject.I wouldn't use the real repository in the test class. Rather I'd like to mock Paginator with some result so that I can pass it to my service.
I'm looking for the examples in the source code but I still not find them.
Can someone supply some examples to mock Paginator?
Thanks
@Ocramius commented on GitHub (Aug 31, 2016):
A Paginator is just an iterator: you can type-hint against that, usually.
On 31 Aug 2016 1:19 p.m., "Matteo" notifications@github.com wrote:
@matiux commented on GitHub (Aug 31, 2016):
Thanks @Ocramius,
what do you mean with "type-hint against that" ?
@lcobucci commented on GitHub (Aug 31, 2016):
PaginatorimplementsIteratorAggregate(that extendsTraversable). So you can useTraversableas type-hint and pass any iterator you want (instead of mockingPaginatorclass).@matiux commented on GitHub (Aug 31, 2016):
Thanks,
I'm was also able to create a mock:
adding the methods that I need
@Ocramius commented on GitHub (Sep 1, 2016):
That is all waaaaaay too specific mocking of implementation details. Refrain from doing that: it is just making your tests fragile, hard to read and an overall no-value-added.
See what @lcobucci said instead.
Closing here: thought it was on the mailing list, but it's on the tracker, and the tracker is for issues ;-)
@matiux commented on GitHub (Sep 1, 2016):
I have a doubt. Using Traversable object, how can I have, for example
getFirstResult()method? My service (that acceptsPaginatorobject), needs to access toQueryinPaginatorto get the first resultThanks
@Ocramius commented on GitHub (Sep 1, 2016):
You'd inject the query and the paginator separately then. Getting the query from the paginator is usually a bad idea...
@matiux commented on GitHub (Sep 1, 2016):
Paginator is based on the query. You can simply get the query with
$paginator->getQuery(). Passing Paginator and Query separately is a redundant way. My 2 cents.I don't think that the second approach it's wrong :)
@Ocramius commented on GitHub (Sep 1, 2016):
The second approach is wrong because you are assuming that the query that
you retrieve is the same one you gave in. No guarantee on this comes from
that API.
Marco Pivetta
http://twitter.com/Ocramius
http://ocramius.github.com/
On Thu, Sep 1, 2016 at 11:06 AM, Matteo notifications@github.com wrote:
@matiux commented on GitHub (Sep 1, 2016):
According to this logic, from the repository I need to return not only
Doctrine\ORM\Tools\Pagination\Paginatorbut alsoDoctrine\ORM\Query@Ocramius commented on GitHub (Sep 1, 2016):
Or just the iterator. If you want the first result only, you can traverse
the operator only to its first element. Throwing a query object around is
mostly trouble.
On 1 Sep 2016 11:13, "Matteo" notifications@github.com wrote:
@matiux commented on GitHub (Sep 1, 2016):
There is a mistake.
getFirstResult()doesn't return the first item of the collection. It returns the offset of the query.... LIMIT 40 OFFSET 81
It is the parameter that you can set in the query doctrine with
$queryBuilder->setFirstResult($offset);This paramater there isn't in the
Paginatorobject