Skip to content

Instantly share code, notes, and snippets.

@amirbehzad
amirbehzad / phpunit_pdo_mock.php
Created January 21, 2016 04:18
How to mock PDO for test-cases written in PHPUnit
<?php
// ...
protected function getMockedPDO()
{
$query = $this->getMock('\PDOStatement');
$query->method('execute')->willReturn(true);
$db = $this->getMockBuilder('\PDO')
->disableOriginalConstructor()
@amirbehzad
amirbehzad / redis_mlpop.py
Last active September 28, 2020 16:34
How to LPOP multiple items from a Redis LIST in Python
# function to return multiple items (n) from a redis list (q) in an atomic manner
# as seen in: http://redis.io/commands/lpop#comment-431677622
def multi_pop(r, q, n):
p = r.pipeline()
p.multi()
p.lrange(q, 0, n - 1)
p.ltrim(q, n, -1)
return p.execute()
@amirbehzad
amirbehzad / is_empty_array.php
Last active December 30, 2015 03:58
PHP: How to ensure a variable is an Array, and it is not empty
<?php
// returns True if the given $var is an Array, and it is not empty;
// otherwise, False. It does not gives an error if the given $var
// is not an Array, however, it returns False.
function isNonEmptyArray($var) {
return !empty(@reset($var));
}