Skip to content

Instantly share code, notes, and snippets.

Avatar

Olle Härstedt olleharstedt

View GitHub Profile
@olleharstedt
olleharstedt / tmp6.php
Created Mar 2, 2022
One universal dry-run mock-spy AST evaluator to rule them all
View tmp6.php
<?php
namespace Tmp6;
use InvalidArgumentException;
interface Node
{
}
View edsl.php
<?php
/**
* EDSL = embedded domain-specific language
* The domain in this example is "side-effects", anything writing/reading to io, database, file, etc
*
* Using this EDSL, you neither need DI nor mocking to make the code testable. You can dry-run the particular
* parts needed of the "effectful" logic.
*
* Inspired by tagless-final pattern from FP. https://okmij.org/ftp/tagless-final/course/optimizations.html#primer
View que.md

Pre-req: Functional core, imperative shell

Pre-req: Side-effects, purity, referential transparency

  • The functional core is more testable and more composable than the imperative shell or effectful code
  • Extending the functional core means making a bigger part of the software pure
  • Side-effects can in some cases easily be lifted out from a function, creating a more composable and testable unit
  • But sometimes, side-effects are tangled in business logic
  • Some side-effects can be delayed until end of request (e.g., updating user's name in database; exception at failure can still be thrown)
  • Example: Create dummy users
@olleharstedt
olleharstedt / benchmark.js
Last active Dec 30, 2020
Clone benchmark
View benchmark.js
class Query {
constructor() {
this.select = '';
this.from = '';
this.where = '';
}
}
for (let i = 0; i < 1000000; i++) {
const q = new Query();
@olleharstedt
olleharstedt / amp_io.php
Created Aug 21, 2020
Mocking in Amphp, 2
View amp_io.php
<?php
require __DIR__ . '/../vendor/autoload.php';
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Request;
use Amp\Http\Client\HttpClient;
use Amp\Loop;
use Amp\ByteStream\ClosedException;
use Amp\File\File;
@olleharstedt
olleharstedt / WriteIO
Created Aug 21, 2020
Amphp and mocking
View WriteIO
<?php
require __DIR__ . '/../vendor/autoload.php';
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Request;
use Amp\Loop;
use Amp\ByteStream\ClosedException;
use Amp\File;
@olleharstedt
olleharstedt / tmp.php
Created Sep 5, 2019
socket to browser
View tmp.php
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (socket_bind($sock, '127.0.0.10', 888) === false) {
die('cannot bind' . PHP_EOL);
View tmp.php
<?php
$command = sprintf(
'echo -e "%s" | nc -U /var/run/docker.sock',
"GET /info HTTP/1.1\r\nHost: localhost\r\n\r\n"
);
echo '<pre>'; var_dump($command); echo '</pre>';
var_dump(exec($command));
View tmp.php
<?php
$command = sprintf(
'echo -e "%s" | nc -U /var/run/docker.sock',
"GET /info HTTP/1.1\r\nHost: localhost\r\n"
);
echo '<pre>'; var_dump($command); echo '</pre>';
var_dump(exec($command));
View gist:9b1f2362ea50b85439abddf05dd1a542
<?php
$command = sprintf(
'echo -e "%s" | nc -U /var/run/docker.sock',
"GET /info HTTP/1.1\r\nHost: localhost\r\n"
);
echo '<pre>'; var_dump($command); echo '</pre>';
var_dump(exec($command));