Embrace Dreams
Archive for the ‘Uncategorized’ Category
It’s mine, existed
January 12th, 2012 | admin
Fully isolated tests in Symfony2
January 12th, 2012 | admin
The most important thing you should provide with your re-usable bundles is unit tests set. Lately I solved two major cases which Symfony2 hasn’t got out of the box: testing services, defined in Dependency Injection Container and running model tests with fixtures in fully isolated environment.
Testing services defined in DIC
Since DIC in Symfony2 is one of the most awesome solutions, you probably (so do I) want to reflect creating services in tests in the same manner as in tested code. So you probably wondering how to use container in the tests. For this purpose I created BaseTestCase, which all of my tests cases extends.
<?php
require_once(__DIR__ . "/../../../../app/AppKernel.php");
class BaseTestCase extends \PHPUnit_Framework_TestCase
{
protected $_container;
public function __construct()
{
$kernel = new \AppKernel("test", true);
$kernel->boot();
$this->_container = $kernel->getContainer();
parent::__construct();
}
protected function get($service)
{
return $this->_container->get($service);
}
}
Now you can simply retrieve services as $this->get(“service.name”) and invoke them (for testing purposes) in the same way, as you do it in production code (for example as in controlers)
Fully isolated model tests
Often we deal with testing code which works with database. For this purpose we should use Doctrine Fixtures Bundle and create some fixture classes. But we need to load those fixtures before every test, to make sure, that our tests are running in isolated environement. We can use ModelTestCase, like in the listing below.
<?php
require_once(__DIR__ . "/../../../../app/AppKernel.php");
class ModelTestCase extends \PHPUnit_Framework_TestCase
{
protected $_application;
public function setUp()
{
$kernel = new \AppKernel("test", true);
$kernel->boot();
$this->_application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
$this->_application->setAutoExit(false);
$this->runConsole("doctrine:schema:drop", array("--force" => true));
$this->runConsole("doctrine:schema:create");
$this->runConsole("cache:warmup");
$this->runConsole("doctrine:fixtures:load", array("--fixtures" => __DIR__ . "/../DataFixtures"));
}
protected function runConsole($command, Array $options = array())
{
$options["-e"] = "test";
$options["-q"] = null;
$options = array_merge($options, array('command' => $command));
return $this->_application->run(new \Symfony\Component\Console\Input\ArrayInput($options));
}
}
Last thing we need to do, is to setup sqlite in memory database in doctrine config (app/config/config_tes.yml):
doctrine:
dbal:
driver: pdo_sqlite
path: :memory:
memory: true
orm:
auto_generate_proxy_classes: true
auto_mapping: true
Now before every single test, in test case which extends ModelTestCase, whole database structure will be created into the sqlite memory databse, and all fixtures will be loaded.
This approach allows you to create test suites which can be run not only locally, but also in Continous Integration servers, like Bamboo or Hudson.
Some of concepts used in this post were borrowed from this blog post: http://www.elao.org/developpement/a-la-decouverte-de-symfony-2-tests-unitaires-sur-le-modele-phpunit-et-doctrine-2.html
Thanks to origin author.
Symfony2 Coding Standards
January 12th, 2012 | admin
Remember that the main advantage of standards is that every piece of code looks and feels familiar, it’s not about this or that being more readable.
Since a picture – or some code – is worth a thousand words, here’s a short example containing most features described below:
<!--?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com-->
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Acme;
class Foo
{
const SOME_CONST = 42;
private $foo;
/**
* @param string $dummy Some argument description
*/
public function __construct($dummy)
{
$this->foo = $this->transform($dummy);
}
/**
* @param string $dummy Some argument description
* @return string|null Transformed input
*/
private function transform($dummy)
{
if (true === $dummy) {
return;
}
if ('string' === $dummy) {
$dummy = substr($dummy, 0, 5);
}
return $dummy;
}
}
Structure
- Never use short tags ( closing tag;
- Indentation is done by steps of four spaces (tabs are never allowed);
- Use the linefeed character (0x0A) to end lines;
- Add a single space after each comma delimiter;
- Don’t put spaces after an opening parenthesis and before a closing one;
- Add a single space around operators (==, &&, …);
- Add a single space before the opening parenthesis of a control keyword (if, else, for, while, …);
- Add a blank line before return statements, unless the return is alone inside a statement-group (like an if statement);
- Don’t add trailing spaces at the end of lines;
- Use braces to indicate control structure body regardless of the number of statements it contains;
- Put braces on their own line for classes, methods, and functions declaration;
- Separate the conditional statements (if, else, …) and the opening brace with a single space and no blank line;
- Declare visibility explicitly for class, methods, and properties (usage of var is prohibited);
- Use lowercase PHP native typed constants: false, true, and null. The same goes for array();
- Use uppercase strings for constants with words separated with underscores;
- Define one class per file;
- Declare class properties before methods;
- Declare public methods first, then protected ones and finally private ones.
Naming Conventions
- Use camelCase, not underscores, for variable, function and method names;
- Use underscores for option, argument, parameter names;
- Use namespaces for all classes;
- Suffix interfaces with Interface;
- Use alphanumeric characters and underscores for file names;
- Don’t forget to look at the more verbose Conventions document for more subjective naming considerations.
Documentation
- Add PHPDoc blocks for all classes, methods, and functions;
- Omit the @return tag if the method does not return anything;
- The @package and @subpackage annotations are not used.
Put braces on their own line for classes, methods, and functions declaration;
New voice of voicens.
November 17th, 2011 | admin
After a long talking about voicens, we made the first plan of voicens project. Maybe God planed everything, I met this article written by a great programmer who is on the way to his own business.
Yes, production is very important, prototype is another important part, operation is kernel? Maybe not. I must share this story to my partners, then I must tell them, it’s time to share everything we need to share, because a good schedule is a good encourage.
All right, do it tomorrow morning.
Reload
November 16th, 2011 | admin
15 days ago, Haulyn Jason reload.
Today, marked.
error: Untracked working tree file ‘……[snip]/apps.php’ would be overwritten by merge. Aborting
November 16th, 2011 | admin
When I update files on my server via git, I got the following:
error: Untracked working tree file ‘……[snip]/apps.php’ would be overwritten by merge. Aborting
OK, I do not like it, it’s easy, $git clean -dqfx, now it works.
Do not forget to backup your repository if you do not have a safe .gitignore file.
Other way, just[code]$git checkout -f filename[/code]
More details, refer to git manual.
Active fcitx on fresh fedora 16.
November 16th, 2011 | admin
I am not familiar with Fedora on desktop, it’s very easy to use fcitx on Mandriva and Slackware, but I can not active it on my fresh fedora 16.
After some days, it works now.
First, #yun install fcitx
Then, #alternatives –config xinputrc, then choose fcitx
Do not forget to choose fcitx under KDE via Input Method Selector
I add the following code to /etc/profile, I do not know is it necessary.
export XMODIFIERS=”@im=fcitx”
export QT_IM_MODULE=fcitx
export GTK_IM_MODULE=fcitx
At last, it works well. Thanks.
不破不立,最美的秋日,最操蛋的日子
September 20th, 2011 | admin
最美的秋日,最有创造力的季节,最适合的温度,最操蛋的日子。
事情一件接着一件,我却不得不呆在一个相当于没有网络的地方,顺着这帮来认证的孙子们做什么ISO安全体系认证,就这样一年又一年地过去。
坑爷爷,生命随着时间流失,就像清水流入沙子,再也不会回来。
我快达到所有事情的忍耐极限了。
网络不可用,时间不集中,事情不隔离,事情无主次,目标不明确,而现在已经长期处于一周工作七天,一天工作十几个小时,这种状态,已经快达到所有事情忍耐的极限了,因为不是不努力,而是我们自己还在不断在自己有限的时间里,给自己找麻烦。
这么美丽的日子,也许有一天,我们的后辈会说:那个最完美的日子里,你们这帮死胖子在做什么啊,你们做出来了什么啊,你们怎么那么二啊。
Learn Javascript Again
July 23rd, 2011 | admin
5 years ago, I wrote my first AJAX application, it’s not hard. 5 years past, I find the world of Javascript has changed, JQuery, Mootools, YUI3 and many many very good front end framework.
I have to learn something more, something new development roads.The back end has no changed these years, IO, NIO, AIO, sync, async and so on. HTTP is everything, but the front end changed.
I find a good resource website
July 22nd, 2011 | admin
I like this website, I like the resource on it. Good boy.
Oh, poor girl, why your mobile is out of service?
July 17th, 2011 | admin
It’s a good weekend if I am not so tired, a good interview, a good girl, a good PHP intern. Then at the last of the interview, I confirmed the mobile and QQ account, she confirmed yes.
Oh, poor girl, after 12 hours, why your mobile is out of service? Even another 24 hours past, your mobile kept on no service. I have told you we will call you soon.
If you did these, how can I inform you to enroll tomorrow morning?
Do not forget to enroll tomorrow morning if you receive my email or check this post out.
Regards, poor girl.
Stick to one thing is really hard
July 15th, 2011 | admin
A day is simple, one thing is simple, but stick to one thing is really hard. Two years ago, I start my blog here, the plan is posting three blog posts per week at least, but at the moment, there are much fewer posts here, I didn’t stick to it.
I am developer of yunshang, then I write some more websites, but why I do not have any websites online? They all die.
Back to the history, I have many diary books, I wrote them for 5 years, it’s one thing, 5 years is not long, not short. When I decide to stick to one thing, I believe I can do it, but on internet, I didn’t make it. I have to constantly consider what’s wrong. I have a good idea, then I make it, why I do not constantly make it again and again? I need to deep in.
The best time is today, I need to come on to improve zhishang, even it’s not a good domain name.
Good luck, yunshang, I stick to make you better.
What is friends? They tell you.
July 8th, 2011 | admin
It’s special, do you like it?
July 8th, 2011 | admin
Some books
July 7th, 2011 | admin
I like reading. It’s is little part of my books. Regards, books.
Do it, if you have dream
May 20th, 2011 | admin
Who is it?
May 11th, 2011 | admin
You know, Ma yun!
Make Apache working with tomcat&Glassfish
May 4th, 2011 | admin
You have many choice, I have tried many ways on Domix.in. It’s not easy to learn how to make Apache&Tomcat&Glassfish to work fast.
These days I find a very good post http://rimuhosting.com/mod_jk2_and_mod_proxy_ajp.jsp, in order to mark it, I copy the post here, hope it’s useful.
Accessing Tomcat on Port 80: From example.com:8080 to example.com
By default Tomcat and JBoss run on port 8080. So your Tomcat urls look like http://example.com:8080/index.jsp.There are a few ways ways you can access Tomcat webapps on the regular port 80 so that you do not need the port number in the URL.
Apache, mod_proxy/mod_jk2, Tomcat: You can use Apache as the front end to all requests then forward certain URLs or virtual hosts to Tomcat. This option lets have some content served by Tomcat (e.g. a whole domain, or a certain directory on a domain) and other content (e.g. HTML, PHP pages, etc) served by Apache. If you are on a newer distro (e.g. FC5, Centos5 and Debian Etch) with Apache 2.2 you would use mod_proxy or if you had an older distro with Apache 2.0 (e.g. RHEL4, Debian Sarge) then you would configure the Apache mod_jk2 module.
Tomcat, iptables, Port 80: Another method you can use is iptables. iptables let you forward traffic received on one port (80) to another port (8080). This method bypasses the Apache webserver completely (even if it is running on port 80). This is the simple to setup. But you will not be able to use Apache (e.g. for webmail, using PHP apps, or apps like phpMyAdmin).
Tomcat, Root, Port 80: You can also set Tomcat to run on port 80. We do not recommend this method, since then Tomcat would need to run as a privileged user. Which has security implications.
Apache 2.2 mod_proxy
Often customers want to combine the use of JSP and Apache. For example, they may have some PHP scripts and some JSP programs they want to run. (And the Servlet engines cannot execute PHP scripts). Using Apache to handle your incoming requests also gives you more options for things like using SSL, error logs, and using .htaccess files.Apache 2 introduces the mod_proxy module. It is a standard module in most modern distros. This module pretty much deprecates the need for mod_jk2. Full documentation for mod_proxy is available.
To setup mod_proxy_ajp add something like the following inside of your Apache VirtualHost entries (e.g. in /etc/httpd/conf/httpd.conf under CentOS/Fedora, /etc/apache2/sites-enabled under Ubuntu/Debian):
ProxyPass /tomcat ajp://127.0.0.1:8009/
ProxyPassReverse /tomcat ajp://127.0.0.1:8009/
This will forward all requests to http://yourip/tomcat to tomcat’s ROOT webapp. Or you could use the following line to forward all requests to http://yourip/tomcat to tomcat’s jsp-examples webapp.ProxyPass /tomcat ajp://127.0.0.1:8009/jsp-examples
Run the following to enable mod-proxy-ajp and to change allow/deny setting toDeny from none # was all :a2enmod proxy proxy_ajp
sed –in-place ‘s/Deny from all/Deny from none # was all/’ /etc/apache2/mods-available/proxy.conf
Resolving “client denied by server configuration”
If you get an error in the Apache error log like client denied by server configuration: proxy:ajp://127.0.0.1:8009/tomcat then you may need to enable Proxying. e.g. on Ubuntu/Debian systems change the Proxy * setting from Deny all to Deny none in /etc/apache2/mods-enabled/proxy.conf:
AddDefaultCharset off
Order deny,allow
Deny from none
#Allow from .example.com
Also on Ubuntu/Debian systems, you may need to enable the specific proxy module for apache. Something like the shell commands bellow…a2enmod proxy_ajp
a2enmod proxy_http
Running JSP Through Apache: mod_jk2
mod_jk2 is an option on Apache setups prior to Apache 2.2 (e.g. RHEL4, Debian 3.1). It is not an option on newer setups (e.g. not on Centos5, FC5 or newer, Debian Etch, or Ubuntu 7.10). For the newer distros you would use mod_proxy instead.mod_jk2 intercepts Apache requests to certain URLs and forwards them to your Servlet engine. For example, it can hand off requests to certain “mount points” like /jsp to your Servlet engine. It can also hand off certain file types (e.g. *.jsp) to your Servlet engine.
mod_jk2 forwards the Apache requests to your Servlet engine via a socket using a protocol called ajp13. Both Tomcat and Jetty support the ajp13 protocol.
First Step: Serve Content on Default Servlet Port
The first step towards running your webapps with Apache as a front end, is to first get them working directly from your default servlet eninge port. 8080 is the default port for Tomcat (including JBoss/Tomcat) and Jetty. Taking this step before testing the mod_jk2 setup makes troubleshooting problems easier:Set up your web apps (you may use any of the installed Servlet engines, they all support mod_jk2).
Make sure your can browse your webapp using a URL like: http://yourip:8080/jsp/index.jsp. When using Apache as a front end, it is best to not use a ‘ROOT’ mounted webapp.
Do not run the iptables command to forward port 80 requests to the Servlet engine (since we want Apache to handle the incoming requests).
Test you can load your web app on http://yourip/jsp/index.jsp
Note: your Servlet container needs to listen for connections from Apache. The Tomcat and Jetty installations on RimuHosting VPSs are set up this way by default. You just need to make sure they are running (see the Tomcat and Jetty start up instructions above).Next Steps: Serving up your WebApp from Apache
RimuHosting have pre-installed the mod_jk2.so module binary for the Fedora, White Box Enterprise Linux 3 and recent Red Hat 9 distributions. (If your Red Hat VPS was setup prior to 2004, contact support for information on how to get the mod_jk2.so module).mod_jk2 is configured in the /etc/httpd/conf.d/mod_jk2.conf file.
This file includes a sample ‘Location’. It looks like this:
JkUriSet worker ajp13:localhost:8009
This Location tag will direct requests to http://yourdomain/jsp-examples to the jsp-examples webapp (if one exists) on your Servlet engine.If you want to run your own webapp instead, just edit this file and rename jsp-examples to your own webapp’s name. Alternatively you can add your own Location tags in /etc/httpd/conf/httpd.conf.
You can put the Location tag inside a VirtualHost tag if you want the Location to only work for a particular domain. Note: on some servers there seems to be a problem where the VirtualHost is ignored, and the Location is used across all domains. In this case you will need to setup a workers2.properties.
ROOT WebApps
If you want to have all requests go to Tomcat, use a Location of / and your requests will go to the Tomcat ROOT webapp.If you want to mix and match Apache and Servlet engine requests for the domain, you may need to change the Location tag. For example, set it to /*.jsp and /*.do. This will make it so only the Servlet related requests go to the Servlet engine.
Tomcat Host Directive: When Each Domain Has Their Own Root Webapp
Do you have more than one domain and you want each domain to use a different root webapp? Then you will need to use Tomcat Host directives so that Tomcat knows which webapp to serve for which domain.Setup Apache VirtualHosts. Then add a Location tag inside each of the VirtualHost directives in /etc/httpd/conf/httpd.conf, e.g.
ServerName yourdomain.com
ServerAlias *.yourdomain.com
JkUriSet worker ajp13:localhost:8009
In your server.xml file (usually /usr/local/tomcat/conf/server.xml) add something like:
appBase="/usr/local/tomcat/webapps">
www.your2nddomain.com
These directives setup the default webapps for each of the virtual hosts.Then restart Apache and Tomcat.
Note: The server.xml changes are only required if you want to run multiple virtual hosts with different ROOT webapps. If you just have multiple virtual hosts with uniquely named webapps, they are not required. Just use the regular Location tags and make sure the webapp name and the Location directory match.
workers2.properties: When Location Tags Inside VirtualHost Directives Do Not Work
On some versions of mod_jk2 and Apache we have noticed a problem where Apache ignores the VirtualHost that a Location tag is nested within. For example, a Location / inside one VirtualHost ends up applying to all VirtualHosts on the server. So that all requests go to Tomcat, not just the requests for the intended domain.To workaround this problem, remove your Location directives. Create a /etc/httpd/conf/workers2.properties file. Then add directives like:
[uri:yourdomain.com:80/*]
worker=ajp13:localhost:8009
[uri:your2nddomain.com:80/*]
worker=ajp13:localhost:8009
These directives will pass the requests to the named virtual hosts to your Java server. If you have yourdomain.com and www.yourdomain.com you’ll need to add both those [uri] directives for each.jk2_init Errors
Getting something like this in your apache error_log file?[Mon Feb 04 15:36:04 2008] [error] jk2_init() Can’t find child 9744 in none of the 256 scoreboard slots
We have seen these errors before. They do not appear to cause any problems. And according to our googling can just be ignored.IPTables: Forwarding Incoming Port 80 requests to Port 8080
If you want to use Apache as a front end to Tomcat, skip this section.If you want to use iptables to forward incoming requests on port 80 to Tomcat on port 8080, run these commands to setup an iptable rule:
First, stop and disable apache. Under Centos or Fedora try the following…
# prevent Apache from running on startup
chkconfig –del httpd
# stop Apache from running right now
/etc/init.d/httpd stop
… or if you have Debian/Ubuntu based systems try …# prevent Apache from running on startup
update-rc.d -f apache2 remove
# stop Apache from running right now
/etc/init.d/apache2 stop
… then set the redirection up…# install the iptables applications if they are not already there
apt-get -y install iptables
# tell iptables to forward incoming requests on port 80 to tomcat
/sbin/iptables -t nat -I PREROUTING -p tcp –dport 80 -j REDIRECT –to-port 8080
For RedHat/Fedora/CentOS based distros (if there is a file called /etc/redhat-release) use this:# save the iptable rules
/sbin/iptables-save > /etc/sysconfig/iptables
# make sure iptables starts up by default after a server restart
chkconfig –level 35 iptables on
For Debian based distros (if there is a file called /etc/debian_version) use this:# save the iptable rules
/sbin/iptables-save > /etc/firewall.conf
# create a script so ifupdown loads these rules on boot
echo ‘#!/bin/sh’ > /etc/network/if-up.d/iptables
echo “iptables-restore < /etc/firewall.conf" >> /etc/network/if-up.d/iptables
chmod +x /etc/network/if-up.d/iptables
You may get a QM_MODULES warning. You can safely ignore that. Our VPS servers are built without modules and with the iptable features built into kernel itself. The iptables scripts just do not detect that modules are not available and do not know to skip that step.If you decide to turn off your iptables rules, run this:
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
iptables -t nat -F
For RedHat/Fedora/CentOS based distros (if there is a file called /etc/redhat-release) use this:# save the iptable rules
/sbin/iptables-save > /etc/sysconfig/iptables
# make sure iptables starts up by default after a server restart
chkconfig –level 35 iptables off
For Debian based distros (if there is a file called /etc/debian_version) use this:# save the iptable rules
/sbin/iptables-save > /etc/firewall.conf
Maintaining Java Sessions through an Apache Proxy
If your application makes use of Java Sessions, you may need to tweak the Cookie Path attribute to make it work as expected. A common problem this might fix is when attempts to log in appear successful, but require you to log in again immediately.The problem is that Java is sending a cookie path for a certain location, but Apache is configured to serve the application at a different location, so the browser receives the session cookie, but never sends it back because it never requests a page that matches the cookies location.
The original Set-Cookie header looks like this in the HTTP response
Set-Cookie: JSESSIONID=TOMCAT_SESSION_ID_HERE; Path=/myapp
If your application is not accessible at /myapp though, it won’t ever send that cookie back in a request, so you don’t appear to have working sessions. The solution is simple, but requires Apache 2.2 which introduced the ProxyPassReverseCookiePath setting with mod_proxy. Add this line to your Apache configuration to have Apache rewrite the Path attribute in the Set-Cookie header:ProxyPass / ajp://127.0.0.1:8009/myapp/
ProxyPassReverse / ajp://127.0.0.1:8009/myapp/
ProxyPassReverseCookiePath /myapp /
That will change the path attribute in the Set-Cookie response header to this:Set-Cookie: JSESSIONID=TOMCAT_SESSION_ID_HERE; Path=/
And now your browser will send the cookie back on requests to your application.
Razy, the dancing man.
May 3rd, 2011 | admin
You was brilliant,Oh, Razy.
You know, you lost some part of your life, but you have your own world
May 1st, 2011 | admin
You know, you lost some part of your life, but you have your own world, you can just hit a restart, then just start from new life. Do not care about others, they do not care about us. If you are a man like us in the special Chinese world, please look at the video, you can call it suck baidu.com, but it’s really world for individual person.











































