Skip to content

Instantly share code, notes, and snippets.

View hungtrinh's full-sized avatar

Hưng Trịnh hungtrinh

  • Ha Noi, Viet Nam
  • 10:30 (UTC +07:00)
View GitHub Profile
@hungtrinh
hungtrinh / composer.md
Created May 21, 2020 14:56 — forked from xeoncross/composer.md
How composer actually works

Composer

the missing quick-start guide

We will assume we have a package/project called https://github.com/foo/bar

Most redistributable packages are hosted on a version control website such as Github or Bitbucket. Version control repositories often have a tagging system where we can define stable versions of our application. For example with git we can use the command:

git tag -a 1.0.0 -m 'First version.'

With this we have created version 1.0.0 of our application. This is a stable release which people can depend on. If we wanted to include "foo/bar" then we could create a composer.json and specify the tagged release we wanted:

@hungtrinh
hungtrinh / .gitlab-ci.yml
Created May 12, 2020 10:39 — forked from jlis/.gitlab-ci.yml
AWS ECS and ECR deployment via Docker and Gitlab CI
image: docker:latest
variables:
REPOSITORY_URL: <AWS ACCOUNT ID>.dkr.ecr.eu-central-1.amazonaws.com/<ECS REPOSITORY NAME>
REGION: eu-central-1
TASK_DEFINTION_NAME: <TASK DEFINITION NAME>
CLUSTER_NAME: <CLUSTER NAME>
SERVICE_NAME: <SERVICE NAME>
services:
@hungtrinh
hungtrinh / discountRateByMembershipType.php
Created November 3, 2019 16:00
Final Production Code (Testable)
<?php
function discountRateByMembershipType ($membershipType) {
if ('platinum' === $membershipType) return 0.15;
if ('gold' === $membershipType) return 0.1;
if ('silver' === $membershipType) return 0.05;
throw new UnexpectedValueException('invalid membership type');
}
@hungtrinh
hungtrinh / discountRateByMembershipType.php
Created November 3, 2019 15:30
minimal production code pass business rule for 'platinum', 'gold' customer
<?php
function discountRateByMembershipType ($membershipType) {
if ('platinum' === $membershipType) return 0.15;
if ('gold' === $membershipType) return 0.1;
}
@hungtrinh
hungtrinh / testSuiteDiscountRateByMembershipType.php
Last active November 3, 2019 15:21
second specification, second requirement, business rule for gold customer
<?php
function test_with_gold_member_will_discount_ten_percent() {
if ( 0.1 === discountRateByMembershipType('gold') {
echo 'Test Done';
} else {
echo 'Test Fail';
}
}
//... code test_with_platinum_member_will_discount_fifteen_percent here
@hungtrinh
hungtrinh / discountRateByMembershipType.php
Created November 3, 2019 15:05
minimal production code for first requirement
<?php
function discountRateByMembershipType ($membershipType) {
if ('platinum' === $membershipType) return 0.15;
}
@hungtrinh
hungtrinh / test_with_platinum_memeber_will_discount_fifteen_percent.php
Last active November 3, 2019 15:24
First specification, first unittest coe, first consume code
<?php
function test_with_platinum_member_will_discount_fifteen_percent() {
if ( 0.15 === discountRateByMembershipType('platinum') {
echo 'Test Done';
} else {
echo 'Test Fail';
}
}
@hungtrinh
hungtrinh / discountRateByMembershipType.php
Last active November 3, 2019 14:32
Write Production Without Test
<?php
function discountRateByMembershipType($userId) {
//$date = getCurrentDateFromLocalTimeOrRemoteTimeServer(); //with remote time server, we can't fake date
//if (is_not_black_friday($date) return 0; // we can't not test CORE BUSINESS if add this spec
$membershipType = getMemberShipTypeFromDatabaseOrRemoteApi($userId);
//CORE BUSINESS need coverage by unit test or manual test 
if ('platinum' === $membershipType) return 0.15;
if ('gold' === $membershipType) return 0.1;
if ('slive' === $membershipType) return 0.05;
throw new UnknowMembershipTypeException('invalid membership type');
@hungtrinh
hungtrinh / Export CSV.php
Created July 24, 2019 03:01 — forked from SimonEast/Export CSV.php
PHP Example: Stream a CSV File to Browser with GZIP Compression (exporting from MySQL/PDO)
<?php
/**
* This script performs a full dump of a database query into
* CSV format and pipes it directly to the browser.
*
* - YES, the browser will save the CSV file to disk
* - YES, it should support large files without using massive amounts of memory
* - YES, it compresses the request using GZIP to reduce download time
*/
@hungtrinh
hungtrinh / CorsMiddlewareFactory.php
Created June 20, 2019 02:41 — forked from akrabat/CorsMiddlewareFactory.php
Expressive factory to use CORS middleware
<?php declare(strict_types=1);
namespace App\Factory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Tuupola\Middleware\Cors;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\JsonResponse;
use Zend\ProblemDetails\ProblemDetailsResponseFactory;
use Zend\Stratigility\Middleware\CallableMiddlewareWrapper;