mock catch exception using PhpUnit - phpunit

recently i see some issue on my projects, a found that there is some exception that i forget to catch them! this is my code:
try {
$this->user = $invoiceEvent->user;
$this->invoice = $invoiceEvent->invoice;
if ($this->user->email) {
$this->sendEmail();
}
}catch (Swift_RfcComplianceException $e) {
}
by using this try/catch my problem is solved, this is my test, and is green!, who can i assert the exception is catch?
/**
* #test
*/
public function it_should_Not_provide_exception_when_mailFromAddress_is_not_set()
{
$invoice = $this->makeInvoice();
$user = $this->makeUser();
$mail = app('Illuminate\Contracts\Mail\Mailer');
(new SendInvoiceToUser($mail))->handle(new InvoiceCreated($invoice, $user));
}

finally using this way i assert the exception catch an my method in this situation return null that is exactly what i except.
/**
* #test
*/
public function it_should_Not_provide_exception_when_mailFromAddress_is_not_set()
{
$invoice = $this->makeInvoice();
$user = $this->makeUser();
$mail = app('Illuminate\Contracts\Mail\Mailer');
$mocked = $this->getMockBuilder(SendInvoiceToUser::class)
->setConstructorArgs([$mail])
->setMethods(null)
->getMock();
$this->assertNull($mocked->handle(new InvoiceCreated($invoice, $user)));
}
by pass null to ->setMethods(null) the mocke object run the actual code contained within the method when called,

Related

How to print subcommand result automatically?

