PHPUnit and Laravel: Call to undefined method stdClass::isEmpty() - php

I'm trying to test this controller method.
public function handleCreate()
{
$offer = new Offer();
$offer->key_word = $key_word;
$offer->url = $url;
$offer->save();
return Redirect::action('OffersController#index');
}
Here is the test.
public function testCreate(){
Input::replace($input = array('key_word' => 'foo', 'url' => 'http://bar.com'));
$this->mock->shouldReceive('create')->once()->with($input);
$this->app->instance('Offer', $this->mock);
$this->call('POST', 'create');
$this->assertRedirectedToRoute('/');
}
I'm getting this error when running the test.
PHP Fatal error: Call to undefined method stdClass::isEmpty()
Here is the part of the view that is messing it up.
#if($offers->isEmpty())
<p>No offers found.</p>
#else
...
#endif
I cannot figure out how to add the isEmpty() method to my mock model to get past this error.
I have tried mocking the isEmpty() method with this but it still gave the same error.
$this->mock->shouldReceive('isEmpty')->once()->andReturn(false);

Seems the answer to my question was
$this->mock->shouldReceive('isEmpty')->once()->andReturn(false);
But I'm guessing I had something setup wrong the first time I tried it.

Related

I can't create a view in Laravel

I'm new in Laravel and I'm trying to create a View in Acelle (app based on Laravel). I read a lot of tutorials, but I've not been luck with this problem.
I created the view "lol.blade.php" on /resources/views folder with this code:
HELLO (just hello)
The Route:
Route::get('lol', function()
{
if (view()->exists('lol')) {
//return 'helloooo'; <--- it works
return view('lol');
} else {
return 'not exists';
}
});
The code knows the view exists, but the url (localhost/acelle/public/lol) prints this message:
"Whoops, looks like something went wrong."
I can't solve the problem with tutorials. I followed all the steps about creating views in Laravel, but I don't know why the view prints that message.
Please help!
PS: Laravel version: 5.2.45
EDIT:
In console [network] shows Error 500. and laravel.log prints 59 lines. but the first line show:
[2017-07-14 14:08:20] production.ERROR: ErrorException: Undefined index:controller in /home/acelle/public_html/acelle/app/Providers/AppServiceProv‌​ider.php:20
You posted this in the comments:
app('view')->composer('*', function ($view) {
$action = app('request')->route()->getAction();
$controller = class_basename($action['controller']);
list($controller, $action) = explode('#', $controller);
$view->with(compact('controller', 'action'));
});
Your issue is that this route uses a closure, and has no controller:
Route::get('lol', function() {});
Therefore, $action['controller'] doesn't exist and throws a warning as a result. You'll want to check isset($action['controller']) before doing the rest of your code that uses the controller variable.
Already solved!!
SOLUTION:
creating a controller : MiwebController.php
<?
namespace Acelle\Http\Controllers;
class MiwebController extends Controller
{
public function __construct()
{
parent::__construct();
$this->middleware('auth');
}
public function index()
{
return view('lol');
}
}
?>
routes.php:
Route::get('lol', 'MiwebController#index');
It works fine. Thank you!

Laravel Mail:: inside function creates error

I am making my own package.
I simplified the code to explain the weird error.
I have a simple method inside a simple controller. First, i am sending an email (printing in laravel.log) and works fine.
public function signup(){
Mail::send('vendor.proto.register.emails.proto-register-new-account', [], function ($m) {
$m->from('noreply#test.com', 'Hello');
$m->to('sfs#dgf.c', 'pepe')->subject('Test. Congratulations, your account was created');
});
return view('view path');
}
But when i want to move the Mail:: to a private method like this.
public function signup(){
$this->sendNotification2();
return view('view path');
}
public function sendNotification2(){
Mail::send('vendor.proto.register.emails.proto-register-new-account', [], function ($m) {
$m->from('noreply#test.com', 'Hello');
$m->to('sfs#dgf.c', 'pepe')->subject('Test. Congratulations, your account was created');
});
}
It crash an print the error
FatalErrorException in ClassLoader.php line 373: Maximum function nesting level of '100' reached, aborting!
Is it a bug? or i am doing something wrong ?
Solution.
Googling i found this solution. Notice i am using laravel 5.2 and php 5.6.7
Add in bootstrap\autoload.php this code ini_set('xdebug.max_nesting_level', 200); and it fix the problem.

