Comments (7)
Seems now I understand. Array non-associative keys shouldn't exist in json, right?
from pushok.
I think you are right.
I added JSON_FORCE_OBJECT
for passing tests. It is useful when the recipient of the output is expecting an object and the array is empty. It will not throw an exception.
So you can make a PR if you wish.
from pushok.
I just updated the test (setCustomValue('array', [1,2,3])
) and it passed (without your PR):
public function testConvertToJSon()
{
$alert = Alert::create()->setTitle('title');
$payload = Payload::create()
->setAlert($alert)
->setBadge(1)
->setSound('sound')
->setCategory('category')
->setThreadId('tread-id')
->setContentAvailability(true)
->setMutableContent(true)
->setCustomValue('key', 'value')
->setCustomValue('array', [1,2,3]);
$this->assertJsonStringEqualsJsonString(
'{"aps": {"alert": {"title": "title"}, "badge": 1, "sound": "sound", "category": "category", ' .
' "thread-id": "tread-id", "mutable-content": 1, "content-available": 1}, "key": "value", "array": [1,2,3]}',
$payload->toJson()
);
}
Can you please give an example of Payload data which doesn't work as expected?
from pushok.
It's because assertJsonStringEqualsJsonString
doesn't compare properly the JSON string is this case. (I might create an issue on PHPUnit but each thing in time...).
Just var_dump($payload->toJson())
and you will see what i'm talking about.
The only way to properly test this case it to json_decode($payload->toJson())
and test the type of the array
value like i did in my PR.
Add this and you will see your test breaking:
$decoded_json = json_decode($payload->toJson());
$this->assertEquals(gettype($decoded_json->custom_array), 'array');
from pushok.
If you want json_decode
returns an array you should pass second parameter true
.
This will pass:
$decoded_json = json_decode($payload->toJson(), true);
$this->assertEquals(gettype($decoded_json['custom_array']), 'array');
from pushok.
Yeah but why passing this ? It's not good. This 2nd parameter does : When TRUE, returned objects will be converted into associative arrays. (aka the exact opposite of JSON_FORCE_OBJECT). So ok it works for the test but the lib is still outputing {0: 1, 1:2, 2:3}
when i want [1,2,3]
.
Also look at my PR request, i feel you used JSON_FORCE_OBJECT
just to make the aps key always an object even when empty. To me this is bad pratice as the aps
key should have been a PHP Object from the start. Not an array.
from pushok.
Thanks, now it is clear
from pushok.
Related Issues (20)
- VoIP pem certificates don't work HOT 1
- Target [Pushok\AuthProviderInterface] is not instantiable while building [Pushok\Client]
- BadDeviceTokenThe specified device token was bad. Verify that the request contains a valid token and that the token matches the environment HOT 3
- Support PHP 8
- Uncaught Exception HOT 3
- Package spomky-labs/jose is abandoned, you should avoid using it. Use web-token/jwt-framework instead. HOT 1
- Deprecation notices with PHP 8.1 HOT 4
- Question: Push Notifications for PkPass Updates HOT 8
- send a notification on a chosen date HOT 1
- Updating a badge HOT 1
- Support for creating WeatherKit JWT tokens
- The `Alert` option is set in the code but there is no Alert on the phone
- Package fgrosse/phpasn1 is abandoned HOT 2
- Started getting InvalidArgumentException "Unable to load the key." HOT 4
- Live Activities Supported? HOT 3
- Laravel Vapor - HTTP/2 stream 15 was not closed cleanly before end of the underlying stream HOT 3
- 0.15.x is no longer compatible with PHP 8.0 HOT 4
- HTTP/2 stream was not closed cleanly: REFUSED STREAM HOT 9
- Sending `Alert` as `Background` due to `content-available` set HOT 1
- php 8.0 via command package not installed
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pushok.