testing output buffering with phpspec - phpspec

Given the following PHPUnit code, how do I translate this to a phpspec test?
$content = 'Hello world!';
ob_start();
$this->displayer->output($content);
$output = ob_get_clean();
$this->assertEquals($content, $output);
What $this->displayer->output($content) does is simply echo the $content:
class Displayer {
public function display(string $content) { echo $content; }
}

I believe this is the only way:
use PHPUnit\Framework\Assert;
public function it_outputs_a_string()
{
$content = 'Hello world!';
ob_start();
$this->display($content);
$output = ob_get_clean();
Assert::assertEquals($content, $output);
}

Related

Try to use the codeigniter's file upload library as a general function from Helpers

Can anybody help as I am trying to use the codeigniter's upload library from the helpers folder but I keep getting the same error that I am not selecting an image to upload? Has any body tried this before?
class FileUpload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'file_uploading'));
$this->load->library('form_validation', 'upload');
}
public function index() {
$data = array('title' => 'File Upload');
$this->load->view('fileupload', $data);
}
public function doUpload() {
$submit = $this->input->post('submit');
if ( ! isset($submit)) {
echo "Form not submitted correctly";
} else { // Call the helper
if (isset($_FILES['image']['name'])) {
$result = doUpload($_FILES['image']);
if ($result) {
var_dump($result);
} else {
var_dump($result);
}
}
}
}
}
The Helper Function
<?php
function doUpload($param) {
$CI = &get_instance();
$CI->load->library('upload');
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|png|jpg|jpeg|png';
$config['file_name'] = date('YmdHms' . '_' . rand(1, 999999));
$CI->upload->initialize($config);
if ($CI->upload->do_upload($param['name'])) {
$uploaded = $CI->upload->data();
return $uploaded;
} else {
$uploaded = array('error' => $CI->upload->display_errors());
return $uploaded;
}
}
There are some minor mistakes in your code, please fix it as below,
$result = doUpload($_FILES['image']);
here you should pass the form field name, as per your code image is the name of file input.
so your code should be like
$result = doUpload('image');
then, inside the function doUpload you should update the code
from
$CI->upload->do_upload($param['name'])
to
$CI->upload->do_upload($param)
because Name of the form field should be pass to the do_upload function to make successful file upload.
NOTE
Make sure you added the enctype="multipart/form-data" in the form
element

Sitemap-XML in Processwire 3

how can i generate a sitemap for processwire 3 for huebert-webentwicklung.de/sitemap.xml it doesen't work with the Plugin MarkupSitemapXML. Any idea how to get it work?
Thanks.
Create a new page template (sitemap.xml) then set the page output to be XML the the PW backend. Create a page and link it (set it to hidden).
function renderSitemapPage(Page $page) {
return
"\n<url>" .
"\n\t<loc>" . $page->httpUrl . "</loc>" .
"\n\t<lastmod>" . date("Y-m-d", $page->modified) . "</lastmod>" .
"\n</url>";
}
function renderSitemapChildren(Page $page) {
$out = '';
$newParents = new PageArray();
$children = $page->children;
foreach($children as $child) {
$out .= renderSitemapPage($child);
if($child->numChildren) $newParents->add($child);
else wire('pages')->uncache($child);
}
foreach($newParents as $newParent) {
$out .= renderSitemapChildren($newParent);
wire('pages')->uncache($newParent);
}
return $out;
}
function renderSitemapXML(array $paths = array()) {
$out = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
array_unshift($paths, '/'); // prepend homepage
foreach($paths as $path) {
$page = wire('pages')->get($path);
if(!$page->id) continue;
$out .= renderSitemapPage($page);
if($page->numChildren) $out .= renderSitemapChildren($page);
}
$out .= "\n</urlset>";
return $out;
}
header("Content-Type: text/xml");
echo renderSitemapXML();

Why laravel assertions are not called from closure?

My code is:
public function testOne()
{
$mail = ['subject' => 'My subject'];
$this->assertEquals(1, 1);
$mock = Mockery::mock(\Illuminate\Support\Facades\Mail::getSwiftMailer());
$this->app['mailer']->setSwiftMailer($mock);
$msg = $mock->shouldReceive('send')->once()->andReturnUsing(function($msg) {
echo $msg->getSubject();
$this->assertEquals($mail['subject'], $msg->getSubject());
});
$this->assertEquals(1, 1);
}
I get output:
PHPUnit 4.8.24 by Sebastian Bergmann and contributors.
My subject
Time: 852 ms, Memory: 26.00Mb
OK (1 test, 2 assertions)
I see from output:
echo $msg->getSubject();
that I get good subject but nothing is asserted, why?
Use like this
public function testOne()
{
$mail = ['subject' => 'My subject'];
$this->assertEquals(1, 1);
$mock = Mockery::mock(\Illuminate\Support\Facades\Mail::getSwiftMailer());
$this->app['mailer']->setSwiftMailer($mock);
$obj = $this;
$msg = $mock->shouldReceive('send')->once()->andReturnUsing(function($msg) use ($obj, $mail) {
echo $msg->getSubject();
$obj->assertEquals($mail['subject'], $msg->getSubject());
});
$this->assertEquals(1, 1);
}
Inside your closure it's an instance of Closure so it will not get $this from there for that you have to assign to another variable & use it.
But you can rewrite your tests like this
public function testOne()
{
$mail = ['subject' => 'My subject'];
$this->assertEquals(1, 1);
$mock = Mockery::mock(\Illuminate\Support\Facades\Mail::getSwiftMailer());
$this->app['mailer']->setSwiftMailer($mock);
$message = null;
$mock->shouldReceive('send')->once()->andReturnUsing(function($msg) use ($message) {
$message = $msg->getSubject();
});
$this->assertEquals($mail['subject'], $message);
$this->assertEquals(1, 1);
}

