Im testing this function
/**
* #Route("/list", name="_clients")
* #Method("GET")
*/
public function ClientsAction()
{
$em = $this->getDoctrine()->getManager();
$data = $em->getRepository('InvoiceBundle:Clients')->findByUser($this->user());
if($data){
$Clients = array();
foreach($data as $v){
if($v->getCompanyId() != 0 ) {
$companyId = $v->getCompanyId();
} else {
$companyId = '';
}
if ($v->getClient() == 'person'){
$company = $v->getName().' '.$v->getLname();
} else {
$company = $v->getCompany();
}
$Clients[] = array(
'id' => $v->getId(),
'settings' => $company,
'companyId' => $companyId,
'client' => $v->getClient(),
'mobile' => $v->getMobile(),
'email' => $v->getEmail(),
'clientName' => $v->getClientName(),
'delivery' => $v->getDelivery(),
'ContactPerson' => $v->getContactPerson()
);
}
} else {
$Clients = array('data' => 'empty');
}
$response = new JsonResponse($Clients);
return $response;
}
The function it self runs correctly , but then i want to check if my 'Content-Type' is Json with this function
public function testClients()
{
$client = static::createClient();
$client->request('GET', '/clients/list');
$this->assertTrue(
$client->getResponse()->headers->contains(
'Content-Type',
'application/json'
)
);
}
with this i get a FALSE value.
Then i try to do a test for Status code
$this->assertSame(200, $client->getResponse()->getStatusCode());
With this i get error 500 instead of 200 OK
I understand that is why i get a FALSE value in my 'Content-Type' test but i cant get why.
Im doing all this according to the Symfony documentation.
May be i'm doing something wrong or is it just that you cant check the 'Content-Type'?
Any help would be appreciated!
JsonResponse does add the Content-Type header (application/json) so this should not be an issue.
I think the main issue is that you are missing $ on the client->request() line.
Edit :
Before the declaration of your class, did you add #Route("/clients") ?
Or, maybe the data returned by findByUser is not what you expected and calls to $v fail.
Related
I am developing my unit tests for an API created in Symfony4
Reading the Guzzle documentation I generated the following code:
File SecurityControllerTest.php
$client = new Client([
'base_uri' => 'http://localhost/sacrepad/sacrepad-api/public/index.php/',
'timeout' => 2.0,
]);
$data = array();
$data['email'] = 'admin#admin.com';
$data['password'] = '12345678';
$data2 = array();
$data2['json'] = $data;
$formData = json_encode($data);
$response = $client->request('POST', 'login', [
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
'form_params' => [
'json' => $formData,
]
]);
$body = json_decode($response->getBody(), true);
File SecurityController.php
/**
* #Route("/login", name="login", methods={"POST"})
*/
public function login(Request $request,Helpers $helpers,ValidatorInterface $validator, JwtAuth $jwtauth) {
$data = array(
'status' => 'error',
'code' => 400,
'msg' => 'data not received'
);
$json = $request->request->get('json');
$params = json_decode($json);
}
When I run the tests with the phpunit command, I get the following error:
1) App\Tests\SecurityControllerTest::testAuth GuzzleHttp\Exception\ServerException: Server error: `POST http://localhost/sacrepad/sacrepad-api/public/index.php/login` resulted in a `500 Internal Server Error` response:
If I change the name of the request:
$json = $request->request->get('json2');
It works and it returns me the following:
array(3) {
["status"]=>
string(5) "error"
["code"]=>
int(400)
["msg"]=>
string(18) "data not received"
}
Any ideas on how to make it work and send the parameters?
i build a class for working with guzzle
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
class Api
{
protected $client;
protected $url;
public function __construct()
{
$this->client = new Client([
'verify'=>false
]);
$this->url = 'http://localhost/sacrepad/sacrepad-api/public/';
}
public function get($endpoint, $params = [], $headers = [])
{
$response = $this->sendRequest(
'GET',
$this->url . $endpoint,
$params,
$headers
);
return $response;
}
public function post($endpoint, $params = [], $headers = [])
{
$response = $this->sendRequest(
'POST',
$this->url . $endpoint,
$params,
$headers
);
return $response;
}
public function sendRequest($type, $url, $params = [], $headers = [])
{
if ($type == 'GET') {
$data = [
'query' => $params
];
} elseif ($type == 'FILE') {
$type = 'POST';
$data = [
'multipart' => $params // TODO implements later
];
} else {
$data = [
'json' => $params
];
}
if (!empty($headers)) {
$data['headers'] = $headers;
}
$data['headers']['X-REAL-IP'] = $_SERVER['REMOTE_ADDR'];
$data['headers']['User-Agent'] = $_SERVER['HTTP_USER_AGENT'];;
$data['headers']['X-Platform'] = 'web';
try {
$response = $this->client->request(
$type,
$url,
$data
);
if (in_array($response->getStatusCode(), ['200', '403', '404'])) {
return json_decode($response->getBody());
}
return false;
} catch (RequestException $re) {
if (in_array($re->getResponse()->getStatusCode(), ['403', '404', '422'])) {
return json_decode($re->getResponse()->getBody());
}
return json_decode($re->getResponse()->getBody());
} catch (Exception $e) {
return false;
}
}
}
when i want to send a post request it would be like this
$response = (new Api())->post('index.php/',[
'email'=> 'admin#admin.com',
'password' => '123456'
]);
now it will send a post request to index.php and send email and password data i hope it would be helpful
I am a big fan of Laravel's Cache::remember functionality and I use it on my service classes like this:
/**
* SummaryService
*/
public function getSummaryData(string $userId)
{
$summaryCacheKey = $userId . '_summary_cache';
$summaryCacheLifespanMinutes = config('summary_cache_lifespan_minutes');
return Cache::remember($summaryCacheKey, $summaryCacheLifespanMinutes, function () use ($userId) {
$summaryResult = [
'userExists' => false,
'data' => [],
];
$user = $this->userRepository->findById($userId);
if ($user) {
$summaryResult = [
'userExists' => true,
'data' => $this->summaryRepository->getSummaryByUserId($user->id),
];
}
return $summaryResult;
});
}
This works as expected. If the data is present in the cache, it's returned and if it's not, it's loaded and cached and returned.
Now, I am trying to unit test my SummaryService (both execution paths).
The first part where the data is returned via cache is easy to test and it looks like this:
public function i_can_load_summary_data_via_cache()
{
// given
$userId = 'aaaa45-bbbb-cccc-ddddssswwwdw';
$expectedResult = [
'userExists' => true,
'data' => [ ... ],
];
$summaryCacheKey = $userId . '_summary_cache';
$summaryCacheLifespanMinutes = config('summary_cache_lifespan_minutes');
Cache::shouldReceive('remember')
->once()
->with($summaryCacheKey, $summaryCacheLifespanMinutes, Closure::class)
->andReturn($expectedResult);
// when
$result = $this->summaryService->getSummaryData($userId);
// then
$this->assertSame($expectedResult, $result);
}
However, when I try to test scenario where the data is not present in cache and I have to load it (via mocked repositories) like so:
public function i_can_load_summary_data_via_database()
{
// given
$userId = 'aaaa45-bbbb-cccc-ddddssswwwdw';
$expectedResult = [
'userExists' => true,
'data' => [ ... ],
];
$user = new User();
$user->id = $userId;
$summaryCacheKey = $userId . '_summary_cache';
$summaryCacheLifespanMinutes = 0;
Cache::shouldReceive('remember')
->once()
->with($summaryCacheKey, $summaryCacheLifespanMinutes, \Mockery::on(function() use($user) {
$this->mockedUserRepository
->shouldReceive('findById')
->once()
->andReturn($user);
$this->mockedSummaryRepository
->shouldReceive('getSummaryByUserId')
->once()
->with($user->id)
->andReturn([ ... ]);
}))
->andReturn($expectedResult);
// when
$result = $this->summaryService->getSummaryData($userId);
// then
$this->assertSame($expectedResult, $result);
}
The test is failing:
No matching handler found for
Mockery_3_Illuminate_Cache_CacheManager::remember('aaaa45-bbbb-cccc-ddddssswwwdw_summary_cache', '10', object(Closure)). Either the method was unexpected or its
arguments matched no expected argument list for this method
Objects: ( array ( 'Closure' => array (
'class' => 'Closure',
'properties' =>
array (
), ), ))
Any idea on how to test this properly?
Okay, I seem to have over complicated this; so I have broken it down and fixed it slightly differently like this.
My service code now looks like this:
/**
* SummaryService
*/
public function getSummaryData(string $userId)
{
$summaryCacheKey = $userId . '_summary_cache';
$summaryCacheLifespanMinutes = config('summary_cache_lifespan_minutes');
return Cache::remember($summaryCacheKey, $summaryCacheLifespanMinutes, function () use ($userId) {
return $this->loadLiveSummaryData($userId);
});
}
public function loadLiveSummaryData(string $userId)
{
$summaryResult = [
'userExists' => false,
'data' => [],
];
$user = $this->userRepository->findById($userId);
if ($user) {
$summaryResult = [
'userExists' => true,
'data' => $this->summaryRepository->getSummaryByUserId($user->id),
];
}
return $summaryResult;
}
and now, I just need to confirm via my unit test that:
My service can load cached version and match the call parameters
and I can load live data (where i can mock the repos)
Which looks something like this:
/**
* #test
*/
public function i_can_load_live_summary_data_for_existing_user()
{
// given
$userId = 'aaaa45-bbbb-cccc-ddddssswwwdw';
$expectedResult = [
'userExists' => true,
'data' => [ ... ],
];
$user = new User();
$user->id = $userId;
$this->mockedUserRepository
->shouldReceive('findById')
->once()
->andReturn($user);
$this->mockedSummaryRepository
->shouldReceive('getSummaryByUserId')
->once()
->with($user->id)
->andReturn([ ... ]);
// when
$result = $this->summaryService->loadLiveSummaryData($userId);
// then
$this->assertSame($expectedResult, $result);
}
/**
* #test
*/
public function i_expect_cache_to_be_called_when_loading_summary_data_for_specific_user()
{
// given
$userId = 'aaaa45-bbbb-cccc-ddddssswwwdw';
$expectedResult = [
'userExists' => true,
'data' => [ ... ],
];
$summaryCacheKey = $userId . '_summary_cache';
$summaryCacheLifespanMinutes = 10;
Cache::shouldReceive('remember')
->once()
->with($summaryCacheKey, $summaryCacheLifespanMinutes, \Mockery::on(function($value) {
return is_callable($value);
}))
->andReturn($expectedResult);
// when
$result = $this->summaryService->getSummaryData($userId);
// then
$this->assertSame($expectedResult, $result);
}
Let me know if there was a better or "correct" way to do this.
Had a similar situation where I wanted to test both paths, when data is returned via the cache and when the callback function is executed.
The key for me was to not use any of the facade mock method (e.g. Cache::shouldReceive('remember')) and then the callback code will run.
Seems pretty obvious now :(
These are my controllers
<?php
public static function getAccessToken()
{
$url = 'http://api.tech/oauth/authenticate';
$query = [
'grant_type' => 'client_credentials',
'client_id' => 'E3PuC',
'client_secret' => 'IhvkpkvMdAL7gqpL',
'scope' => 'bookings.read,images.create,images.read,images.update,locations.read,rates.read,rates.update,reports.read,reviews.read,rooms.create,rooms.delete,properties.read',
];
$client = new Client();
$response = $client->get($url, ['query' => $query]);
$content = json_decode($response->getBody()->getContents());
if ($content) {
return $content->access_token;
} else {
return null;
}
}
public function getReviews()
{
$client = new Client();
$access_token = $this->getAccessToken();
$url = 'http://api.tech/hotels/88244/reviews';
$query = [
'access_token' => $access_token,
];
$response = $client->get($url, ['query' => $query]);
$content = json_decode($response->getBody()->getContents());
if ($content->status == 'success') {
// return $content->access_token;
return $content->data;
// return $response;
} else {
return null;
}
}
public function index()
{
$content = $this->getReviews();
return view('channel.channel', [
'content' => $content
]);
}
When i try to output the content in my blade as a link, it says ---
htmlspecialchars() expects parameter 1 to be string, array given
and this is my blade file
This
It also throws an error when i try to output it like thus
{{$content}}
Please How can i solve the error
My question isn't a duplicate cause once i dd it shows an array and i want a link to show the array on a different page
Try Using:
{! $content !}
Or Use:
#json($content)
new with Laravel and I am trying to add a findOrFail on this specific route and it's giving me a hard time. What am I missing?
Route::get('/listing/{type}/{owner}/{id}/{address}', 'Properties\DisplayController#show');
Whats not working
Route::get('/listing/{type}/{owner}/{id}/{address}', function ($id) {
return Properties\DisplayController#show::findOrFail($id);
});
Error I am getting
Parse error: syntax error, unexpected '#', expecting ';'
controller/function I'm calling
public function show($type, $own, $id, $address = null)
{
$page = (object) $this->template;
$page->breadcrumbs[] = array('url' => 'javascript://', 'text' => 'Property Search', 'attribute' => array('data-component' => 'back'));
// Now lets query our server
$client = new GuzzleHttp\Client(['verify' => false ]);
$response = $client->get( env('LISTINGS_SERVER', 'https://listings.homicity.com') . '/property/' . $id);
$page->content = Property::parseResult($response->getBody());
$page->title = strtoupper(trim($page->content->address));
$page->breadcrumbs[] = array('text' => $page->title);
$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
$currency = 'CAD';
$raw = $formatter->parseCurrency($page->content->price, $currency );
$page->content->mortgage = Mortgage::stage(
false,
$raw
);
return view('property.display', compact('page'));
}
Thanks for the help!
To return directly on route:
Route::get('/listing/{type}/{owner}/{id}/{address}', function ($id) {
return App\YourModel::findOrFail($id);
});
https://laravel.com/docs/5.3/eloquent#retrieving-single-models
Since the model is on another server that we connect to using GuzzleHTTP, I could not put findOfFail() on the model.
Here is the edit to the controller. Added in the ['http_errors' => false] which prevents guzzle from returning http errors, and then a if statement using getStatusCode() to find if it was a error 500 or not.
public function show($type, $own, $id, $address = null)
{
$page = (object) $this->template;
$page->breadcrumbs[] = array('url' => 'javascript://', 'text' => 'Property Search', 'attribute' => array('data-component' => 'back'));
// Now lets query our server
$client = new GuzzleHttp\Client(['verify' => false ]);
$response = $client->get( env('LISTINGS_SERVER', 'https://listings.homicity.com') . '/property/' . $id, ['http_errors' => false]);
if ($response->getStatusCode() == "500") {
abort(404);
}
else {
$page->content = Property::parseResult($response->getBody());
$page->title = strtoupper(trim($page->content->address));
$page->breadcrumbs[] = array('text' => $page->title);
$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
$currency = 'CAD';
$raw = $formatter->parseCurrency($page->content->price, $currency );
$page->content->mortgage = Mortgage::stage(
false,
$raw
);
return view('property.display', compact('page'));
}
}
I'm trying to test a method which is using a service, and apparently it's not possible to test it like a normal method.
Does someone know what to do ?
I have this code for the moment :
namespace PlatformBundle\Tests;
use PlatformBundle\Controller\PaymentController;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class PaymentControllerTest extends WebTestCase
{
private $payment;
public function __construct() { parent::__construct(); $this->payment = new PaymentController(); }
public function testSendEmail()
{
$param = array(
'info' => array(
'email' => 'test#test.com', 'name' => 'test', 'fare' => 'test', 'id' => 'test'
)
);
$this->assertEquals(true, $this->invokeMethod($this->payment, 'sendEmail', $param));
}
/**
* Call protected/private method of a class.
*
* #param object &$object Instantiated object that we will run method on.
* #param string $methodName Method name to call
* #param array $parameters Array of parameters to pass into method.
*
* #return mixed Method return.
*/
public function invokeMethod(&$object, $methodName, array $parameters = array())
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $parameters);
}
}
The controller where the method sendEmail is :
<?php
namespace PlatformBundle\Controller;
use PlatformBundle\Entity\Customer;
use PlatformBundle\Entity\Promocode;
use PlatformBundle\Entity\Transfer;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class PaymentController extends Controller
{
public function checkoutAction(Request $req)
{
if (! $req->isMethod('POST')) throw new AccessDeniedHttpException();
$info = $req->request->all();
$this->container->get('platform.formSecurity')->testAllInformation($info);
$this->saveCustomerIntoDb($info);
$info['payed'] = false;
$session = $req->getSession();
$session->set('info', $info);
$info['date'] = $this->container->get('platform.useful')->reverseDateFormat($info['date']);
return $this->render('PlatformBundle:Payment:checkout.html.twig', array(
'isIndex' => false,
'info' => $info,
'stripe' => $this->stripeConfig()
));
}
public function cancelAction(Request $req)
{
$req->getSession()->invalidate();
return $this->render('PlatformBundle:Payment:cancel.html.twig', array('isIndex' => false));
}
public function successAction(Request $req)
{
$session = $req->getSession();
$info = $session->get('info');
if ($info['payed']) {
$req->getSession()->invalidate();
if ($info === null) throw new Exception('Please contact us to make sure that the payment has been done and that your order has been taken into account.');
$this->saveTransferIntoDb($info);
$customer = $this->getDoctrine()->getManager()->getRepository('PlatformBundle:Customer')->findOneBy(array(
'email' => $info['email']
));
$transfer = $this->getDoctrine()->getManager()->getRepository('PlatformBundle:Transfer')->findOneBy(
array('customer' => $customer->getId()),
array('id' => 'desc'),
1
);
$info['id'] = $transfer->getId();
$info['date'] = $this->container->get('platform.useful')->reverseDateFormat($info['date']);
$this->sendEmail($info);
// if 5 payments done, send a promocode
if (is_int($customer->getPayments() / 5)) {
$this->createAndSendNewPromocode($customer);
}
return $this->render('PlatformBundle:Payment:success.html.twig', array(
'isIndex' => false,
'info' => $info
));
} else return new RedirectResponse('cancel');
}
private function sendEmail($info)
{
$mail = $this->container->get('platform.mail');
$mail->send(
$info['email'],
'You have ordered a transfer for Dublin',
$this->renderView('PlatformBundle:Mail:orderSucceed.html.twig', array('info' => $info)),
'info#dubair.ie'
);
$mail->send(
'info#airportcollections.net, info#dubair.ie, info#365onlineholidays.com',
'A customer ordered a transfer for Dublin',
$this->renderView('PlatformBundle:Mail:report.html.twig', array('info' => $info)),
'info#dubair.ie'
);
}
private function saveCustomerIntoDb($info)
{
// test if the customer already exist
$customersList = $this->getDoctrine()->getManager()->getRepository('PlatformBundle:Customer')
->findByEmail($info['email']);
$customerExists = (sizeof($customersList) == 1 ? true : false);
if ($customerExists) {
$customer = $customersList[0];
} else {
// Create the entity
$customer = new Customer();
// dateRegistration, country and ip are automatically created in the constructor
$customer->setEmail($info['email']);
$customer->setPayments(0);
}
$customer->setName($info['name']);
$customer->setPhone($info['phone']);
$em = $this->getDoctrine()->getManager();
$em->persist($customer);
$em->flush();
}
private function saveTransferIntoDb($info)
{
$customers = $this->getDoctrine()->getManager()->getRepository('PlatformBundle:Customer')
->findByEmail($info['email']);
$customer = $customers[0];
$customer->setPayments($customer->getPayments() + 1);
// make promocode outdated
if ($info['promocode'] != '') {
$promocode = $this->getDoctrine()->getManager()->getRepository('PlatformBundle:Promocode')
->findOneBy(array(
'value' => $info['promocode'],
'outdated' => 0,
'type' => 'short'
));
$promocode->setOutdated(1);
}
// test if transfer already exist
$transferList = $this->getDoctrine()->getManager()->getRepository('PlatformBundle:Transfer')->findBy(
array(
'customer' => $customer,
'pickup' => $info['pickup'],
'destination' => $info['destination'],
'pickupTime' => $info['pickupTime'],
'address' => $info['address']
), // criteria
array('pickup' => 'desc'), // sorting
5, // Limit
0 // Offset
);
// if transfer doesn't already exist, create it
if (sizeof($transferList) == 0) {
$transfer = new Transfer();
$transfer->setPickup($info['pickup']);
$transfer->setDestination($info['destination']);
$dateArray = explode('-', $info['date']);
$transfer->setDate(new \DateTime($dateArray[2].'-'.$dateArray[1].'-'.$dateArray[0]));
$transfer->setAddress($info['address']);
$transfer->setFlightTime($info['flightTime']);
$transfer->setPickupTime($info['pickupTime']);
$transfer->setSeats($info['seats']);
$transfer->setAirline($info['airline']);
$transfer->setFlight($info['flight']);
$transfer->setType($info['type']);
$transfer->setBags($info['bags']);
$transfer->setFare($info['fare']);
// join
$transfer->setCustomer($customer);
$em = $this->getDoctrine()->getManager();
$em->persist($transfer);
$em->flush();
}
}
private function createAndSendNewPromocode($customer)
{
$newPromocode = $this->container->get('platform.useful')->createRandomPassword();
$promocode = new Promocode();
$promocode->setValue($newPromocode);
$promocode->setType('short');
$promocode->setDiscount(10);
$em = $this->getDoctrine()->getManager();
$em->persist($promocode);
$em->flush();
$mail = $this->container->get('platform.mail');
$mail->send(
$customer->getEmail(),
'A promotional code for your next transfer on dubair.ie !',
$this->renderView('PlatformBundle:Mail:promocode.html.twig', array(
'customer' => $customer,
'promocode' => $newPromocode
)),
'info#dubair.ie'
);
}
private function stripeConfig()
{
$stripe = array(
"secret_key" => "xx",
"publishable_key" => "xx"
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
return $stripe;
}
public function stripeChargeAction(Request $req)
{
$this->stripeConfig();
$info = $req->getSession()->get('info');
$amount = ($info['fare'] * 100);
$info['payed'] = true;
$req->getSession()->set('info', $info);
$token = $req->request->get('stripeToken');
$customer = \Stripe\Customer::create(array(
'email' => $req->request->get('email'),
'card' => $token
));
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $amount,
'currency' => 'eur'
));
return new RedirectResponse('success');
}
}
thanks