Chapters
37 Chapters
|
3:43:31
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-iconv": "*",
"api-platform/core": "^3.0", // v3.1.2
"doctrine/annotations": "^2.0", // 2.0.1
"doctrine/doctrine-bundle": "^2.8", // 2.8.3
"doctrine/doctrine-migrations-bundle": "^3.2", // 3.2.2
"doctrine/orm": "^2.14", // 2.14.1
"nelmio/cors-bundle": "^2.2", // 2.2.0
"nesbot/carbon": "^2.64", // 2.66.0
"phpdocumentor/reflection-docblock": "^5.3", // 5.3.0
"phpstan/phpdoc-parser": "^1.15", // 1.16.1
"symfony/asset": "6.2.*", // v6.2.5
"symfony/console": "6.2.*", // v6.2.5
"symfony/dotenv": "6.2.*", // v6.2.5
"symfony/expression-language": "6.2.*", // v6.2.5
"symfony/flex": "^2", // v2.2.4
"symfony/framework-bundle": "6.2.*", // v6.2.5
"symfony/property-access": "6.2.*", // v6.2.5
"symfony/property-info": "6.2.*", // v6.2.5
"symfony/runtime": "6.2.*", // v6.2.5
"symfony/security-bundle": "6.2.*", // v6.2.6
"symfony/serializer": "6.2.*", // v6.2.5
"symfony/twig-bundle": "6.2.*", // v6.2.5
"symfony/ux-react": "^2.6", // v2.7.1
"symfony/ux-vue": "^2.7", // v2.7.1
"symfony/validator": "6.2.*", // v6.2.5
"symfony/webpack-encore-bundle": "^1.16", // v1.16.1
"symfony/yaml": "6.2.*" // v6.2.5
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.2
"mtdowling/jmespath.php": "^2.6", // 2.6.1
"phpunit/phpunit": "^9.5", // 9.6.3
"symfony/browser-kit": "6.2.*", // v6.2.5
"symfony/css-selector": "6.2.*", // v6.2.5
"symfony/debug-bundle": "6.2.*", // v6.2.5
"symfony/maker-bundle": "^1.48", // v1.48.0
"symfony/monolog-bundle": "^3.0", // v3.8.0
"symfony/phpunit-bridge": "^6.2", // v6.2.5
"symfony/stopwatch": "6.2.*", // v6.2.5
"symfony/web-profiler-bundle": "6.2.*", // v6.2.5
"zenstruck/browser": "^1.2", // v1.2.0
"zenstruck/foundry": "^1.26" // v1.28.0
}
}
15 Comments
On postToCreateTreasure() test:
login succeeds (status: 204).
post('/api/treasures') returns 401 instead of 422.
While the same testing with /api (swagger) post to /api/treasures returns 422 (the expected).
I do not prefix with 'test' my test methods, I am using the @test keyword in docblock:
`...
/**
There was 1 failure:
1) App\Tests\Functional\DragonTreasureResourceTest::postToCreateTreasure
Current response status code is 401, but 422 expected.
Hi @Alkiviadis-D
Hm looks like session is not working correctly between requests, or maybe login request was not fully successful... have you tried to use
->actingAs($user)shortcut to see if it works correctly?Cheers!
Okkk.. had a typo inside getRoles(). Sorry for this. Thanks for your help.
What could be the issue ?
{
I get this error,
when I use ->actingAs($user) method , as I'm experimenting on new Symfony 7project, but seems working when using ->login()
Hey @sujal_k ,
Does your User entity have $id field? Does that field has the same mapping as in this course code? i.e. it should be :
Also, make sure you don't have an invalid Doctrine mapping by calling this command:
Ad make sure both mapping and DB are OK.
I hope this helps!
Cheers!
I'm getting the exact same error.
Started my project from latest ApiPlatform Distribution (AP 3.4, Symfony 6.4). When I log in making POST to json_login path I'm able to fetch resources which require full authetication, but when I swap login process with
->actingAs($user)I get 500 error described above while trying to fetch any resource.I checked my schema as recommended above (everyting is ok) and my User entity is rather standard (created by make:user). It's id is on place.
Do you have any other ideas what can be wrong here? :(
Hey Kriss,
Hm, difficult to say without further debugging, probably try to find the place where that error is thrown and before that exception try to dump the actual user to make sure it really has an ID.
Also, if you’re customizing serialization groups, ensure that the identifier field is included in the serialization group used when the API Platform serializes the user.
Cheers!
Hey Victor,
I performed some further debuging and I discovered that when I use $entityManager to create my $user object like so:
then I'm am able to use
$browser->actingAs($user)normaly. Also when I get type of created entity usinggetMetatadaFormethod I getApp\Entity\User.When I create
$userusingUserFactoryand try to get object's type using the same method I get an error:Looks like there is something wrong with doctrine mapping? Or maybe It's something wrong with UserFactory configuration?
I used ApiPlatform distribution as a starter, created Facotry with
make:facotryand didn't mess with doctrine mappings at all, so why would they be wrong... Do you have any idea? :)Hey Kriss,
Ah, good catch! Yeah, that's actually on purpose, entity factories from the Foundry lib need that to add more magic in tests - those proxy objects helps you to do not worry about the problem with refreshing the entity in tests. But that AppEntityUserProxy actually extends your User class, so
instanceof Userstill should work.In short, nothing is wrong with this behavior, that's how it's suppose to work in Foundry, you just need to keep this in mind. If it bothers you or causing some issues - you can create objects manually bypassing creating it via Foundry factories.
P.S. Sorry for the long reply, I somehow missed this ticket 🙃
Cheers!
Hey Victor,
thanks for your explanation, however it seems that this does not resolve the issue this thread is about.
Namely: why in the tutorial Ryan was able to use
browser->actingAs($user)with$usercreated with Foundry factory and when I try to do this I get the 500 error mentioned by @sujal_k at the beginning of this thread? And why am I able to use this method when creating user using entity manager?Do you think it can have something to do with the fact I stared my project from AP Distribution, not from cli installation?
Hey @Kriss_G!
Can you tell me what version of
zenstruck/browser&zenstruck/foundryyou are using?(I'm thinking you may be on foundry 2+ but your browser version does not support it yet - I'm hoping upgrading
zenstruck/browserwill fix)--Kevin
Hey Kevin @kbond!,
Indeed, I used foundry v2.1.0 and browser v1.9.0.
I ran
composer update zenstruck/browser, which brought my browser version to 1.9.1 and it seems to solve the issue.I can now use
browser->actingAs($user)with$usergenerated withUserFactoryclass!Thank you for your help! :)
Kriss
Awesome, great to hear!
The authentication fails while testing at: 2:33 timestamp, in the video it passes, but in my dummy Symfony 7 project, same code failed, always says 401 as response code,
This is the error I get in response when I dump the
/loginrequest in test,below is the code:
Hey @sujal_k!
These invalid credentials can be tricky as you can't really see what's going on since the passwords are all hashed. And I don't see any obvious problems. Triple check that the password IS being hashed before saving to the DB. You could also temporarily change the hashing algo to plaintext. It may help you see the underlying problem.
Cheers!
"Houston: no signs of life"
Start the conversation!