I trying to use function ioncube_license_properties() to get encoded property at my project. But I have a trouble - function return false at class method and return actual value at procedure file.
I have next code. Class:
class User {
/**
* Check is can login with new user.
* #return bool
*/
public static function validateLicense()
{
if (function_exists('ioncube_license_properties')) {
$count = ioncube_license_properties()['allowedUserCount']['value']; // ioncube_license_properties() returns false there
if ($count === 10) {
//info($message . 'User can login.');
} else {
//warning($message . 'Access denied for.');
}
return $isCanLogin;
} else {
//warning('Can\'t find Ioncube function `ioncube_license_properties()`.');
return true;
}
}
}
simple php file (it's a view file on MVC model):
if(function_exists('ioncube_license_properties')) {
var_dump(['validateLicense' => User::validateLicense()]); // false always
var_dump(['$allowedUserCount' => $allowedUserCount = ioncube_license_properties()['allowedUserCount']['value']]); // returns actual value int(10)
}
Why function returns incorrect value at the class method and how to fix this?
Project use Yii2.
I am trying to write unit tests for practice on a small application. This is my first time with unit-tests and I have not a very good knowledge. This is what I am trying to do.
$response=$this->request('POST',['crud_controller','add_data' ,$data]);
$this->assertEquals(1,$response);
The function add_data is programmed to return 1 if the data is processed or return -1 otherwise. The data is getting processed but phpUnit shows failed test with
issue:
Failed asserting that null matches expected 1.
I am working in codeigniter and using phpunit for testing.
Complete Test-Case:
<?php
class crud_controller_test extends TestCase{
public function test_add_data(){
$data =array(
'name' => 'badPass',
);
$response=$this->request('POST',['crud_controller','add_data' ,$data]);
$this->assertEquals(1,$response);
//$this->assertEquals(-1,$response);
//$this->assertEquals(0,$response);
}
public function test_update_data(){
$data =array(
'id'=>'29',
'name' => 'badPass',
);
$response=$this->request('POST',['crud_controller','update_data' ,$data]);
var_dump($response);
$this->assertEquals(1,$response);
//$this->assertEquals(-1,$response);
//$this->assertEquals(0,$response);
}
}
?>
Methods getting called:
public function add_data($data){
if($this->crud_model->insert($data) ==true)
return 1;
else
return -1;
//return $this->crud_model->insert($data);
//$this->index();
}
public function update_data($data){
if($this->crud_model->update($data) == true)
return 1;
else
return -1;
//$this->index();
}
First off I know the developer stopped working on this extension full time, but from what I understand there are still alot of people using it. Hoping someone can help me with this (hopefully simple problem!)
I'm trying to read the data I just inserted into MongoDB using the YiiMongoDbSuite extension.
The data is successfully inserted as I can see it via the mongo console.
The problem is when I try to retrieve it. Here's the code I'm running together - it inserts it (no problem), but can't retrieve it:
<?php
$new = new Character();
$new->playerName = "Yii-Insert-Test";
$new->playerId = "123456789";
$new->playerScore = "9001";
$new->save();
$findAll = Character::model()->findAll();
foreach($findAll as $result) {
echo $result->playerName; // result should be "Yii-Insert-Test", instead it's NULL
}
?>
It does get inserted though:
{
"_id" : ObjectId("54a0deda60fc21843100002a"),
"playerName" : "Yii-Insert-Test",
"playerId" : "123456789",
"playerScore" : "9001"
}
And here's my very simple model Character.php :
<?php
class Character extends EMongoDocument
{
public $_id;
public $playerName;
public $playerId;
public $playerScore;
public function getCollectionName()
{
return 'usercollection';
}
public function rules()
{
return array(
array('playerName', 'required'),
);
}
public function attributeNames()
{
return array(
'playerName' => 'Character Name',
'playerId' => 'Character ID',
'playerScore' => 'Character Score',
);
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
I feel like it's right in front of me but can't see what the problem is.
edit:
slowly figuring it out - looks like I am receiving data, but it's all empty - this is the mongoDB cursor. I'm already doing a forloop to iterate it, so I'll have to figure out what else I'm not doing.
I'm trying to learn how to do unit testing in general but specifically the project I'm working on is built with CakePHP. I have this parentNode() method in my user model taken directly from the Simple Acl Controlled Application tutorial.
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['User']['group_id'])) {
$groupId = $this->data['User']['group_id'];
} else {
$groupId = $this->field('group_id');
}
if (!$groupId) {
return null; // not tested
} else {
return array('Group' => array('id' => $groupId));
}
}
I wrote the following tests
public function testParentNodeHasNoUserDataOrId() {
unset($this->User->id);
unset($this->User->data);
$this->assertNull($this->User->parentNode());
}
public function testParentNodeWithGroupIDInUserData() {
$this->User->data['User']['group_id'] = 1;
$this->assertEquals(array('Group'=>array('id'=>1)),$this->User->parentNode());
}
public function testParentNodeWithoutGroupIDInUserData() {
$this->User->id = 1;
unset($this->User->data['User']['group_id']);
$this->assertEquals(array('Group'=>array('id'=>1)), $this->User->parentNode());
}
and they all seem to work. My code coverage report however shows that I'm not testing the return null; in the if(!$groupId) block. I can't figure out how to test that line.
As far as I can tell it will never execute. If my User model has no id and no data it returns null in the first if block. if I cheat a bit and give the user some fake data $this->field('group_id') is still returning group 1 (I think it should return false instead but it doesn't)
So when unit testing do you have to test everything? How could I test for the return null if(!$groupId)? If there's code that will never execute should I just remove it?
Thanks!
I figured out how I could test for the "impossible" scenario with the following test
public function testParentNodeWhenUserHasNoGroupId() {
$this->User->id = 1;
$user = $this->User->read();
$user['User']['group_id'] = 0;
$this->User->save($user);
$this->assertNull($this->User->parentNode());
}
I got an error while running my code, it says call to a member function getBallparkDetailsStartDate() on a non-object.
if($projectStatusId == ProjectStatusKeys::BALLPARK_ACTIVE) {
$ballpark = $this->ballparkDetailsHandler->getBallparkDetailsByProjectId($projectId);
$projectDetails["startdate"] = $ballpark->getBallparkDetailsStartDate();
$projectDetails["enddate"] = $ballpark->getBallparkDetailsEndDate();
$projectDetails["projectid"] = $projectId;
$projectDetails["name"] = $ballpark->getBallparkDetailsBookingRef();
$projectDetails["status"] = ProjectStatusKeys::BALLPARK_ACTIVE;
}
I got the error in this line: $projectDetails["startdate"] = $ballpark->getBallparkDetailsStartDate();
Here is my other code:
public function __construct($ballparkDetailsId, $project,
$ballparkDetailsBookingRef,
$ballparkDetailsStartDate, $ballparkDetailsEndDate,
$ballparkDetailsExpiryDate, $ballparkDetailsDescription,
$ballparkDetailsNotes) {
$this->ballparkDetailsId = $ballparkDetailsId;
$this->project = $project;
$this->ballparkDetailsBookingRef = $ballparkDetailsBookingRef;
$this->ballparkDetailsStartDate = $ballparkDetailsStartDate;
$this->ballparkDetailsEndDate = $ballparkDetailsEndDate;
$this->ballparkDetailsExpiryDate = $ballparkDetailsExpiryDate;
$this->ballparkDetailsDescription = $ballparkDetailsDescription;
$this->ballparkDetailsNotes = $ballparkDetailsNotes;
}
public function getBallparkDetailsId() {
return $this->ballparkDetailsId;
}
public function getProject() {
return $this->project;
}
public function getBankName() {
return $this->getProject()->getBankName();
}
public function getBankRef() {
return $this->getProject()->getBankRef();
}
public function getRegionName() {
return $this->getProject()->getRegionName();
}
public function getProjectStatusName() {
return $this->getProject()->getProjectStatusName();
}
public function getBallparkDetailsBookingRef() {
return $this->ballparkDetailsBookingRef;
}
public function getBallparkDetailsStartDate() {
return $this->ballparkDetailsStartDate;
}
public function getBallparkDetailsEndDate() {
return $this->ballparkDetailsEndDate;
}
public function getBallparkDetailsExpiryDate() {
return $this->ballparkDetailsExpiryDate;
}
public function getBallparkDetailsDescription() {
return $this->ballparkDetailsDescription;
}
public function getBallparkDetailsNotes() {
return $this->ballparkDetailsNotes;
}
public function getProjectId() {
return $this->getProject()->getProjectId();
}
public function getProjectStatusId() {
return $this->getProject()->getProjectStatusId();
}
}
?>
The last time I check this it ran well. But now I don't know what's wrong with this? Please help me find the error. Thanks.
Apparently
$ballpark = $this->ballparkDetailsHandler->getBallparkDetailsByProjectId($projectId);
is not returning a "ballpark" at all. Probably it is returning an error, or something like an empty array.
Try var_dump()'ing $ballpark immediately before the line that raises the error, and see what it contains (probably False, NULL, array() or something equally un-ballparky.
Then, inspect the ballparkDetailsByProjectId() function in the BallparkDetailsHandler.php file. At a guess, you might be passing an invalid (i.e. nonexistent, removed, etc.) $projectId.
Then you might rewrite the code with error checking:
if($projectStatusId == ProjectStatusKeys::BALLPARK_ACTIVE) {
$ballpark = $this->ballparkDetailsHandler->getBallparkDetailsByProjectId($projectId);
if (!is_object($ballpark))
trigger_error("Error: bad project ID: '$projectId': $ballpark",
E_USER_ERROR);
$projectDetails["startdate"] = $ballpark->getBallparkDetailsStartDate();
$projectDetails["enddate"] = $ballpark->getBallparkDetailsEndDate();
$projectDetails["projectid"] = $projectId;
$projectDetails["name"] = $ballpark->getBallparkDetailsBookingRef();
$projectDetails["status"] = ProjectStatusKeys::BALLPARK_ACTIVE;
}
Then in the BallparkDetailsHandler.php file you could modify this code:
// Prepare query or die
if (!($stmt = $this->mysqli->prepare($query))
return "Error in PREPARE: $query";
$stmt->bind_param("i", $projectId);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($ballparkDetailsBookingRef, $bankRef, $regionName,
$projectStatusId, $projectStatusName, $ballparkDetailsDescription,
$ballparkDetailsNotes, $ballparkDetailsStartDate, $ballparkDetailsEndDate,
$ballparkDetailsExpiryDate);
$stmt->fetch();
// If no data, then die
if(!$stmt->num_rows)
return "No data in DB for projectID '$projectId': $query";
// Should be clear sailing from here on. Actually I ought to check
// whether all these new() here do return anything sensible, or not
$bank = new Bank("", "", $bankRef, "");
$region = new Region("", $regionName, "");
$projectStatus = new ProjectStatus($projectStatusId, $projectStatusName);
$project = new Project($projectId, $bank, $region, $projectStatus);
return new BallparkDetails("", $project,
$ballparkDetailsBookingRef, $ballparkDetailsStartDate,
$ballparkDetailsEndDate, $ballparkDetailsExpiryDate,
$ballparkDetailsDescription, $ballparkDetailsNotes);
$ballpark clearly doesn't contain the object you think it does on the line with the error. In fact, it obviously doesn't contain an object at all.
This implies that the preceding line (which sets $ballpark) isn't working properly. It would appear that it's returning a value that is is not an object.
I can't tell what that value is -- it could be null, or it could be an integer, string, array, etc. But whatever it is, it isn't a ballpark object.
I suggest you look at your getBallparkDetailsByProjectId() method to find the source of this problem.