test if array contains value using PHPUnit - php

I created this array of objects:
$ad_1 = new AdUnit(array('id' => '1', 'name' => 'Ad_1', 'description' => 'great ad', 'code' => 'alpha', 'widget_id' => '123'));
$ad_2 = new AdUnit(array('id' => '2', 'name' => 'Ad_2', 'description' => 'good ad', 'code' => 'delta', 'widget_id' => '456'));
$ad_3 = new AdUnit(array('id' => '3', 'name' => 'Ad_3', 'description' => 'bad ad', 'code' => 'sigma', 'widget_id' => '789'));
$adUnitArr = array($ad_1, $ad_2, $ad_3);
and i want to check that a random ad i got from a function exists in the array. the code to get the ad looks like this:
$fixture = new AdGroup();
$fixture->setAds($adUnitArr);
$randad = $fixture->getRandomAd();
now i want to check if the array contains the random ad i received, what i was able to do like this:
$this->assertEquals(in_array($randad, $adUnitArr), 1); //check if ad in array
but my question is, is there an assert or some other way to check this thing better than the way i did it?? i tried using assertArrayHasKey but i got the following error:
PHPUnit_Framework_Exception: Argument #1 (No Value) of PHPUnit_Framework_Assert::assertArrayHasKey() must be a integer or string
any idea please? thx

Try the assertContains method:
$this->assertContains( $randad, $adUnitArr );

Related

Laravel - phpunit array assertion

I am trying to write a phpunit test so I can test that I'm getting the correct assertion, this test i currently have which is passing and works as intended.
/**
#test
*/
$filmInfo = $this->postRequest();
$this->assertFilm($this->requestData, $filmInfo['id']);
$film = Film::findOrFail($filmInfo['id']);
$this->assertEquals('LOTR', $filmInfo['name']);
$this->assertEquals('Film about a ring', $filmInfo['description']);
$this->assertEquals('Fantasy', $filmInfo['genre']['main']);
$this->assertEquals('Adventure', $filmInfo['genre']['sub']);
}
This is the request data array it is referring to:
private function requestData(): array
{
return [
'name' => 'LOTR',
'description' => 'Film about a ring',
'main' => 'Fantasy',
'sub' => 'Adventure',
];
}
This test works fine and it passing but I want to test it within one assertion like so:
$this->assertEquals([
'name' => 'LOTR',
'description' => 'Film about a ring',
'genre' => [
'main' => 'Fantasy',
'sub' => 'Adventure'
]
,
], $filmInfo);
But I keep getting an error that the 2 arrays I'm asserting are not matching, do you guys have an idea on what could be causing this?
Just like what the PHPUnit said, your array aren't matching. Your array should match with all values in $filmInfo array.
From your code, we can guessing that you aren't comparing the id. Maybe you can try this code:
$this->assertEquals(array_merge($filmInfo, [
'name' => 'LOTR',
'description' => 'Film about a ring',
'genre' => [
'main' => 'Fantasy',
'sub' => 'Adventure'
],
]), $filmInfo);

Dot syntax and nested html forms. Unable to fetch data during testing

I am new to the Laravel. I have not tried this in browser I am using tests for this.
I have test that looks like this:
public function testStoreRequestValid()
{
$data = [
'name' => 'New Item',
'parameter.count' => '3',
'parameter.0.parameter_id' => '4',
'parameter.0.value_id' => '',
'parameter.0.value' => 'text',
'parameter.1.parameter_id' => '1',
'parameter.1.value_id' => '2',
'parameter.1.value' => 'Yes',
'parameter.2.parameter_id' => '2',
'parameter.2.value_id' => '',
'parameter.2.value' => '10'
];
$response = $this->call('post', '/item', $data);
// ...
}
When I try to fetch data using:
$parameterCount = $request->input('parameter.count');
I get null value (if I use has method it gets false).
When I have tried to use square brackets I have got the results, but I want the API to be usable with JSON too without too much hastle in the JS.
How do I solve this in the most clean way?
Dot in Laravel has a special meaning so you can't really use arrays which have dots in their keys. You should declare your data as follows:
<?php
$data = [];
array_set($data,'name','New Item');
array_set($data,'parameter.count','3');
array_set($data,'parameter.0.parameter_id','4');
array_set($data,'parameter.0.value_id','');
array_set($data,'parameter.0.value','text');
array_set($data,'parameter.1.parameter_id','1');
array_set($data,'parameter.1.value_id','2');
array_set($data,'parameter.1.value','Yes');
array_set($data,'parameter.2.parameter_id','2');
array_set($data,'parameter.2.value_id','');
array_set($data,'parameter.2.value','10');

QUICKBASE PHP SDK add_record returns blank response