I have Java CLI application based on cliche library and I want to migrate it to picocli.
My application was based on cliche so I have a lot of methods with asg.cliche.Command annotation which return some result. cliche prints command methods's result automatically so result was printed in command line. I replaced asg.cliche.Command annotations by picocli.CommandLine.Command and I see that picocli does not print command methods's results.
I have following class:
import picocli.CommandLine;
#CommandLine.Command(subcommandsRepeatable = true)
public class Foo
{
public static void main( String[] args )
{
new CommandLine( new Foo() ).execute( args );
}
#CommandLine.Command
public String sayHello()
{
return "Hello";
}
#CommandLine.Command
public String sayGoodbye()
{
return "GoodBye";
}
}
when I call java -cp myJar.jar Foo sayHello sayGoodbye I do not see any output.
I see three solutions:
1. Modify each methods to print result instead of return it.
import picocli.CommandLine;
#CommandLine.Command( subcommandsRepeatable = true )
public class Foo2
{
public static void main( String[] args )
{
new CommandLine( new Foo2() ).execute( args );
}
#CommandLine.Command
public void sayHello()
{
System.out.println( "Hello" );
}
#CommandLine.Command
public void sayGoodbye()
{
System.out.println( "GoodBye" );
}
}
I am not happy with this solution. I prefer not modify my methods.
Retrieve results after execution.
public static void main( String[] args )
{
final CommandLine commandLine = new CommandLine( new Foo() );
commandLine.execute( args );
CommandLine.ParseResult parseResult = commandLine.getParseResult();
for( CommandLine.ParseResult pr : parseResult.subcommands() )
{
System.out.println( pr.commandSpec().commandLine()
.getExecutionResult()
.toString() );
}
}
I see a few problems with this solution. The main problem is formatting. Execution result can be null, array, collection. The second problem is that results are printed after execution of all subcommands. If second subcommand throws exception then I firstly see exception stack trace and after that I see result of first subcommand.
Ask on stackoverflow if there is some better solution. I do not believe that there is no any configuration option in picocli which enable results printing.
Personally, I like your first solution best, it is simple and easy to maintain. Maybe introduce a helper method for the printing and formatting so the command methods can look like this:
#CommandLine.Command
public String sayGoodbye()
{
return printValue("GoodBye");
}
You already found the CommandLine.getParseResult method; perhaps a helper method could assist with the formatting there as well.
There is a third option, but it is unfortunately quite a bit more complex: you can create a custom IExecutionStrategy that prints the result of each command after executing it. It involves copying a lot of code from the picocli internals and it’s not really a realistic solution; I just mention it for completeness.
// extend RunLast to handle requests for help/version and exit code stuff
class PrintingExecutionStrategy extends CommandLine.RunLast {
#Override
protected List<Object> handle(ParseResult parseResult) throws ExecutionException {
// Simplified: executes only the last subcommand (so no repeating subcommands).
// Look at RunLast.executeUserObjectOfLastSubcommandWithSameParent if you need repeating subcommands.
List<CommandLine> parsedCommands = parseResult.asCommandLineList();
CommandLine last = parsedCommands.get(parsedCommands.size() - 1);
return execute(last, new ArrayList<Object>());
}
// copied from CommandLine.executeUserObject,
// modified to print the execution result
private List<Object> execute(CommandLine cmd, List<Object> executionResultList) throws Exception {
Object command = parsed.getCommand();
if (command instanceof Runnable) {
try {
((Runnable) command).run();
parsed.setExecutionResult(null); // 4.0
executionResultList.add(null); // for compatibility with picocli 2.x
return executionResultList;
} catch (ParameterException ex) {
throw ex;
} catch (ExecutionException ex) {
throw ex;
} catch (Exception ex) {
throw new ExecutionException(parsed, "Error while running command (" + command + "): " + ex, ex);
}
} else if (command instanceof Callable) {
try {
#SuppressWarnings("unchecked") Callable<Object> callable = (Callable<Object>) command;
Object executionResult = callable.call();
System.out.println(executionResult); <-------- print result
parsed.setExecutionResult(executionResult);
executionResultList.add(executionResult);
return executionResultList;
} catch (ParameterException ex) {
throw ex;
} catch (ExecutionException ex) {
throw ex;
} catch (Exception ex) {
throw new ExecutionException(parsed, "Error while calling command (" + command + "): " + ex, ex);
}
} else if (command instanceof Method) {
try {
Method method = (Method) command;
Object[] parsedArgs = parsed.getCommandSpec().argValues();
Object executionResult;
if (Modifier.isStatic(method.getModifiers())) {
executionResult = method.invoke(null, parsedArgs); // invoke static method
} else if (parsed.getCommandSpec().parent() != null) {
executionResult = method.invoke(parsed.getCommandSpec().parent().userObject(), parsedArgs);
} else {
executionResult = method.invoke(parsed.factory.create(method.getDeclaringClass()), parsedArgs);
}
System.out.println(executionResult); <-------- print result
parsed.setExecutionResult(executionResult);
executionResultList.add(executionResult);
return executionResultList;
} catch (InvocationTargetException ex) {
Throwable t = ex.getTargetException();
if (t instanceof ParameterException) {
throw (ParameterException) t;
} else if (t instanceof ExecutionException) {
throw (ExecutionException) t;
} else {
throw new ExecutionException(parsed, "Error while calling command (" + command + "): " + t, t);
}
} catch (Exception ex) {
throw new ExecutionException(parsed, "Unhandled error while calling command (" + command + "): " + ex, ex);
}
}
throw new ExecutionException(parsed, "Parsed command (" + command + ") is not a Method, Runnable or Callable");
}
}
Use it like this:
public static void main(String... args) {
new CommandLine(new Foo())
.setExecutionStrategy(new PrintingExecutionStrategy())
.execute(args);
}
I wouldn’t recommend the above.
Update: I thought of another, fourth, option (actually a variation of your 2nd solution). You can specify a custom IExecutionExceptionHandler that doesn’t print the stacktrace, but instead stores the exception so you can print the stacktrace after printing the command results. Something like this:
class MyHandler extends IExecutionExceptionHandler() {
Exception exception;
public int handleExecutionException(Exception ex,
CommandLine commandLine,
ParseResult parseResult) {
//ex.printStackTrace(); // no stack trace
exception = ex;
}
}
Use it like this:
public static void main(String... args) {
MyHandler handler = new MyHandler();
CommandLine cmd = new CommandLine(new Foo())
.setExecutionExceptionHandler(handler);
cmd.execute(args);
ParseResult parseResult = cmd.getParseResult();
for( ParseResult pr : parseResult.subcommands() )
{
System.out.println( pr.commandSpec().commandLine()
.getExecutionResult()
.toString() );
}
if (handler.exception != null) {
handler.exception.printStackTrace();
}
}

How to test service function without connecting on database

