使用PHPUnit测试Facebook应用程序

使用PHPUnit测试Facebook应用程序

问题描述:

I am building a Facebook application with PHP and I would like to implement PHPUnit tests. The problem is, I think, with the Facebook authorization and getting the dynamic token. During testing the token cannot be received and my tests fail.

I read that this problem can be avoided using "mock objects" but my app is relying on the social graph and therefore this idea does not seem like a good option.

So I hope that someone could explain me how this can be done or point me to some other approach for automated Facebook apps testing.

Have a nice day :)

我正在使用PHP构建Facebook应用程序,我想实现PHPUnit测试。 我认为问题在于Facebook授权并获得动态令牌。 在测试期间,无法接收令牌并且我的测试失败。 p>

我读到使用“模拟对象”可以避免此问题,但我的应用依赖于社交图,因此这个想法确实存在 看起来不是一个好选择。 p>

所以我希望有人可以解释一下如何做到这一点,或者指出我采用其他方法进行自动Facebook应用程序测试。 p>

祝你有个愉快的一天:) p> div>

I read that this problem can be avoided using "mock objects" but my app is relying on the social graph and therefore this idea does not seem like a good option.

One question I would ask here is how do you expect to deterministically (and quickly!) test your application based on different structures of the social graph? I would recommend doing the legwork to create an internal representation of the social graph in your application so your business logic will work against that representation instead of talking directly to FB. Then you would have a separate component that would generate this internal representation of the graph via the Facebook APIs.

Your tests could then construct any graph of their choosing using your internal representation and Facebook is now totally out of the picture for you to accomplish your original goal to verify your app's logic.

If you don't want to do this legwork, the value of writing automated tests against the live system is limited as it will be difficult to investigate test failures and tests of these nature tend to be very brittle (i.e. they can fail because of configuration errors, network errors, etc)

If you want to actually use Facebook (as opposed to a mock object), you should think about the authentication transaction as handled by your browser, and then implement that same path in your unit test.

For example, when you make an http request to https://www.facebook.com/dialog/oauth?client_id=... , if all goes well, you should receive a 302 (redirect) response, which will contain a Location header. You can then follow the Location header, etc, continuing down the path, until you get to the page you are looking for. You can do all this in your server code using a socket, or a library like cURL.

By using mock data, you keep everything constant except for the code under test. By using the live social graph data, you are testing both your code and Facebook's API at the same time.

If your unit test fails, how do you know whether it's your code or Facebook's code that is failing?

The only way that you could do what you are describing is to set up a big complicated setup script that was loaded every time the tests run. The rule of unit testing - "If it's hard to test, it's badly coded".