Symfony 4 FOSRestBundle boolean always wrong - fosrestbundle

When I want to update my database I use a REST service using the patch verb.
Example:
http://localhost:8000/users/1?validate=false
False is never savd but against true is save.
Is my FOSRestBundle wrongly configured?
Here is the configuration of my FOSRestBundle :
Configuration FOSRestBundle
Do you have a solution ?

It's because 'false' is not false
Example :
<?php
var_dump('false' == false); // false
var_dump('false' == true); // true

Related

Add a custom query parameter in Feathers request

I'm hoping to add a custom query parameter that can be used in a hook to process some results.
I'd like to add $foo=bar to the request. I can whitelist $foo and it appears in the request, but the service is using an ORM so it tries to query the database for $foo=bar.
In a before hook, I can strip $foo from the params.query, but is there somewhere else in the context of the hook that I could stash the value of $foo so that I can act on it in an after hook?
I used to delete the params once the functionality is over here we added customer variable $paginate in before find hook.
find: [
(hook) => {
if (hook.params.query && hook.params.query.$paginate) {
hook.params.paginate =
hook.params.query.$paginate === 'false' ||
hook.params.query.$paginate === false;
delete hook.params.query.$paginate;
}
return hook;
}
],

How do I call `grabFixture()` method from within `ApiTester instance in Yii2?

I am building an API in Yii2 2.0.14 and running tests with Codeception. Examples in the tutorial* show that I can call fixtures like so:
$profile = $I->grabFixture('profiles', 'user1');
However this doesn't seem to be available in my test class here:
<?php
namespace frontend\tests\api;
use frontend\tests\ApiTester;
class DemoCest
{
public function _fixtures()
{
return [
'users' => [
'class' => UserFixture::className(),
// fixture data located in tests/_data/user.php
'dataFile' => codecept_data_dir() . 'user.php'
],
];
}
public function demo(ApiTester $I)
{
$users = $I->grabFixture('users');
$I->wantTo('perform actions and see result');
$I->haveHttpHeader('Content-Type', 'application/x-www-form-urlencoded');
$I->sendPOST('/user/test', ['name' => 'davert', 'email' => 'davert#codeception.com']);
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK); // 200
$I->seeResponseIsJson();
$I->seeResponseContainsJson(['message' => 'test OK']);
}
}
I have seen that you need to add fixtures to frontend/tests/api-suite.yml but Codeception is throwing an exception when I do that
PHP Notice: Undefined index: tests in
/var/www/vendor/codeception/base/src/Codeception/Command/Run.php on
line 389
I am completely lost.
actor: ApiTester
modules:
enabled:
- Yii2:
part: [orm, email, fixtures]
- \frontend\tests\Helper\Api
- REST:
url: http://securedata.test/api/v1
depends: PhpBrowser
part: Json
Can anyone lead me in the right direction?
tutorial - http://www.yiiframework.com/doc-2.0/guide-test-fixtures.html
Add to your suite configuration, you haven't defined the tests path in the codeception.yml under paths.
If you look into the line the error points to it will take you to the line inside the matchSingleTest function
in your case
list(, $suite, $test) = $this->matchTestFromFilename($suite, $config['paths']['tests']);
in which it gives tests as undefined index for the $config['paths'] array, which if you backtrack parses the codeception.yml to the $config,
You are using an older version of Yii 2.0.14 which may have the missing section from the execute() see this ISSUE
So add tests directory as . and make sure your codeception.yml is in the /tests root.
see the following
paths:
tests: .

How to change the field delete instead of id in loopback?

I want to the delete a few rows in MySQL table using loopabck. But I don't want to use the id to delete the record. I'm trying it in Angular SDK. My code is:
ModelName.destroyById({ fieldName : myValue })
.$promise
.then(function() {
});
I'd be very thankful for ideas.
Seems like you need to use the destroyAll method, which does what you want, but you need to set up remoting for it first, since it's disabled by default:
ModelName.remoteMethod('destroyAll', {
isStatic: true,
description: 'Delete all matching records',
accessType: 'WRITE',
accepts: {arg: 'where', type: 'object', description: 'filter.where object'},
http: {verb: 'del', path: '/'}
});
Then after that you regenerate your Angular client and you can use it like this in the client side:
ModelName.destroyAll({ filter: { fieldName : myValue }}).$promise.then(...)
Note that using destroyAll is risky as if you have any mistakes in your code you will end up losing data. Enabling it to client side might be security concern.
Another way to do this is to write your own remote method which uses destroyAll in turn with proper validation of the inputs.

Laravel + GuzzleHTTP no response data?

I'm querying my API using guzzle.
$client = new Client();
$response = $client->post('xxxxxx',
array(
'headers' => array('Content-Type'=>'application/json'),
'json'=> array(
"type" => 0,
[...]
)
)
);
//$response = json_decode($response);
dd($response->getBody());
This should output me something like this:
{
"returnCode": 0,
"success": true
}
but instead I get something else.
Stream {#230 ▼
-stream: stream resource #12 ▼
wrapper_type: "PHP"
stream_type: "TEMP"
mode: "w+b"
unread_bytes: 0
seekable: true
uri: "php://temp"
options: []
}
-size: null
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
Can anyonee help me or tell me what i did wrong? I want to send raw post data and also get raw post data afterwards I then want to save (for example in a db).
getBody()
returns a streaminterface. You want to get the content of that stream, so:
$response->getBody()->getContents();
is what you're searching for
Note
According to the docs, you can also cast the streaminterface to a string, but I've had varying results. getContents() always does what it needs to do, so that's my go to solution.

Rails: how do I test a helper method that executes a mysql query

I'm having a method in a helper, that determines whether a user has a specific flag (enabled), based on tags and a code, stored in a cookie.
def user_enabled?
Code.joins({tags: :user}).
where(code: cookies[:user_code]).
where(users: {enabled: true})
exists?
end
Basically, a tag is associated with a user and a code with a tag.
I'm not really sure how to recreate that in my specs, so that the query executes all the records required. So far I have:
it 'returns true when enabled is checked' do
tag = create(:tag, user: create(:user, enabled: true))
Code.create(code: 'enabled', tags: [tag])
cookies[:user_code] = 'enabled'
expect(user_enabled?).to be_truthy
end
But that returns false and I'm not really sure what I'm missing.
Here's what I did to fix this:
Context 'some context'
let!(:tag) { create(:tag, user: create(:user)) }
let!(:code) { create(:code, code: 'enabled', tags: [tag]) }
it 'returns true when enabled is checked' do
cookies[:user_code] = 'enabled'
expect(user_enabled?).to be_truthy
end
end