I'm turning again on you guys, because I spend my fair share of hours on this "task" and I still can't figure out how to test my service method, without my function connection on database (I have to mock repository functions)
This is my service function
public function getInfo($history, $name)
{
$requestRepository = $this->em->getRepository(Request::class);
if ($history) {
$requests = [];
foreach ($requestRepository->getRequestsByName($name) as $request) {
$requests[] = $requestRepository->transform($request);
}
return $requests;
} else {
$request = $requestRepository->getCompletedRequestByName($name);
if (!is_null($request)) {
return $requestRepository->transform($request);
} else {
return null;
}
}
}
And this is my test
public function testGetInfo()
{
/* This returns errors, because it tries to connect to DATABASE, but I don't wan't that, that's why I figure out I need to mock this
$requestManager = new RequestManager($this->entityManager);
$test = $requestManager->getInfo('histroy', 'antrax.com');
*/
$requestManager = $this->getMockBuilder(RequestManager::class)->disableOriginalConstructor()->setMethods(['getInfo'])
->getMock();
// And rest of this are just my FAILED attempts to figure out, how to test my methods
$queryBuilder = $this->getMockBuilder(RequestRepository::class)->disableOriginalConstructor()
->setMethods(['getInfo'])->getMock();
$test = $queryBuilder->method('getInfo')->willReturnSelf();
$queryBuilder->method('getInfo')->willReturnCallback(function ($field, $value) use ($queryBuilder, $test){
if ($field == 'newStatus') {
$this->assertSame('EXPIRED', $value);
}
return $queryBuilder;
});
}
Can some one please help me how to write a test for my method getInfo so it will have 100% cover. If you need any additional informations, please let me know and I will provide. Thank you!
This is an answer to my problem
public function testGetInfo()
{
$mockEntity = $this->mockEntityManager;
$name = 'antrax.com';
$requestMock = new RequestEntity();
$transformedRequest = [
'id' => 1
];
$requestRepo = $this->getMockBuilder(RequestRepository::class)->disableOriginalConstructor()
->setMethods(['getRequestsByName', 'transform', 'getCompletedRequestByName'])->getMock();
$requestRepo->method('getRequestsByName')->willReturnCallback(function ($passedName) use ($name, $requestMock) {
$this->assertSame($name, $passedName);
return [$requestMock, $requestMock];
});
$requestRepo->method('transform')->willReturnCallback(function ($request) use ($requestMock, $transformedRequest) {
$this->assertSame($requestMock, $request);
return $transformedRequest;
});
$i = 0;
$requestRepo->method('getCompletedRequestByName')->willReturnCallback(function ($passedName) use ($name, $requestMock, &$i) {
$this->assertSame($name, $passedName);
if ($i == 0) {
$i+=1;
return null;
} else {
return $requestMock;
}
});
$mockEntity->method('getRepository')->willReturnCallback(function ($requestClass) use ($requestRepo) {
$this->assertSame(RequestEntity::class, $requestClass);
return $requestRepo;
});
$requestManager = new RequestManager($mockEntity);
$this->assertSame([$transformedRequest, $transformedRequest], $requestManager->getInfo(true, $name));
$this->assertNull($requestManager->getInfo(false, $name));
$this->assertSame($transformedRequest, $requestManager->getInfo(false, $name));
}

Access session variable from different controller in symfony