Parse error NuSOAP webservice with Codeigniter

I'm using CodeIgniter with NuSOAP library for webservices and this is the error I get when accessing the Client controller:
wsdl error: XML error parsing WSDL from http://localhost/turismoadmin/index.php/Webservice/index/wsdl on line 77: Attribute without value
This is the server controller:
class Webservice extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->library('soap_lib');
$server = new nusoap_server;
$server->configureWSDL('Agencia Turistica', 'urn:server');
$server->wsdl->schemaTargetNamespace = 'urn:server';
$server->register('addcontact',
array('nombre' => 'xsd:string', 'apellido' => 'xsd:string' , 'ciudad' => 'xsd:string'),
array('return' => 'xsd:string'));
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA: '';
$server->service($HTTP_RAW_POST_DATA);
}
function index()
{
if($this->uri->rsegment(3)=="wsdl"){
$_SERVER['QUERY_STRING']="wsdl";
}else{
$_SERVER['QUERY_STRING']="";
}
function addcontact($nombre, $apellido, $ciudad){
$this->modelo_turismo->addcontact($nombre, $apellido, $ciudad);
$resultado = $this->modelo_turismo->selectmax_contacto();
return (json_encode($resultado->fetch_all()));
}
}
}
and this is the Client controller:
class Client extends CI_controller {
function __construct() {
parent::__construct();
}
function index() {
$this->load->library('soap_lib');
$this->nusoap_client = new nusoap_client(site_url('Webservice/index/wsdl'), true);
$err = $this->nusoap_client->getError();
if ($err){
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
}
$result1 = $this->nusoap_client->call('addcontact', array("marcos","de lafuente","hermosillo"));
echo($result1);
// Check for a fault
if ($this->nusoap_client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result1);
echo '</pre>';
} else {
// Check for errors
$err = $this->nusoap_client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result1);
echo '</pre>';
}
}
}
}
I'm trying to do it based
ON THIS TOPIC (Thanks nana.chorage)
I also added this entry to my config/routes.php
$route['Webservice/wsdl']="Webservice/index/wsdl";
And for not to pass unnoticed, I can see my service when I enter this URL:
http://localhost/turismoadmin/index.php/Webservice/wsdl
I really dont know what I'm doing wrong, I have searched a lot around and I can't get rid of it!
Then nusoap client URL should be like this
$this->nusoap_client = new nusoap_client(site_url('Webservice/index?wsdl'), 'wsdl');

how do i get the current year and compare it with the file.text name

example: i have this file name change log 2013.txt but when its 2014, I want it to create new file name change log 2014.txt
Anyone can help? this is my controller:
<?php
class changelog extends Controller {
function changelog()
{
parent::Controller();
$this->load->helper('form');
}
function index()
{
$this->change_log_add();
}
function change_log_view()
{
$data['message'] = read_file('C:\wamp\www\changeLog\change log 2013.txt');
$this->load->view('change_log_view', $data);
}
function change_log_add()
{
$this->session->set_flashdata('msg','Please Insert the task changed');
$data['action'] = 'changelog/change_log_save_add/';
$this->load->view('change_log_form', $data);
}
function change_log_save_add()
{
if($this->input->post('message') != NULL)
{
date_default_timezone_set ("Asia/Singapore");
$data = date("Y-m-d, H:i:s");
$body = $this->input->post('message');
$text = $data.' - '.$body."\r\n";
if ( ! write_file('C:\wamp\www\changeLog\change log 2013.txt', $text, 'a+'))
{
echo 'Unable to write the file';
}
else
{
$this->session->set_flashdata('msg', 'File Written');
redirect('changelog/change_log_read/');
}
}
}
function change_log_read()
{
if(read_file('C:\wamp\www\changeLog\change log 2013.txt') == NULL)
{
echo 'File is Empty!';
}
else
{
$string = read_file('C:\wamp\www\changeLog\change log 2013.txt');
echo $string;
redirect('changelog/change_log_view/');
}
}
function change_log_update()
{
$this->session->set_flashdata('msg','Please Insert the task changed');
$data['message'] = read_file('C:\wamp\www\changeLog\change log 2013.txt');
$data['action'] = 'changelog/change_log_save_update/';
$this->load->view('change_log_form', $data);
}
function change_log_save_update()
{
if($this->input->post('message') != NULL)
{
$message = $this->input->post('message');
if ( ! write_file('C:\wamp\www\changeLog\change log 2013.txt', $message, 'r+'))
{
echo 'Unable to write the file';
}
else
{
$this->session->set_flashdata('msg', 'File Written');
redirect('changelog/change_log_read/');
}
}
}
}
For each appearance of change log 2013.txt, replace it with :
'change log ' . date('Y') . '.txt'
For example, instead of :
write_file('C:\wamp\www\changeLog\change log 2013.txt', $message, 'r+')
change it to:
write_file('C:\wamp\www\changeLog\change log ' . date('Y') . '.txt', $message, 'r+')
You can change the static 2013 to use php date instead, this should be change in all instance of 2013 over your code
if ( ! write_file('C:\wamp\www\changeLog\changeLog' . date('Y') . '.txt', $message, 'r+'))
Codeigniter will create the file if it doesn't exists

Resources