Trying to get property of non-object (show.blade.php)

I have this problem in my routes where in I get this error tying to get property of non-object view (show.blade.php) on clicking a link to view/redirect allstats.blade.php. It is very weird because I am not returning my routes to show.blade.php but to allstats.blade.php. I was wondering if I am doing something wrong with the routes. This is very tricky, I don't know what's causing this error.
#Controller:
EmployeeController.php
public function postdisplaySummaryReport()
{
$employeesquery = Employee::paginate(10);
return View::make('employees.allstats')
->with('employeequerytoallstat', $employeesquery);
}
#Routes:
routes.php
Route::post('employees/sumarryReports','EmployeeController#postdisplaySummaryReport');
Route::resource('employees', 'EmployeeController');
#index.php(URL to the allstat.blade.php):
<span class="glyphicon glyphicon-folder-open"></span> {{trans('labels.payroll_reports.lbl_summary_report')}}
#ERROR:
Just change your function to this.
public function postdisplaySummaryReport()
{
$employeesquery = Employee::paginate(10);
return view('employees.allstats')
->with('employeequerytoallstat', $employeesquery);
}
also change your Route method to get instead of post if you want to display content.
Route::get('employees/sumarryReports','EmployeeController#postdisplaySummaryReport');

Fatal error: Class 'Zend_Log' not found

I'm getting the following error in magento administration
Fatal error: Class 'Zend_Log' not found in /home/website/public_html/app/code/community/Uni/Fileuploader/Block/Adminhtml/Fileuploader/Edit/Tab/Products.php on line 241
This is a community extension, which has been working fine on my website. The error makes no sense to me, because the line 241 contains just a closing "}" character.
class Uni_Fileuploader_Block_Adminhtml_Fileuploader_Edit_Tab_Products extends Mage_Adminhtml_Block_Widget_Grid {
...
...
...
public function getRowUrl() {
return '#';
}
public function getGridUrl() {
return $this->getUrl('*/*/productgrid', array('_current' => true));
}
protected function getFileuploaderData() {
return Mage::registry('fileuploader_data');
}
protected function _getSelectedProducts() {
$products = $this->getRequest()->getPost('selected_products');
if (is_null($products)) {
$products = explode(',', $this->getFileuploaderData()->getProductIds());
return (sizeof($products) > 0 ? $products : 0);
}
return $products;
}
} // line 241, where error occurs
I can post the rest of the code, if you need it.
I noticed that if I upgrade to PHP 5.4 version the error disappears, but since 5.4 version causes other errors on my website, I have to continue using 5.3.
Any ideas on how to solve this?
The problem could be the name of one of the methods in your custom class.
Take for example the method name is getData() ,
Try searching for generic method names in your script, such as getData, which might be reserved by some of Magento’s core classes. I figure that these methods have predefined functionality, which your module is missing support for, and Zend then tries to write an exception to Zend log.
Reference link: netismine
I got the same error when rewriting a payment method.
public function authorize($payment, $amount)
Solved rewriting exactly the same main method:
public function authorize(Varien_Object $payment, $amount)
Magento 1.9.1.0/PHP 5.5

sfBBCodeParserPlugin, problems with partial

I use symfony 1.4.8 , and sfBBCodeParserPlugin
It works , but I have problem with partial.
My IndexSuccess
include_partial('post/list', array('voice_posts' => $voice_posts)) ?>
In _list.php
echo $bb_parser->getRawValue()->qparse($voice_post->getDescription());
And I have error
Notice: Undefined variable: bb_parser in...
according to the readme I added in action.class
public function executeIndex(sfWebRequest $request)
{
....
$this->bb_parser = new sfBBCodeParser();
}
In ShowSuccess I do not use partial and all work fine.
ShowSuccess.php
echo $bb_parser->getRawValue()->qparse($voice_post->getDescription())
action.class
public function executeShow(sfWebRequest $request)
{
$this->bb_parser = new sfBBCodeParser();
...
}
p.s Sorry for my bad English
You forget you send to the partial the bb_parser:
include_partial('post/list', array('voice_posts' => $voice_posts, 'bb_parser' => $bb_parser))
Remember that the variables used in partials (unless they're global) must be sent when you're defining it.

Categories