i want to create an array as a session table to put it empty in the beggining for my cart shop to display nothing as an empty cart and when i press AddtoCart i want get that array and do an array_push with the new items but i didn't know how to do it
This is the first controller when i create the array empty
public function FrontAction()
{
$pro=new Produit();
$pro->setNom('');
$pro->setQuantite(0);
$pro->setPrix(0);
$em = $this->getDoctrine()->getManager();
$sess=new Session();
$sess->setName('PANIER');
$sess=array('');
array_push($sess,$pro->getNom(),$pro->getQuantite(),$pro->getPrix());
$paniers = $em->getRepository(Panier::class)->findByUserId(1);
$produits = $this->getDoctrine()->getRepository(Produit::class)->findAll();
$boutiques = $this->getDoctrine()->getRepository(Boutique::class)->GetBestShopsDQL();
if ($paniers != null)
{
$prixTotal = 0;
foreach ($paniers as $panier) {
$prixTotal += $panier->getPrixTotal();
}
$resultCount = count($paniers);
return $this->render('BoutiqueBundle:FrontViews:ListBoutique.html.twig', array('produits' => $produits, 'boutiques' => $boutiques,'paniers' => $paniers, 'prixTotal' => $prixTotal,'resultCount' => $resultCount));
}
return $this->render('BoutiqueBundle:FrontViews:ListBoutique.html.twig', array('produits' => $produits, 'boutiques' => $boutiques,'sess'=>$sess));
}
and this is the second controller where i want to fill that empty array with new items
public function ajouterauPanierAction($id)
{
$ses=new Session();
$ses->getName('PANIER');
$test=array('');
$test=$ses;
// $user_id = $this->getUser()->getId(); //à modifier!!!!!
$em = $this->getDoctrine()->getManager();
$produit = $em->getRepository(Produit::class)->find($id);
$test = $em->getRepository(Panier::class)->findExistant($id, 1);
// $session->replace(array_push($produit,));
if(($produit != null)&&(empty($test)))
{
array_push($test,$produit->getNom(),$produit->getQuantite(),$produit->getPrix());
/* $panier = new Panier();
$panier->setProduitId($produit->getId());
$panier->setUserId(1); //à changer avec le fos
$panier->setDate(new \DateTime("now"));
$panier->setPrixTotal($produit->getPrix());
$em->persist($panier);
*/ $em->flush();
$msg = "success";
// return $this->redirectToRoute('Dashboard');
}
else
{
//return $this->render('BoutiqueBundle:FrontViews:404.html.twig');
$msg = "failure";
}
return new JsonResponse(array('msg' => $msg));
}
i didn't know how to do it correctly or if my idea is wrong so hope u guys got what i need to do
Here is how I am doing it in Symfony 4, (I think this part is unchanged). First I have declared my session keys as class constants on the entities to avoid collisions.
class Appointment
{
const SESSION_KEY = 'confirmed_app_entity_appointment';
...
}
Then in the controller use the SessionInterface and it will get autowired into your controller method with a typehinted parameter (SessionInterface $session). Symfony will handle starting the session, just set, get, and/or remove the key as needed.
use Symfony\Component\HttpFoundation\Session\SessionInterface;
...
public function confirmAppointment($first_name, $last_name, $time, SessionInterface $session)
{
...
$session->set(Appointment::SESSION_KEY, $appointment);
$session->set(StaffMember::SESSION_KEY_SELECTED_STAFF, $member);
...
if ($this->isConflict($appointment)) {
$session->remove(Appointment::SESSION_KEY);
$session->remove(StaffMember::SESSION_KEY_AUTH);
$this->addFlash('error', 'Failed to create Appointment, conflict found');
return $this->redirectToRoute('customer');
}
...
}
public function saveAppointment(SessionInterface $session)
{
if (!empty($session->get(Appointment::SESSION_KEY))) {
$appointment = $session->get(Appointment::SESSION_KEY);
}
...
}

Functional test fails although content exists

