Writing a unit test to my index controller, i need to capture the html exit from the console into an array or string in order to test it.
But i have no idea how to capture the exit from my phpunit console.
Any thoughts ?
Thanks to John comment about ob_get_clean i got the thing done like that:
$userController = new Application\controllers\User;
ob_start();
$userController->index();
$output = ob_get_clean();
echo "Output: " . $output;
Related
I am designing my own MVC pattern to ease the process of creating homepages. My templating system needs my controller class to output my views. This means I have to output the file through a php function. I have been searching for some a while now and can't seem to find a solution.
How can I, through a PHP function, run a string representing some source code ("< ?", "< ?php", "? >" and so on) as php? Eval would not take my < ? signs (and I read that function is crap for some reason).
You could execute the php code and collect the output like this:
ob_start();
include "template.phtml";
$out1 = ob_get_clean();
http://www.php.net/manual/de/function.ob-get-contents.php
Just include()ing the file instead should be fine. I've not dug that deeply into the source code but I'm fairly sure that's how Zend Framework implements templates.
Replacing "echo file_get_contents()" with "include()" as GordonM suggested worked exactly as needed. can't upvote yet since I'm too new but for my needs this is the direct answer to the headlined question.
$code = '<?php
function GetBetween($content, $start, $end) {
$r = explode($start, $content);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return '';
}';
function _readphp_eval($code) {
ob_start();
print eval('?>'. $code);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
print _readphp_eval($code);
Well, eval is not crap per se, it's just easy to create a security hole with that if you're not careful about what you allow inside that eval (that, and maybe the fact that it can be bad for readability of the code).
As for the <? signs, that's because eval expects php code, so if you wan't to mix php with plain output, include just the closing ?> tag.
In general however, you can implement templates in better fashion, try to look into output buffering.
I'd like it if ob_start() didn't let echo's output to their normal destination and just log their contents instead. But it doesn't seem to be doing that. Any ideas? Here's my code:
<?php
ob_start();
echo 'test';
$out = ob_get_contents();
var_dump($out);
test is still echo'd. It's var_dump'd, as well, but I don't want it to be echo'd.
Any ideas?
Thanks!
The output buffer is automatically flushed at the end of the script, so it's expected behaviour.
You are looking for ob_get_clean(), which returns the current buffer before clearing it:
$out = ob_get_clean();
i have a site, where i buffer some output with
ob_start();
...
and it worked fine until today i updated my debian from an older php5.3 to the latest php5.3.3-7+squeeze8
Now i sometimes have something in the output buffer before i call it the first time
please don't answer things like
"header must be called before any output is sent."
(I know, I work a lot with output buffers)
when i set an extra ob_get_clean(); at the very first line of my script, it works
<?
ob_get_clean();
it seems, like php is creating some output beforehand
if i put the first line
<? print_r(ob_get_clean()); ?>
then i see, that there is an empty string already in the buffer:
""
on all other pages it isn't, there ob_get_clean(); contains
null
is it possible you have some " " in front of your <?php somewhere? or wrong file encoding issue its usually some kind of that nature, check your files and include files.
Now i sometimes have something in the output buffer before i call it
the first time
It'll be much easier if you give us some info about that mysterious data.
perhaps a case of BOM character?
more info here
i found it:
i had no invisible character in front, it was something different: i called ob_end_clean() one time too much:
this was my code, inside a function i call:
function print_something(){
ob_start();
echo some stuff...
echo ob_get_clean();
ob_end_clean(); // this was the bug!
}
it seems, that you can clear your main output buffer ;)
I'm having trouble with setting a variable and then giving it the outcome of a translated string. What am I doing wrong?
# usage: this translates some text into different language.
echo __('some text');
# make a variable and fill it with the outcome of the translated text
$title="echo __('translated content text')";
The first line outputs nicely.
The second line output literaly echo __('translated concent text').
Update
Thanks all. great answers. Gosh how stupid i must ve been, therefore am a bit wiser now:)
$title = __('Colourful train rides'); # works
now experimenting with these endings
ob_end_flush();
ob_end_clean();
Don't include quotes around the function call and don't call echo:
$title = __('translated concent text');
The first line outputs nicely
It sounds like that function echoes (also, its name would be very confusing if it didn't).
You will need to use an output buffer to capture that output.
ob_start();
__echo('translated concent text');
$title = ob_get_contents();
ob_end_clean(); // Thanks BoltClock
You're wrapping the function call in quotes, so it's being outputted literally (instead of invoking the function). Take away the pair of quotes, and you'll have a result closer to what you want:
$title = __echo('translated concent text');
However, for this to work the __echo() function will have to return the string, not just echo it. If you want to catch the output, you'll have to use PHP's Output Buffering.
ob_start();
__echo('translated concent text');
$title = ob_get_contents();
ob_end_clean();
I'm using a PHP library that echoes a result rather than returns it. Is there an easy way to capture the output from echo/print and store it in a variable? (Other text has already been output, and output buffering is not being used.)
You could use output buffering :
ob_start();
function test ($var) {
echo $var;
}
test("hello");
$content = ob_get_clean();
var_dump($content); // string(5) "hello"
But it's not a clean and fun syntax to use. It may be a good idea to find a better library...
The only way I know.
ob_start();
echo "Some String";
$var = ob_get_clean();
You should really rewrite the class if you can. I doubt it would be that hard to find the echo/print statements and replace them with $output .=. Using ob_xxx does take resources.
Its always good practise not to echo data until your application as fully completed, for example
<?php
echo 'Start';
session_start();
?>
now session_start along with another string of functions would not work as there's already been data outputted as the response, but by doing the following:
<?php
$output = 'Start';
session_start();
echo $output;
?>
This would work and its less error prone, but if its a must that you need to capture output then you would do:
ob_start();
//Whatever you want here
$data = ob_get_contents();
//Then we clean out that buffer with:
ob_end_clean();