I'm trying to create my first test in laravel.
$random = str_random(12);
$payload = [
'name' => $random,
'email' => $random .'#www.com',
'password' => '123456',
];
$this->json('post', '/api/v1/register', $payload)
->assertStatus(200);
The method being called from this url /api/v1/register contains:
$rules = User::rules();
$validation = \Validator::make( $this->input, $rules );
if ( $validation->fails() ) {
return true;
}else return true;
When I run the test it gives response status 500.
when I write ( echo 1; ) above this line :
if ( $validation->fails() ) {
it outputs (1 ) plus the 500 status response
If I write echo 1; below the same line, nothing outputted, so that line the line producing the error.
I check laravel log file and this is the error:
[2018-03-03 12:55:07] testing.ERROR: SQLSTATE[HY000] [2002] No such
file or directory {"exception":"[object] (PDOException(code: 2002):
SQLSTATE[HY000] [2002] No such file or directory at
/opt/lampp/htdocs/ws/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:67)
Note: calling the same URL from the browser or from a rest client works fine with no errors.
Have you made any changes to your phpunit.xml? It sounds to me like your testing environment is looking for a database that is not there.
Related
Phpunit, Laravel 5.5
How do I emulate, not fake, an actual file upload with phpunit and Laravel. My latest stab at it is like this. From the unit test:
$handle = fopen($path,'r');
$content = fread($handle,2048);
fclose($handle);
$fdata = [
'delimiter' => '3',
'id' => 1,
'allow_shared_roles' => 'on',
'file'=>$name
];
$this->call('POST','/event/add-wizard/2',$fdata,[],[],[
'Content-Length'=>strlen($content),
'Content-Type'=>'multipart/form-data;boundary='.$content,
'Content-Disposition'=>'form-data;name="file";filename="'.$name.'"'
],$content);
Then on the server side, this is where I get hung up.
if ($request->hasFile('file')) {
$input['extension'] = strtolower($request->file('file')->getClientOriginalExtension());
}
$validator = \Validator::make($input, ['file' => 'required', 'extension' => 'in:csv', 'delimiter' => 'required'], ['extension.in' => 'The file must be a .csv file.']);
if ($validator->fails()) {
return \Redirect::back()->withInput()->withErrors($validator);
}
if (!file_exists(storage_path('temp-files'))) {
\File::makeDirectory(storage_path('temp-files'));
}
$date = \Carbon\Carbon::now();
$tmpFile = $request->file('file')->move(storage_path('temp-files'), $date->format('YmdHis') . '_' . $request->file('file')->getClientOriginalName());
Then I get move on null error on the last line shown.
Having never done this kind of thing before I admit I'm stabbing in the dark. Any help would be greatly appreciated.
After confirming in the comments that you want to check if the upload routine is being followed instead really uploading a file you can mock the facade File to see if the methods are called and with the right parameters (optional).
To mock a Facade in Laravel you can use the build in shouldReceive('method_name') method. In your situation you can add this before the call:
// should create new directory
File::shouldReceive('makeDirectory')
->once();
// should move the uploaded file to the dir
File::shouldReceive('move')
->once()
->andReturn( $fake_file );
You can read more about mocking facades here.
Using the below PHP code to invoke lambda function but i am getting the below error. Looked at the PHP AWS api docuumentation not able to find whats causing issue.
Receiving the below error from aws php sdk
PHP Warning: Illegal string offset 'StatusCode' in /var/www/html/Guzzle/Service/Command/LocationVisitor/Response/StatusCodeVisitor.php on line 21
PHP Warning: Illegal string offset 'FunctionError' in /var/www/html/Guzzle/Service/Command/LocationVisitor/Response/HeaderVisitor.php on line 24
PHP Warning: Illegal string offset 'LogResult' in /var/www/html/Guzzle/Service/Command/LocationVisitor/Response/HeaderVisitor.php on line 24
PHP Catchable fatal error: Argument 1 passed to Guzzle\Service\Resource\Model::__construct() must be of the type array, string given, called in /var/www/html/Guzzle/Service/Command/OperationResponseParser.php on line 86 and defined in /var/www/html/Guzzle/Service/Resource/Model.php on line 20
use Aws\Lambda\LambdaClient;
$client = LambdaClient::factory(array(
'credentials' => array(
'key' => $f_key,
'secret' => $f_secret,
),
'region' => 'ap-southeast-2'
));
try {
echo "new invoke" . "\n";
$result = $client->invoke(array(
// FunctionName is required
'FunctionName' => 'hello_wo’,
));
}catch(Exception $e) {
echo 'Error retrieving lambd error message code-' . $e->getCode . '-error message-' . $e->getMessage() ;
}
Try this:
$client = LambdaClient::factory([
'key' => $f_key,
'secret' => $f_secret,
'region' => 'ap-southeast-2'
]);
I'm trying to make an asynchronous POST call, but guzzle call returns the following error:
" ErrorException in Request.php line 220:
Argument 1 passed to Symfony\Component\HttpFoundation\Request::__construct() must be of the type array, string given, called in C:\Program Files ...\app\Http\Controllers\ConfirmAccountController.php on line 87 and defined "
In ConfirmAccountController:
$client = new Client(['base_uri' => 'correct_api_address']);
$request = new Request('POST', 'testpromoboiler/updateUser', [
'query' => ['token' => $user->sdg_token ,
'address' => $user->address ,
]
]);
$promise = $client->sendAsync($request)->then(function ($response) {
echo 'I completed! ' . $response->getBody();
});
$promise->wait();
Why I get this error?
I have done two other synchronous calls and everything went well.
Thanks
That code instantiates Laravel frameworks Request class.
Use guzzles request method instead:
$request = $client->request('POST', 'testpromoboiler/updateUser', [
'query' => ['token' => $user->sdg_token ,
'address' => $user->address ,
]
]);
Code:
function getUserId($userName) {
$myModel = new MY_Model();
$requiredFields = "id";
$whereClause = [ // line number 35
"is_active" => 'ACTIVE',
"user_name" => $userName
];
$userId = $myModel->select("users", $requiredFields, $whereClause);
return $userId;
}
Error:
Parse error: syntax error, unexpected '[' in /home/com/public_html/_apis/application/helpers/custom_helper.php on line 35
A PHP Error was encountered
Severity: Parsing Error
Message: syntax error, unexpected '['
Filename: helpers/custom_helper.php
Line Number: 35
Backtrace:
Replace Where Clause with this (Hope this help) :
$whereClause = array( // line number 35
"is_active" => 'ACTIVE',
"user_name" => $userName
);
Try this
$whereClause = array();
$whereClause['is_active'] = 'ACTIVE';
$whereClause['user_name'] = $userName;
I'm tring to return products from my shopify store and dump the expected returned json on to the page. No products return via the code but if I go directly to the url in the form below I get the expected json displayed on the page:
https://API_KEY:PASSWORD#your-store.myshopify.com/admin/products.json
Can anyone help me return the json using this package which I'm using to connect to shopify? I'm also using Laravel 5.
I've noticed that the Input calls return nothing:
Input::get('code');
Input::all();
I added this to app.php:
'Input' => Illuminate\Support\Facades\Input::class,
I'm getting this error:
ERROR #22: The requested URL returned error: 401
in api.php line 309 at API->call(array('METHOD' => 'GET', 'URL' => '/admin/products.json?page=1')) in routes.php line 87
This is my route:
Route::get('/show_products', function() {
$shopify = App::make('ShopifyAPI');
// This creates an instance of the Shopify API wrapper and
// authenticates our app.
$shopify = App::make('ShopifyAPI', [
'API_KEY' => 'api_key',
'API_SECRET' => 'api_secret',
'SHOP_DOMAIN' => 'shop_domain.myshopify.com',
'ACCESS_TOKEN' => 'access_token'
]);
$shopify->installURL(['permissions' => array('read_products', 'write_products'), 'redirect' => 'https://dev.shopify.com/public/']);
try {
$verify = $shopify->verifyRequest(Input::all(), true);
if ($verify)
{
$code = Input::get('code');
echo "code: ".$code; // no code returned
$accessToken = $shopify->getAccessToken($code);
echo "accessToken: ".$accessToken; // no access token
}
else
{
echo "issue with data";
// Issue with data
}
}
catch (Exception $e)
{
echo '<pre> Error: ' . $e->getMessage() . '</pre>';
}
// Gets a list of products
$result = $shopify->call([
'METHOD' => 'GET',
'URL' => '/admin/products.json?page=1'
]);
$products = $result->products;
print_r($products);
exit;
});
I ran into this issue on Android. The accessing the products by the url with key/secret worked fine if I was in a browser, but failed trying to do a basic GET against that URL from the device.
I couldn't figure out why, but I ended up setting the header on the request, and that worked well.
Where you had
https://API_KEY:PASSWORD#your-store.myshopify.com/admin/products.json
Instead, request
https://your-store.myshopify.com/admin/products.json
but set the header x-shopify-access-token to PASSWORD