In the functional test below, the assertion that the text 'Glenshire' exists fails even though the output of echo $client->getResponse()->getContent(); includes
<li>Glenshire...
The first two assertions are true. There is no redirect.
The test
class SearchTest extends WebTestCase
{
public function setUp()
{
$classes = array(
'Vol\VolBundle\DataFixtures\ORM\LoadFocusSkillData',
'Vol\VolBundle\DataFixtures\ORM\LoadOpportunity',
);
$this->loadFixtures($classes);
}
public function testSearch()
{
$client = static::createClient();
$crawler = $client->request('GET', '/search');
$this->assertTrue($crawler->filter('html:contains("Focus")')->count() > 0);
$this->assertTrue($crawler->filter('html:contains("Skill")')->count() > 0);
$form = $crawler->selectButton('match_search[Submit]')->form();
$form['match_search[focuses][0]'] = 1;
$client->submit($form);
echo $client->getResponse()->getContent();
$this->assertTrue($crawler->filter('li:contains("Glenshire")')->count() > 0, 'Glenshire not found');
}
The fixture (using LiipFunctionalTestBundle)
public function load(ObjectManager $manager)
{
$manager->clear();
$org = new Organization();
$org->setOrganization('Glenshire Marmot Fund');
$foc1 = $manager->getRepository("VolVolBundle:Focus")->find(1);
$foc3 = $manager->getRepository("VolVolBundle:Focus")->find(3);
$foc4 = $manager->getRepository("VolVolBundle:Focus")->find(4);
$org->addFocus($foc1);
$org->addFocus($foc3);
$org->addFocus($foc4);
$opp = new Opportunity();
$opp->setName('Feeder');
$opp->setDescription("Beyond recourse");
$opp->setExpireDate(date_add(new \DateTime(), new \DateInterval('P1Y')));
$opp->setOrganization($org);
$manager->persist($opp);
$manager->flush();
}
Curiously, replacing the line
$this->assertTrue($crawler->filter('li:contains("Glenshire")')->count() > 0, 'Glenshire not found');
with
$this->assertRegExp(
'/Glenshire/', $client->getResponse()->getContent(), 'Glenshire not found'
);
provides a successful test!

Bukkit How do I get a argument from a string?

I'm trying to make a plugin that detects when people chat
"#say " it will broadcast a message with those arguments.
What I need to know is how to get arguments from a string.
Please help.
Main:
package com.gong.say;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin{
String sayMessage = ChatColor.GREEN + "Your message has been said!";
public void onEnable()
{
Bukkit.getLogger().info("[BukkitAPIEnhancer] Plugin started!");
Bukkit.getPluginManager().registerEvents(new ChatListener(this), this);
}
public void onDisable()
{
Bukkit.getLogger().info("[BukkitAPIEnhancer] Plugin disabled!");
}
}
ChatListener:
package com.gong.say;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
public class ChatListener implements Listener {
Main plugin;
public ChatListener(Main plugin)
{
this.plugin = plugin;
}
#EventHandler
public void onChat(AsyncPlayerChatEvent e)
{
if(e.isAsynchronous())
{
String message = e.getMessage();
if(message.contains("#say"))
{
//String[] args = Arguments after #say
//Bukkit.broadcastMessage(args);
}
}
}
}
You should usually use commands prefixed by /, so, normally you would do /say String[args], and It would be easier to get the arguments, yet if you want it to be prefixed by #, then that's another story. You could do something like this:
if(message.contains("#say")){
String messageToSend = message.replaceAll("#say", "");//get the arguments
if(messageToSend.length <= 0){//make sure there's something after #say
e.getPlayer().sendMessage("Correct usage: #say <arguments>"); //the user didn't put anything after #say
return;
}
else{
e.setCancelled(true);//cancel the event
Bukkit.getServer().broadcastMessage(messageToSend);//send the message that comes after "#say"
//you may want to add a chat color to the message to make it stand out more
}
}
So, here's what your event should look like:
#EventHandler
public void onChat(AsyncPlayerChatEvent e){
if(e.isAsynchronous()){
String message = e.getMessage();
if(message.contains("#say")){
String messageToSend = message.replaceAll("#say", "");//get the arguments
if(messageToSend.length <= 0){//make sure there's something after #say
e.getPlayer().sendMessage("Correct usage: #say <arguments>"); //the user didn't put anything after #say
return;
}
else{
e.setCancelled(true);//cancel the event
Bukkit.getServer().broadcastMessage(messageToSend);//send the message that comes after "#say"
//you may want to add a chat color to the message to make it stand out more
}
}
}
}
#EventHandler
public void onChat2(AsyncPlayerChatEvent e) {
if(e.isAsynchronous()) {
String msg = e.getMessage();
/** Verify if message starts with #say **/
if(msg.startsWith("#say")) {
/** Split message for get the args **/
String[] args = e.getMessage().split(" ");
/** Verify if have something after #say **/
if(args.length > 1) {
/** Cancel message and broadcast **/
e.setCancelled(true);
StringBuilder sb = new StringBuilder();
for(int i = 1; i <args.length; i++) {
sb.append(args[i] + " ");
}
/** Add color to broadcast */
String broadcast = ChatColor.translateAlternateColorCodes('&', sb.toString());
/** Broadcast prefix **/
String prefix = "§c[Broadcast] §r";
/** Broadcast **/
Bukkit.broadcastMessage(prefix + broadcast);
}
}
}
}

Resources