Use Symfony service in phpunit - symfony

I would like to test my DAO service.
The problem is that nothing append when I execute my test. No test is executed.
But I'm sur that the config file phpunit.xml.dist is ok, beause if I make a mistake in the "use" line for exemple, the test show me the errors.
So I need your help..
My first test :
namespace RepositoryBundle\Tests\DAO;
use RepositoryBundle\Entity\User;
use RepositoryBundle\Enum\RoleEnum;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class UserDAOTest extends KernelTestCase
{
private $userDAO;
private $logger;
public function setUp()
{
self::bootKernel();
$this->userDAO = static::$kernel->getContainer()->get('repository.userDAO');
$this->logger = static::$kernel->getContainer()->get('logger');
$logger->notice('Show the log'); // Nothing is in my console.. (I configure Monolog to display in the console)
}
public function testTRUC()
{
//Stupid test but not executed !!!???
$i = 1;
$this->assertTrue($i == 1);
}
public function testCreate()
{
//Real test but not executed !!!???
$user1 = new User();
$user1->setName("TestName");
$user1->setEmail("TestEmail");
$user1->setPassword("TestPassword");
$user1->setMemberNumber(12345);
$user1->setAdmin(true);
$user1->setRole(RoleEnum::User);
$user1 = $UserDAO->save($user1);
$this->assertTrue($user1->getId() > 0);
$this->assertEquals("TestName", $user1->getName());
$this->assertEquals("TestEmail", $user1->getEmail());
$this->assertEquals("TestPassword", $user1->getPassword());
$this->assertEquals(12345, $user1->getMemberNumber());
$this->assertEquals(true, $user1->isAdmin());
$this->assertEquals(RoleEnum::User, $user1->getRole());
}
phpunit.xml.dist
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="phpunit.xsd"
bootstrap="bootstrap.php.cache"
backupGlobals="false"
verbose="true">
<testsuites>
<testsuite name="Project Test Suite">
<directory>../src/*Bundle/Tests</directory>
</testsuite>
</testsuites>
<php>
<const name="PHPUNIT_TESTSUITE" value="true"/>
</php>
</phpunit>
When I execute the command :
A screen of the console
Thanks for your help. I'm new with Symfony and PHPUnit and I lost a lot of time with this kind of problems..

Please correct me if I'm wrong, but your test is located in src\RepositoryBundle\Tests\DAO and you are executing phpunit on folder app...
Try phpunit(Edit of course c option requires an argument :/) and it should work.
You configuration is configured to look up '../src/*Bundle/Tests'. If you provide the app folder as the place to to look up test, then your configuration won't be considered.

Related

WebSocket not working on Jetty 9.4

My websocket servlet does not work on Jetty 9.4.6.v20170531 although it works perfectly with version 9.3.2.v20150730.
My code looks like this:
#SuppressWarnings("serial")
#WebServlet(name = "TcpProxy", urlPatterns = { "/sockets/tcpProxy" })
public class TcpProxySocketServlet extends WebSocketServlet {
#Override
public void configure(WebSocketServletFactory factory) {
factory.register(TcpProxySocket.class);
}
}
and
#WebSocket
public class TcpProxySocket {
/* ... */
public TcpProxySocket() {
LOGGER.info("Instantiating a TCP proxy");
}
/**
* Open a new socket
*
* #param session the session
*/
#OnWebSocketConnect
public void onConnect(Session session) throws RestException {
this.session = session;
CachedSession toriiSession = null;
...
When trying to access my socket, I get a 404 error.
On server side, the configure is never called.
I tried to force the loading of the servlet by adding it to web.xml
<servlet>
<servlet-name>TcpProxySocket</servlet-name>
<servlet-class>com.fujitsu.fse.torii.servlets.tcpProxy.TcpProxySocketServlet</servlet-class>
</servlet>
<servlet-mapping> <servlet-name>TcpProxySocket</servlet-name>
<url-pattern>/sockets/tcpProxy</url-pattern>
</servlet-mapping>
Then the servet is loaded, configure function is called.
When trying to open the socket, I don't get any error but the onConnect error is never called.
So far I have reverted to using Jetty 9.3.2, but it's not satisfying.
Any Idea ?
This was fixed by using a correct web-app markup in web.xml to use webapp version 3.1
## -1,6 +1,8 ##
<?xml version="1.0" encoding="UTF-8"?>
-<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
+<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
+ version="3.1">
The other problem was that the onConnect method was never called. It disappeared when I changed the servlet mapping using a path with a trailing slash ("/sockets/scripts/" instead of "/sockets/scripts").
We could not reproduce the trailing-slash problem on a simpler example. So I'm not sure if there was an actual problem or if it was just a misinterpretation of mine.
the full story is on https://github.com/eclipse/jetty.project/issues/1800
I thank Joakim and the Jetty project for their reactivity.
I had the same issue with Java Spark web framework when they updated Jetty to 9.4.
The trailing slash issue mentioned by Michael Dussere did the trick for me, I changed the path in my client from "http://example.org/chat/" to "http://example.org//chat" (in the server it is ".../chat" as well).

How can I prevent PHPUnit from running a PHP file in the Tests directory?

I have a lot of tests that require a particular mock object to be created. So, here is what I've done:
namespace AppBundle\Tests\Services\Terminal;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use AppBundle\Entity\Tunnel;
class TunnelBasedTestCase extends MockeryTestCase
{
/**
*
* #var Tunnel
*/
protected $tunnel;
protected function setup()
{
$tunnel = new Tunnel();
$tunnel->setName('B9')
->setIp('192.168.2.52')
->setUsername('username')
->setPassword('password')
->setSign('sign')
->setStatus('open');
$this->tunnel = $tunnel;
}
}
However, the problem is that now PHPUnit tries to run this test, which is only meant to prevent me from copy-pasting the setup() function. I don't want to skip it, because I want to get a completely green test result instead of getting something like:
....S.....
Or this:
....I.....
Here is my PHPUnit configuration that also fails to do this:
<testsuites>
<testsuite name="Project Test Suite">
<directory>../src/*/*Bundle/Tests</directory>
<directory>../src/*/Bundle/*Bundle/Tests</directory>
<directory>../src/*Bundle/Tests</directory>
<exclude>../src/AppBundle/Tests/Services/Terminal/TunnelBasedTestCase.php</exclude>
</testsuite>
</testsuites>
How can I do this?
you can edit your phpunit.xml like this:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
<testsuites>
<testsuite name="foo">
<directory>./tests/</directory>
<exclude>./tests/path/to/excluded/test.php</exclude>
^-------------
</testsuite>
</testsuites>
</phpunit>
You can also try to make a blacklist like this:
<filter>
<blacklist>
<file>../src/AppBundle/Tests/Services/Terminal/TunnelBasedTestCase.php</file>
</blacklist>
</filter>

Using EJBs from a different JAR

I'm developing a JAX-WS WebService in JDeveloper 11.1.1.4 that should use EJBs from a JAR previously deployed to a WebLogic server. Both the WebService project and the EJB project are my own code, but I'd like to deploy them separately. For now I'm experimenting with the setup.
In the ExampleEJB project I have a bean ExampleBean that implements a remote interface Example.
#Remote
public interface Example {
public String doRemoteStuff();
}
#Stateless(name = "Example", mappedName = "ExampleApplication-ExampleEJB-Example")
public class ExampleBean implements Example {
public String doRemoteStuff() {
return "did remote stuff";
}
}
In that project, I have two deploy descriptors (ejb-jar.xml and weblogic-ejb-jar.xml):
ejb-jar.xml
<?xml version = '1.0' encoding = 'UTF-8'?>
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
version="3.0" xmlns="http://java.sun.com/xml/ns/javaee">
<enterprise-beans>
<session>
<ejb-name>Example</ejb-name>
</session>
</enterprise-beans>
</ejb-jar>
weblogic-ejb-jar.xml
<?xml version = '1.0' encoding = 'UTF-8'?>
<weblogic-ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-ejb-jar http://www.bea.com/ns/weblogic/weblogic-ejb-jar/1.0/weblogic-ejb-jar.xsd"
xmlns="http://www.bea.com/ns/weblogic/weblogic-ejb-jar">
<weblogic-enterprise-bean>
<ejb-name>Example</ejb-name>
<stateless-session-descriptor/>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
Additionaly, I've created an EJB JAR deployment profile named example-ejb.jar and managed to deploy it to the server.
In the ExampleWS project I have an ExampleWebService:
#WebService(serviceName = "ExampleWebService")
public class ExampleWebService {
#EJB
Example example;
public String doStuff() {
return example.doRemoteStuff();
}
}
I added the ExampleEJB project dependency to this project (so it would compile). The only XML I have in this project is the web.xml used to describe the servlet. Also, I have the WebServices WAR file created automatically by jDeveloper when creating a WebService. Lastly, I created an EAR deployment profile named example-ws that only includes the WebServices WAR file in it's application assembly.
What do I need to do for this to work? Also, what would the procedure be if the ExampleEJB project was referenced from another project (say, AdditionalExampleEJB) that has additional beans that use ExampleBean? How would I reference the ExampleBean from there?
Thank you VERY MUCH for any help you can give me!
EDIT:
I've managed to reference the EJB from the WebService!
In the ExampleEJB project I modified the weblogic-ejb-jar.xml and now it looks like this:
weblogic-ejb-jar.xml
<?xml version = '1.0' encoding = 'UTF-8'?>
<weblogic-ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-ejb-jar http://www.bea.com/ns/weblogic/weblogic-ejb-jar/1.0/weblogic-ejb-jar.xsd"
xmlns="http://www.bea.com/ns/weblogic/weblogic-ejb-jar">
<weblogic-enterprise-bean>
<ejb-name>Example</ejb-name>
<stateless-session-descriptor>
<pool>
<max-beans-in-free-pool>10</max-beans-in-free-pool>
<initial-beans-in-free-pool>3</initial-beans-in-free-pool>
</pool>
<business-interface-jndi-name-map>
<business-remote>hr.example.Example</business-remote>
<jndi-name>ejb/example-ejb/Example</jndi-name>
</business-interface-jndi-name-map>
</stateless-session-descriptor>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
In the ExampleWS project I added a deployment descriptor weblogic.xml that looks like this:
weblogic.xml
<?xml version = '1.0' encoding = 'UTF-8'?>
<weblogic-web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-web-app http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd"
xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app">
<ejb-reference-description>
<ejb-ref-name>ExampleReference</ejb-ref-name>
<jndi-name>ejb/example-ejb/Example</jndi-name>
</ejb-reference-description>
</weblogic-web-app>
Note that the ExampleReference value and ejb/example-ejb/Example value are something I decided to enter - I think they is more or less a developer's choice.
Also, I referenced the EJB in my WebService using the ExampleReference value, so my ExampleWebService looks like this:
ExampleWebService
#WebService(serviceName = "ExampleWebService")
public class ExampleWebService {
#EJB(
name="ExampleReference"
)
Example example;
public String doStuff() {
return example.doRemoteStuff();
}
}
Lastly, in the deployment profile of ExampleWS (the WebServices.war) I added the dependency contributor and checked the interface Example.class element (NOT the ExampleBean.java that has the implementation).
Now, how would this work if the Example bean was referenced from another EJB project (not a WebService)?
So, for all those that encounter the same problem, I have solved it. There is no way to look up a remote EJB in EJB 3.0 other than using InitialContext.lookup("jndi/name"). Also, narrowing the object seems to help in some ClassCastException situations, so I tend to do it as a precaution. This is how I look up my EJBs:
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
public Object lookup (String jndiName, Class type) throws NamingException {
return PortableRemoteObject.narrow(InitialContext.doLookup(jndiName), type);
}
If using EJB 3.1, there is a way using #EJB(lookup = "jndi/name"), but since I'm not using this version, I cannot guarantee that this works.

How to group test cases in TestNG?

I want to launch two browsers at a time with different parameters. I've written Test suite like below. but, it is launching 8 browsers at a time (As I've mentioned parallel='tests' it is launching browsers for all classes available in that test)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="2" name="Suite" parallel="tests">
<test name="Test1" preserve-order="true">
<parameter name="propertyFileName" value="Constants.properties"/>
<classes preserve-order="true">
<class name="com.test.TestCase1"/>
<class name="com.test.TestCase2"/>
<class name="com.test.TestCase3"/>
<class name="com.test.TestCase4"/>
</classes>
</test> <!-- Test -->
<test name="Test2" preserve-order="true">
<parameter name="propertyFileName" value="Constants2.properties"/>
<classes preserve-order="true">
<class name="com.test.TestCase5"/>
<class name="com.test.TestCase6"/>
<class name="com.test.TestCase7"/>
<class name="com.test.TestCase8"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Is there any way to launch only two browsers at a time ?
I want to group Test cases such that while running in parallel (only two test cases at a time) both should take Constants from different property file.
EDIT-I
TestCase1.java
public class TestCase1
{
private WebDriver driver;
CommonMethods comObj;
StringBuffer failureMsgs;
#Parameters({"propertyFileName"})
#BeforeTest
public void beforeTest(String pname) throws Exception
{
comObj=new CommonMethods(pname);
driver=new FirefoxDriver();
comObj.login(driver, comObj.userName,comObj.password);
}
#Test
public void f()
{
try
{
}
catch(Exception e)
{
e.printStackTrace();
}
}
#AfterTest
public void afterTest()
{
System.out.println("Inside after method");
driver.quit();
}
}
The problem is with your setup and tear down code. You are using #Aftertest and before test. Your browsers would only shutdown when aftertest runs which would be only twice. Either you should use #afterclass or #aftermethod and corresponding beforemethods. testng is workin correctly coz it runs all your before test methods at the start, thus launching 8 browsers first.

PHPUnit Listener loaded but unused

I'm using PHPUnit 3.4.14 and I'm trying to add a Listener.
I wrote a simple one:
class My_Test_Listener implements PHPUnit_Framework_TestListener
{
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
{
...
I declared it in my phpunit.xml file:
<phpunit bootstrap="./bootstrap.php">
<testsuites>
<testsuite name="auth">
<directory>./library/Ademe/Auth</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="Listener" file="./library/My/Test/Listener.php">
</listener>
</listeners>
</phpunit>
My class is loaded (if I omit to implement one of the method, it says so in the logs), but I never go inside thoses methods. I tried this for instance :
public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
{
die('startTestSuite');
}
Do you have any idea of what could be missing?
Thanks!
OK I got it, the class name was wrong, allthough no error was reported. I should have done this instead:
<phpunit bootstrap="./bootstrap.php">
<testsuites>
<testsuite name="auth">
<directory>./library/Ademe/Auth</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="My_Test_Listener" file="./library/My/Test/Listener.php">
</listener>
</listeners>
</phpunit>
I had the same error but the listener worked only when I used
<listener class="\Name\Space\MyTestListeners" ></listener>

Resources