I was recently brought on by a company that uses Quickbase. They have limited systems in place to talk to the Quickbase application, so I am trying to incorporate the PHP SDK in order to add/edit records in Quickbase using a front end designed by myself so customers can submit a form from the web into Quickbase.
I'm coming across a problem right away trying to get the SDK to even respond with something other than an error. Currently it doesn't respond with anything when trying to add a record.
I keep reading that a recent (~2-3 years ago) change has caused this to be a bit difficult to use.
below is my codesnippet from a php page called "addnewcustomer.php"
include_once('quickbase.php');
//my PHP SDK Options located inside quickbase.php
var $user_name = 'username'; // QuickBase user who will access the QuickBase
var $passwd = 'pw'; // Password of this user
var $db_id = 'dbid'; // Table/Database ID of the QuickBase being accessed
var $app_token = 'my app token';
var $xml = true;
var $user_id = '';
var $qb_site = "www.mycompany.quickbase.com";
var $qb_ssl = "https://www.mycompany.quickbase.com/db/";
var $ticketHours = 12;
$quickbase = new QuickBase('myusername', 'mypw', true, "dbid", "token", "realm", hour);
$fields = array(
array(
'fid' => '148',
'value' => $agentid), //agentid
array(
'fid' => '15',
'value' => $city), //city
array(
'fid' => '16',
'value' => $state), //state
array(
'fid' => '14',
'value' => $address), //address
array(
'fid' => '524',
'value' => $apt), //apt #
array(
'fid' => '17',
'value' => $zip), //zip code
array(
'fid' => '33',
'value' => $rentown), //rent/own
array(
'fid' => '28',
'value' => $first), //first name
array(
'fid' => '29',
'value' => $last), //last name
array(
'fid' => '21',
'value' => $email), //email
array(
'fid' => '18',
'value' => $phone) //phone
);
$quickbase->add_record($fields);
It currently responds with nothing, ie. blank response. If I change the realm with something inaccurate I get an error of "Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML'", which makes me think I have everything setup correctly.
What should a successful entry return? What am I doing wrong?
The add_record method should be returning an XML object. If you capture that object, you can check it to see if the API call was successful or if an error was returned. So, if you change your code to:
$results = $quickbase->add_record($fields);
print_r($results);
You'll get something you can understand. It's inelegant, but it will show you quickly if you're getting a response and if that response is an error. You should see something like this on success:
SimpleXMLElement Object ( [action] => API_AddRecord [errcode] => 0 [errtext] => No error [rid] => 81 [update_id] => 1436476140453 )

Troubleshooting dynamic dropdowns in SugarCRM 6.5

I had asked a question here a while back about setting up database populated dropdowns for SugarCRM. I received a really good answer and, after more php studies and a dev instance running, I decided to give it a shot. The instructions I followed can be found here. After I run the repair and rebuild, I would expect to see the custom field in my Fields list under the module in studio, but have not been able to find it. The module is named Makers (a1_makers as a database table). For good orders sake, there were no errors when I repaired/rebuilt after saving the files. Per the instructions, I first created a php file with a custom function to query the database (custom/Extension/application/Ext/Utils/getMakers.php):
<?php
function getMakers() {
static $makers = null;
if (!$makers){
global $db;
$query = "SELECT id, name FROM a1_maker";
$result = $db->query($query, false);
$accounts = array();
$accounts[''] = '';
while (($row = $db->fetchByAssoc($result)) !=null) {
$accounts[$row['id']] = $row['name'];
}
}
return $makers;
}
?>
Then, I set 'function' field in Vardefs to point to the function (custom/Extension/modules/Maker/Ext/Vardefs/makers_template.php):
<?php
$dictionary['Maker']['fields']['list_of_makers'] = array (
'name' => 'list_of_makers',
'vname' => 'LBL_MKRLST'
'function' => 'getMakers',
'type' => 'enum',
'len' => '100',
'comment' => 'List of makers populated from the database',
);
?>
Unfortunately, there are no errors and the repair/rebuild runs fine. I am just unable to see the custom field when I go into studio. Can anyone please help point out what I may be doing wrong?
I would recommend checking existence of newly created field 'list_of_makers' in cache/modules/Maker/Makervardefs.php file. If new field definition exists in that file, try add 'studio' => 'visible' to custom/Extension/modules/Maker/Ext/Vardefs/makers_template.php to get something like this:
<?php
$dictionary['Maker']['fields']['list_of_makers'] = array (
'name' => 'list_of_makers',
'vname' => 'LBL_MKRLST'
'function' => 'getMakers',
'type' => 'enum',
'studio' => 'visible'
'len' => '100',
'comment' => 'List of makers populated from the database',
);
Try to edit your custom/modules/Maker/metadata/editviewdefs.php manually and insert field definition by hand in proper place if everything above won't work.
$dictionary['Maker']['fields']['list_of_makers'] = array (
'name' => 'list_of_makers',
'vname' => 'LBL_MKRLST'
'function' => 'getMakers',
'type' => 'enum',
'studio' => 'visible'
'len' => '100',
'comment' => 'List of makers populated from the database',
'studio' => array(
'listview' => true,
'detailview' => true,
'editview' => true
),
);

Converting undefined indexes to null in PHP

I'm not sure if the title of this question is necessarily the accurate description of what I need to do, but I'll go ahead and ask my question and see what everyone thinks...
Basically, I am receiving data from a source that I have no control over, and I need to transpose it into a suitable format for inserting into my database using CakePHP. So, here's how I'm doing it:
public function submitApp($data) {
$array = array(
'Student' => array(
'name' => $data['name'],
'email' => $data['email'],
'phone' => $data['phone'],
'address' => $data['address'],
'dob' => $data['dob'],
'gender' => $data['gender']
),
'Application' => array(
'course_id' => $data['course_id'],
'question1' => $data['question1'],
'question2' => $data['question2'],
'question3' => $data['question3'],
'question4' => $data['question4'],
),
'ApplicationQualification' => $data['Qualifications']
);
// Logic to save $array goes here
}
The problem is that sometimes not all of the keys in $data will be submitted to my app but I still want my app to work with what it gets.
I know that I can wrap each key in a conditional like this:
if (!isset($data['name'])) { $data['name'] = null; }
...and then building the array, but this seems like a pretty clumsy way of doing it. Is there a more efficient way to do this?
You could use a simple ternary statement
'name' => array_key_exists('name', $data) ? $data['name'] : null
Alternatively, you can set up a default array and then merge the given values in
$defaults = [
'name' => null,
'email' => null,
// etc
];
$data = array_merge($defaults, $data);

Categories