With Valentine’s Day quickly approaching, the team here at AYI (AreYouInterested.com®) surveyed more than 500 of our Facebook fans to find out what people will be up to. Wondering how likely you are to get dumped on Valentine’s Day or how many people will be logged on to a dating...
We have Engineering Summer Internships @ Tumblr! We’re looking for aspiring software engineers with a passion for open source software to join us for a summer of programming and fun. You will be integrated into a small engineering team working on a real-world project as part of
Product Engineering: Using PHP and JavaScript, create new and improve site features that keep our growing millions of users doing amazing things with their tumblelogs.
Search Engineering: Research, expand and refine Tumblr’s search infrastructure and search features.
Platform Engineering: Write highly optimized distributed services that manage data and requests in real time, helping our site scale to billions of posts.
Infrastructure Engineering: Work hands on with the Network team configuring, deploying and maintaining JunOS network devices.
Mobile Engineering: Come work on the application that’s putting Tumblr in millions of users’ pockets. A passion for IOS and Android is needed.
Polish your code samples, then send us your resume via our Engineering Summer Internship job page.
(We’re also hiring full time Engineers at every level of our technical stack. Learn more on Tumblr’s Jobs page).
How to get started with writing tests for your PHP code
You've finally decided to come to the light side and start testing your code. Great! So now you're sitting in front a piece of code that you want to cover with a test and have no clue how to start. First question to ask yourself when start testing a function is what are test cases for this function. So how to come up with these cases.
When it comes to testing, "divide and conquer" principle is a key to success. See what your function does, make sure it has as little side effects as possible, follow function's path as you were a compiler and make sure you create a test case for each statement. PHP allows multiple statements in a line but our standards don't so it's easy to decouple test cases from the function on lines basis. For the first example let's cover with tests a simple class Order.
class Order { private $parameters; public function __construct(array $row) { if (empty($row)) { throw new InvalidArgumentException('Cannot create an order from empty array'); } $this->parameters = $row; } public function __set($name, $value) { $this->parameters[$name] = $value; } public function __get($name) { if (array_key_exists($name, $this->parameters)) { return $this->parameters[$name]; } throw new InvalidArgumentException('Undefined property ' . $name); } }
So as a compiler we are moving from the top to the bottom. First method is a constructor. And the first test for the construtor is to check its main functionality. The purpose of constructors is to create an object. Asking ourselves "Is it returning an object?"
public function testConstructShouldReturnInstanceOfOrder() { $order = new Order(array('product' => 'apples', 'price' => 1.5,'quantity' => 10, 'amount' => 15)); $this->assertInstanceOf('Order', $order); }
Then what is the first statement of the constructor? To make sure we get parameters for the order. So we have to test this statement works and throws expected exception. There is more than one way to test exceptions in phpunit. You can use the @expectedException annotation to test whether an exception is thrown inside the tested code
/** * @expectedException InvalidArgumentException * @expectedExceptionMessage 'Cannot create an order from empty array' */ public function testConstructWithEmptyArrayShouldThrowException() { new Payments_Order(array()); }
Alternatively, you can use the setExpectedException() method to set the expected exception
public function testConstructWithEmptyArrayShouldThrowException() { $this->setExpectedException('InvalidArgumentException', 'Cannot create an order from empty array'); new Payments_Order(array()); }
You can also use the approach shown in the example below. If the code that is expected to raise an exception does not raise the expected exception, the subsequent call to fail() will halt the test and signal a problem with the test. If the expected exception is raised, the catch block will be executed, and the test will end successfully.
public function testConstructWithEmptyArrayShouldThrowException() { try { new Payments_Order(array()); } catch (InvalidArgumentException $expected) { $this->assertEquals('Cannot create an order from empty array', $expected->getMessage()); return; } $this->fail('An expected exception CANNOT_CREATE_ORDER has not been raised.'); }
Now when false statement is covered let's test successful creation of the order. According to the code of our constructor the next line after if-statement is setting parameters consequently our next test case is to check that parameters have been set.
public function testConstructShouldSetParameters() { $parameters = array('product' => 'apples', 'price' => 1.5,'quantity' => 10, 'amount' => 15); $order = new Payments_Order($parameters); foreach ($parameters as $key => $value) { $this->assertEquals($value, $order->$key); } }
Ok, but here we test only one set of parameters. What about edge cases, for example free products (price $0)? Sure, we can add more tests to make sure it works for different sets of parameters. Or we can use data providers to send arbitrary arguments to the same test method. The data provider method to be used is specified using the @dataProvider annotation. A data provider method must be public and either return an array of arrays or an object that implements the Iterator interface and yields an array for each iteration step. For each array that is part of the collection the test method will be called with the contents of the array as its arguments.
As you see an array that data provider returns can be an associative array which is helpful to see different test cases in phpunit output. Setting parameters was the last line of the constructor. We're done with the constructor! At least we've covered the most common use cases. Can you write tests for __set() and __get() by yourself now?
At Snap Interactive we love building things and deploying them for the whole world to see. This means we deploy lots of new code every single day. In fact we currently do around 20 deploys per day. Luckily we also love Continuous Integration, which is how we can do this without constant...
Three days and I finally were able to set it up on my mac. I tried tons different ways, installed tons different versions of everything, almost gave up and went to take a drink with a good friend of mine somewhere in the Cobble Hill, Brooklyn.
- Are you still use macports to install software? Try HomeBrew, - she advised
- Home what?
- HomeBrew is the new way to install everything on the mac
Well, I was too skeptic after all this time fighting with mysql and python but decided to give homebrew a try. I searched (can't say googled as I use duckduckgo and it's not the easiest word to create a verb) it and loved their ads "MacPorts driving you to drink? Try Homebrew!". This was so true. And I tried.
To test if it has been setup correctly try to start mysql server
mysql.server start
It didn't want to start for me until I rebooted the mac. After reboot it started successfully. I go to python shell to check that mysql connector is working