Arbitrary tweets made by TheGingerDog up to 27 January 2019
Continue reading “Automated twitter compilation up to 27 January 2019”
Linux, PHP, geeky stuff … boring man.
Arbitrary tweets made by TheGingerDog up to 27 January 2019
Continue reading “Automated twitter compilation up to 27 January 2019”
Arbitrary tweets made by TheGingerDog up to 20 January 2019
Continue reading “Automated twitter compilation up to 20 January 2019”
Arbitrary tweets made by TheGingerDog up to 13 January 2019
Continue reading “Automated twitter compilation up to 13 January 2019”
[This is a relatively old post I think I forgot to publish….]
Previously, I’d only used PHPUnit’s mock implementation; however lately I’ve been exposed to Mockery.
While they both achieve broadly the same result (at least from my point of view), here’s an example of how to mock dependencies with Mockery.
Class to test:
class TestMe { private $db; public function __construct(PDO $db) { $this->db = $db; } public function doesSomethingImportant(array $data) : bool { $stmt = $this->db->prepare("UPDATE my_table SET field2 = :field2 WHERE field1 = :field1"); return $stmt->execute(['field1' => $data['field1'], 'field2' => $data['field2']); } }
And to test ….
.... use Mockery as m; class MyTest extends \PHPUnit\Framework\TestCase { public function tearDown() { m::close(); parent::tearDown(); } public function testDoesSomethingImpotant() { $fakePdo = m::mock(\PDO::class); $stmt = m::mock(\PDOStatement::class); $stmt ->shouldReceive('execute') ->withArgs(['field1' => 'hello', 'field2' => 'world']) ->once() ->andReturn(true); $fakePdo ->shouldReceive('prepare') ->withArgs(['UPDATE my table SET field2 = :field2 WHERE field1 = :field1']) ->once() ->andReturn($stmt); $testClass = new TestMe($fakePdo); $this->assertTrue( $testClass->updateSomething(['field1' => 'hello', 'field2' => 'world']); } }
So that’s all well and good, and with a little imagination you can see how a method that does some calculation could be tested to ensure it does the calculation correctly and performs the appropriate database query. It obviously requires you inject all dependencies in to the class (else you can’t pass in the appropriate mocks!)
While this test is isolated from the underlying database, it doesn’t ensure you code will work – what if someone’s changed the database schema – your test will still (incorrectly) pass …
You can also create ‘fake’ errors throughout your code which might help give you a higher code coverage score 🙂
$pdo = m::mock(PDO::class); $pdo->shouldReceive('prepare') ->withArgs(['bad sql']) ->andThrow(PDOException::class); $pdo->shouldReceive('prepare') ->withArgs(['whatever good sql']) ->once() ->andReturn($stmt); $pdo->shouldReceive('prepare') ->withArgs(['more good sql']) ->once() ->andReturn($stmt); ....
So, my 2009 MacBook Pro decided to slowly die … and after dithering for about 3 years over what to buy to replace it …. I chose a Google Pixelbook (i7 variant, 500Gb NVME disk etc) (via eBay).
Here are some findings …
I’m toying with the idea of replacing ChromeOS with a native Linux install; but I’ve not yet seen enough evidence to suggest that it’ll work well.
Hopefully the Campfire project will have a release soon …. Until then I’ll be watching https://www.reddit.com/r/pixelbook etc
Arbitrary tweets made by TheGingerDog up to 06 January 2019
Continue reading “Automated twitter compilation up to 06 January 2019”
Arbitrary tweets made by TheGingerDog up to 30 December 2018
Continue reading “Automated twitter compilation up to 30 December 2018”
Arbitrary tweets made by TheGingerDog up to 23 December 2018
Continue reading “Automated twitter compilation up to 23 December 2018”
Arbitrary tweets made by TheGingerDog up to 16 December 2018
Continue reading “Automated twitter compilation up to 16 December 2018”
Arbitrary tweets made by TheGingerDog up to 09 December 2018
Continue reading “Automated twitter compilation up to 09 December 2018”