Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use IteratorAggregate interface instead of final class #2395

Closed
wants to merge 1 commit into from

Conversation

soyuka
Copy link
Contributor

@soyuka soyuka commented Dec 6, 2021

Q A
Type improvement
BC Break no

Hi, I have unit tests which are mocking the Builder and as Aggregation is a final class (even instantiating it in a test is hard, and even if I do so the getIterator method uses an assert($cursor instanceof Cursor) where Cursor is a final class that we can not instantiate. Using an interface here seems more appropriate, thoughts?

@IonBazan
Copy link
Member

IonBazan commented Dec 6, 2021

Why are you mocking the Builder in your tests? Aggregation does not take that many arguments - you can grab them from the Builder itself.

I think we are missing some context here - could you provide some code examples and share more information about your use case?

@malarzm
Copy link
Member

malarzm commented Dec 6, 2021

I have unit tests which are mocking the Builder

This usually is a bad idea and make tests very fragile. You probably should be writing integration tests that do run against the database, or at least checks whether the query generated by your code makes sense/contains specific stuff.

@soyuka
Copy link
Contributor Author

soyuka commented Dec 7, 2021

I agree with you but this means having a database running. We're unit testing an API Platform's abstraction that fetches a collection. No assertion is done on the queries, we just want to make sure that options are propagated to the execution context. For example: https://github.com/api-platform/core/blob/2.6/tests/Bridge/Doctrine/MongoDbOdm/CollectionDataProviderTest.php#L86

In that case, we want to assert that our doctrine_mongodb options are propagated to the execution context of the builder. I think that it makes sense to be able to unit test this kind of things. I must agree that mocking the whole Builder is a bad idea in general but there are still valid use cases.

@malarzm
Copy link
Member

malarzm commented Dec 7, 2021

Given how the test is mocking everything from the registry down to the builder it should be OK to change it in this way:

  1. Don't mock the builder using Prophecy, write your own implementation of a spy
  • your spy can extend our Builder, it'll be easier that way and Builder itself is not final
  • log what is being passed as an argument to methods you want to inspect (or the fact that methods are called), call parent's method when you want to
  • to avoid using or even mocking Aggregation throw an exception instead of calling parent method
  1. have your mocked repository return the spy instead of mocked aggregation builder
  2. assert on your own that spy was interacted with in an expected way

Or on a second thought you probably should be able to tell Prophecy to throw an exception instead of returning the iterator from the execute method. You can catch the exception in the test (or even set it as expected to be thrown to know the method was indeed called as there's a branch for execute/getAggregation in the code) and still have other assertions tested.

It'll be a bit more work but I don't think that not being able to mock stuff is a valid reason to change a library. Also that'll allow you to work without waiting for a release :)

@stale
Copy link

stale bot commented Apr 16, 2022

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in a week if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Stale Issues that have not seen any activity in 30 days label Apr 16, 2022
@malarzm malarzm added the Idea label Apr 16, 2022
@stale stale bot removed the Stale Issues that have not seen any activity in 30 days label Apr 16, 2022
@malarzm malarzm closed this Apr 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants