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

Proposal: haveInRepository returns the created entity instead of the primary key #11

Open
TavoNiievez opened this issue Nov 8, 2020 · 2 comments

Comments

@TavoNiievez
Copy link
Member

TavoNiievez commented Nov 8, 2020

In many tests i have come across situations like this:

        $I->haveInRepository(User::class, [
            'name' => 'Jane',
            'email' => '[email protected]',
            'password' => '123456',
            'enabled' => true
        ]);
        $user = $I->grabEntityFromRepository(User::class, [
            'email' => '[email protected]'
        ]);
		// do assertions with the $user

Use haveInRepository, and then be forced to use grabEntityFromRepository to get the entity i just persisted. If i want to check that an entity listener is working, for example, i always have to execute those two statements.

My point is that this would be more useful:

        $user = $I->haveInRepository(User::class, [
            'name' => 'Jane',
            'email' => '[email protected]',
            'password' => '123456',
            'enabled' => true
        ]);
		// do assertions with the $user

if i wanted to know the primary key of that entity, i would simply write:

        $user->getId();

In this context, the ORM is useful for its ability to abstract the details of the persistence process and leave it to me as a programmer to worry more about the data i am working with.

I'm not saying that getting the primary key is not useful, i am saying that it is much more useful to get the entity, where there is more information to work with in my tests, including the persistence identifier.

https://github.com/Codeception/module-doctrine2/blob/1ae32bdcd77abde4a583462f5949fc86b9bda2a2/src/Codeception/Module/Doctrine2.php#L504-L530

Please let me know your opinions about it.

@yesdevnull
Copy link
Contributor

yesdevnull commented Feb 11, 2022

I like the idea of this, I often have to chain haveInRepository() with grabEntityFromRepository().

For the sake of backwards compatibility (and perhaps consistency) I would be in favour of adding a new method (such as haveInRepositoryAndReturn(), haveInRepositoryAndReturnEntity(), or even haveAndGrabInRepository()) to differentiate between the methods and give developers the option to whether they want the PK or entity returned.

@TavoNiievez
Copy link
Member Author

@Naktibalda @DavertMik

@TavoNiievez TavoNiievez transferred this issue from Codeception/module-doctrine2 Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants