PHPunit Global Variable returns NULL












1














I'm new to php Unit test, but I could installed it and make it work (PHP 7.1.2 / phpUnit 6.1.1 on a Mac / MAMP system). But I now I can't let it test my app. Probably due to a strange class architectural hierarchy.



In my autoload.php I load my classes like that:



/* get timing functions */
$th = new TimingHelper();

/* Get basic functions */
$basic = new basic();

/* Get App data (param: fake localhost true / false) */
$App = new App();

/* Start DB Connection */
$db = new db(0);

/* Get usr data (param: fake user mode true / false, user id) */
$usr = new User();


There are even more classes... ;)



Now inside the User Class I get the previous classes like that:



class User {
/* get DB connection */
private $db, $App, $tr, $th, $basic;

public function __construct() {

/* get DB connection */
Global $db, $App, $tr, $th, $basic;
$this->db = $db;
$this->App = $App;
$this->tr = $tr;
$this->th = $th;
$this->basic = $basic;

/* Start timing */
$this->th->start();
}
}


That's probably not way one should do it, but it works ;)



But now if I want to use phpUnit, I heard it ignores global objects, so



 var_dump($this->th);


is NULL.



Is there no way, that I can use phpUnit with my class system? If not, what do I need to change?



I tried to extend each class, but then my app was SUPER slow...










share|improve this question
























  • You should pass all of your global variables to as constructor arguments - then you won't need global variables any more
    – Robbie Averill
    Nov 12 at 19:59










  • Hi Robbie, yes that could work. But it's also not so nice to have so many arguments then... No other idea?
    – B. Rentrug
    Nov 12 at 21:22










  • Arguably it's nicer than using global variables in OOP. You're already setting all of the global arguments to class properties in your constructor, so why not allow them to be passed to the constructor instead? You could make them nullable if you have cases where you don't always want/need to pass them
    – Robbie Averill
    Nov 13 at 9:22










  • Yes, I see your point. But now I have in my test file this: $usr = new User($db); PhpUnit gives me an error: Undefined variable: db. But I load in the phpunit.xml file my autloader.php, were i defined $db. <phpunit bootstrap="inc/autoload.php"> Why is this not working?
    – B. Rentrug
    Nov 13 at 14:58










  • It works now! I needed to add Global $usr; in the testfile!
    – B. Rentrug
    Nov 13 at 16:45
















1














I'm new to php Unit test, but I could installed it and make it work (PHP 7.1.2 / phpUnit 6.1.1 on a Mac / MAMP system). But I now I can't let it test my app. Probably due to a strange class architectural hierarchy.



In my autoload.php I load my classes like that:



/* get timing functions */
$th = new TimingHelper();

/* Get basic functions */
$basic = new basic();

/* Get App data (param: fake localhost true / false) */
$App = new App();

/* Start DB Connection */
$db = new db(0);

/* Get usr data (param: fake user mode true / false, user id) */
$usr = new User();


There are even more classes... ;)



Now inside the User Class I get the previous classes like that:



class User {
/* get DB connection */
private $db, $App, $tr, $th, $basic;

public function __construct() {

/* get DB connection */
Global $db, $App, $tr, $th, $basic;
$this->db = $db;
$this->App = $App;
$this->tr = $tr;
$this->th = $th;
$this->basic = $basic;

/* Start timing */
$this->th->start();
}
}


That's probably not way one should do it, but it works ;)



But now if I want to use phpUnit, I heard it ignores global objects, so



 var_dump($this->th);


is NULL.



Is there no way, that I can use phpUnit with my class system? If not, what do I need to change?



I tried to extend each class, but then my app was SUPER slow...










share|improve this question
























  • You should pass all of your global variables to as constructor arguments - then you won't need global variables any more
    – Robbie Averill
    Nov 12 at 19:59










  • Hi Robbie, yes that could work. But it's also not so nice to have so many arguments then... No other idea?
    – B. Rentrug
    Nov 12 at 21:22










  • Arguably it's nicer than using global variables in OOP. You're already setting all of the global arguments to class properties in your constructor, so why not allow them to be passed to the constructor instead? You could make them nullable if you have cases where you don't always want/need to pass them
    – Robbie Averill
    Nov 13 at 9:22










  • Yes, I see your point. But now I have in my test file this: $usr = new User($db); PhpUnit gives me an error: Undefined variable: db. But I load in the phpunit.xml file my autloader.php, were i defined $db. <phpunit bootstrap="inc/autoload.php"> Why is this not working?
    – B. Rentrug
    Nov 13 at 14:58










  • It works now! I needed to add Global $usr; in the testfile!
    – B. Rentrug
    Nov 13 at 16:45














1












1








1







I'm new to php Unit test, but I could installed it and make it work (PHP 7.1.2 / phpUnit 6.1.1 on a Mac / MAMP system). But I now I can't let it test my app. Probably due to a strange class architectural hierarchy.



In my autoload.php I load my classes like that:



/* get timing functions */
$th = new TimingHelper();

/* Get basic functions */
$basic = new basic();

/* Get App data (param: fake localhost true / false) */
$App = new App();

/* Start DB Connection */
$db = new db(0);

/* Get usr data (param: fake user mode true / false, user id) */
$usr = new User();


There are even more classes... ;)



Now inside the User Class I get the previous classes like that:



class User {
/* get DB connection */
private $db, $App, $tr, $th, $basic;

public function __construct() {

/* get DB connection */
Global $db, $App, $tr, $th, $basic;
$this->db = $db;
$this->App = $App;
$this->tr = $tr;
$this->th = $th;
$this->basic = $basic;

/* Start timing */
$this->th->start();
}
}


That's probably not way one should do it, but it works ;)



But now if I want to use phpUnit, I heard it ignores global objects, so



 var_dump($this->th);


is NULL.



Is there no way, that I can use phpUnit with my class system? If not, what do I need to change?



I tried to extend each class, but then my app was SUPER slow...










share|improve this question















I'm new to php Unit test, but I could installed it and make it work (PHP 7.1.2 / phpUnit 6.1.1 on a Mac / MAMP system). But I now I can't let it test my app. Probably due to a strange class architectural hierarchy.



In my autoload.php I load my classes like that:



/* get timing functions */
$th = new TimingHelper();

/* Get basic functions */
$basic = new basic();

/* Get App data (param: fake localhost true / false) */
$App = new App();

/* Start DB Connection */
$db = new db(0);

/* Get usr data (param: fake user mode true / false, user id) */
$usr = new User();


There are even more classes... ;)



Now inside the User Class I get the previous classes like that:



class User {
/* get DB connection */
private $db, $App, $tr, $th, $basic;

public function __construct() {

/* get DB connection */
Global $db, $App, $tr, $th, $basic;
$this->db = $db;
$this->App = $App;
$this->tr = $tr;
$this->th = $th;
$this->basic = $basic;

/* Start timing */
$this->th->start();
}
}


That's probably not way one should do it, but it works ;)



But now if I want to use phpUnit, I heard it ignores global objects, so



 var_dump($this->th);


is NULL.



Is there no way, that I can use phpUnit with my class system? If not, what do I need to change?



I tried to extend each class, but then my app was SUPER slow...







php phpunit






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 19:37









MonkeyZeus

12.5k22154




12.5k22154










asked Nov 12 at 19:33









B. Rentrug

266




266












  • You should pass all of your global variables to as constructor arguments - then you won't need global variables any more
    – Robbie Averill
    Nov 12 at 19:59










  • Hi Robbie, yes that could work. But it's also not so nice to have so many arguments then... No other idea?
    – B. Rentrug
    Nov 12 at 21:22










  • Arguably it's nicer than using global variables in OOP. You're already setting all of the global arguments to class properties in your constructor, so why not allow them to be passed to the constructor instead? You could make them nullable if you have cases where you don't always want/need to pass them
    – Robbie Averill
    Nov 13 at 9:22










  • Yes, I see your point. But now I have in my test file this: $usr = new User($db); PhpUnit gives me an error: Undefined variable: db. But I load in the phpunit.xml file my autloader.php, were i defined $db. <phpunit bootstrap="inc/autoload.php"> Why is this not working?
    – B. Rentrug
    Nov 13 at 14:58










  • It works now! I needed to add Global $usr; in the testfile!
    – B. Rentrug
    Nov 13 at 16:45


















  • You should pass all of your global variables to as constructor arguments - then you won't need global variables any more
    – Robbie Averill
    Nov 12 at 19:59










  • Hi Robbie, yes that could work. But it's also not so nice to have so many arguments then... No other idea?
    – B. Rentrug
    Nov 12 at 21:22










  • Arguably it's nicer than using global variables in OOP. You're already setting all of the global arguments to class properties in your constructor, so why not allow them to be passed to the constructor instead? You could make them nullable if you have cases where you don't always want/need to pass them
    – Robbie Averill
    Nov 13 at 9:22










  • Yes, I see your point. But now I have in my test file this: $usr = new User($db); PhpUnit gives me an error: Undefined variable: db. But I load in the phpunit.xml file my autloader.php, were i defined $db. <phpunit bootstrap="inc/autoload.php"> Why is this not working?
    – B. Rentrug
    Nov 13 at 14:58










  • It works now! I needed to add Global $usr; in the testfile!
    – B. Rentrug
    Nov 13 at 16:45
















You should pass all of your global variables to as constructor arguments - then you won't need global variables any more
– Robbie Averill
Nov 12 at 19:59




You should pass all of your global variables to as constructor arguments - then you won't need global variables any more
– Robbie Averill
Nov 12 at 19:59












Hi Robbie, yes that could work. But it's also not so nice to have so many arguments then... No other idea?
– B. Rentrug
Nov 12 at 21:22




Hi Robbie, yes that could work. But it's also not so nice to have so many arguments then... No other idea?
– B. Rentrug
Nov 12 at 21:22












Arguably it's nicer than using global variables in OOP. You're already setting all of the global arguments to class properties in your constructor, so why not allow them to be passed to the constructor instead? You could make them nullable if you have cases where you don't always want/need to pass them
– Robbie Averill
Nov 13 at 9:22




Arguably it's nicer than using global variables in OOP. You're already setting all of the global arguments to class properties in your constructor, so why not allow them to be passed to the constructor instead? You could make them nullable if you have cases where you don't always want/need to pass them
– Robbie Averill
Nov 13 at 9:22












Yes, I see your point. But now I have in my test file this: $usr = new User($db); PhpUnit gives me an error: Undefined variable: db. But I load in the phpunit.xml file my autloader.php, were i defined $db. <phpunit bootstrap="inc/autoload.php"> Why is this not working?
– B. Rentrug
Nov 13 at 14:58




Yes, I see your point. But now I have in my test file this: $usr = new User($db); PhpUnit gives me an error: Undefined variable: db. But I load in the phpunit.xml file my autloader.php, were i defined $db. <phpunit bootstrap="inc/autoload.php"> Why is this not working?
– B. Rentrug
Nov 13 at 14:58












It works now! I needed to add Global $usr; in the testfile!
– B. Rentrug
Nov 13 at 16:45




It works now! I needed to add Global $usr; in the testfile!
– B. Rentrug
Nov 13 at 16:45

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268922%2fphpunit-global-variable-returns-null%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268922%2fphpunit-global-variable-returns-null%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine