Skip to content

Instantly share code, notes, and snippets.

Hưng Trịnh hungtrinh

  • Ha Noi, Viet Nam
Block or report user

Report or block hungtrinh

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@hungtrinh
hungtrinh / discountRateByMembershipType.php
Created Nov 3, 2019
Final Production Code (Testable)
View discountRateByMembershipType.php
<?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 Nov 3, 2019
minimal production code pass business rule for 'platinum', 'gold' customer
View discountRateByMembershipType.php
<?php
function discountRateByMembershipType ($membershipType) {
if ('platinum' === $membershipType) return 0.15;
if ('gold' === $membershipType) return 0.1;
}
@hungtrinh
hungtrinh / testSuiteDiscountRateByMembershipType.php
Last active Nov 3, 2019
second specification, second requirement, business rule for gold customer
View testSuiteDiscountRateByMembershipType.php
<?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 Nov 3, 2019
minimal production code for first requirement
View discountRateByMembershipType.php
<?php
function discountRateByMembershipType ($membershipType) {
if ('platinum' === $membershipType) return 0.15;
}
@hungtrinh
hungtrinh / test_with_platinum_memeber_will_discount_fifteen_percent.php
Last active Nov 3, 2019
First specification, first unittest coe, first consume code
View test_with_platinum_memeber_will_discount_fifteen_percent.php
<?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 Nov 3, 2019
Write Production Without Test
View discountRateByMembershipType.php
<?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 Jul 24, 2019 — forked from SimonEast/Export CSV.php
PHP Example: Stream a CSV File to Browser with GZIP Compression (exporting from MySQL/PDO)
View Export CSV.php
<?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 Jun 20, 2019 — forked from akrabat/CorsMiddlewareFactory.php
Expressive factory to use CORS middleware
View CorsMiddlewareFactory.php
<?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;
@hungtrinh
hungtrinh / index.jsx
Created Mar 15, 2019 — forked from avinmathew/index.jsx
Multiple layouts with React Router v4
View index.jsx
import React from "react"
import { Route, Switch } from "react-router-dom"
const AppRoute = ({ component: Component, layout: Layout, ...rest }) => (
<Route {...rest} render={props => (
<Layout>
<Component {...props} />
</Layout>
)} />
)
@hungtrinh
hungtrinh / introrx.md
Created Feb 26, 2019 — forked from staltz/introrx.md
The introduction to Reactive Programming you've been missing
View introrx.md
You can’t perform that action at this time.