Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions .idea/PHPUnitPlayground.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
## about
This is a project to play around with unit tests.

## articles about unit tests
## articles/discussions about unit tests

- [Steve Sanderson - Writing Great Unit Tests: Best and Worst Practices](http://blog.stevensanderson.com/2009/08/24/writing-great-unit-tests-best-and-worst-practises/)


## how unit tests impact code


### depencency injection

- [replacing methods in class to test](https://stackoverflow.com/questions/11399600/is-it-possible-to-phpunit-mock-object-to-replace-one-created-in-class)
- [replacing objects in class to test](https://stackoverflow.com/questions/279493/phpunit-avoid-constructor-arguments-for-mock)


### static

- [static methods should be avoided](https://stackoverflow.com/questions/34380247/php-how-to-make-testable-static-methods)
- [dependency injection](https://stackoverflow.com/questions/11399600/is-it-possible-to-phpunit-mock-object-to-replace-one-created-in-class)

## Frameworks
## useful frameworks

- [PHP dependency injection container](http://php-di.org/)

24 changes: 12 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions src/Sample3/A.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Created by PhpStorm.
* User: frankfleige
* Date: 17.07.17
* Time: 11:29
*/

namespace FrankFleige\PHPUnitPlayground\Sample3;

/**
* Class A
* @package FrankFleige\PHPUnitPlayground\Sample3
*/
class A implements Sample3
{
/**
* array with keys of entitlements
* @var string[]
*/
private $entitlements;

/**
* Constructor.
*/
public function __construct()
{
$this->entitlements = [];
$this->initEntitlements();
}

/**
* will init the entitlement keys
*/
private function initEntitlements()
{
$this->entitlements = $this->getConfigurationRepository()->getConfig('entitlements');
}

/**
* will get an instance of the configuration repository
* @return C
*/
public function getConfigurationRepository()
{
return C::getInstance();
}

/**
* will return the keys of all entitlements
* @return string[]
*/
public function getEntitlements()
{
return $this->entitlements;
}

/**
* will set the internal entitlements storage
* @param string[] $entitlements array with entitlement keys
*/
public function setEntitlements($entitlements)
{
$this->entitlements = $entitlements;
}

/**
* checks if the given key is within the list of entitlements
* @param string $key
* @return boolean
*/
public function isEntitledTo($key)
{
return in_array($key, $this->entitlements);
}
}
75 changes: 75 additions & 0 deletions src/Sample3/A1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Created by PhpStorm.
* User: frankfleige
* Date: 17.07.17
* Time: 11:29
*/

namespace FrankFleige\PHPUnitPlayground\Sample3;

/**
* Class A1
* @package FrankFleige\PHPUnitPlayground\Sample3
*/
class A1 implements Sample3
{
/**
* array with keys of entitlements
* @var string[]
*/
private $entitlements;

/**
* Constructor.
*/
public function __construct()
{
$this->entitlements = [];
}

/**
* will init the entitlement keys
*/
public function initEntitlements()
{
$this->entitlements = $this->getConfigurationRepository()->getConfig('entitlements');
}

/**
* will get an instance of the configuration repository
* @return C
*/
public function getConfigurationRepository()
{
return C::getInstance();
}

/**
* will return the keys of all entitlements
* @return string[]
*/
public function getEntitlements()
{
return $this->entitlements;
}

/**
* will set the internal entitlements storage
* @param string[] $entitlements array with entitlement keys
*/
public function setEntitlements($entitlements)
{
$this->entitlements = $entitlements;
}

/**
* checks if the given key is within the list of entitlements
* @param string $key
* @return boolean
*/
public function isEntitledTo($key)
{
return in_array($key, $this->entitlements);
}
}
67 changes: 67 additions & 0 deletions src/Sample3/B.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Created by PhpStorm.
* User: frankfleige
* Date: 17.07.17
* Time: 11:30
*/

namespace FrankFleige\PHPUnitPlayground\Sample3;

/**
* Class B
* @package FrankFleige\PHPUnitPlayground\Sample3
*/
class B implements Sample3
{
/**
* instance of the configuration repository
* @var C
* @Inject
*/
private $configurationRepository;

/**
* internal storage of entitlement keys
* @var string[]
*/
private $entitlements;

/**
* B constructor.
* @param C $cr instance of the configuration repository
*/
public function __construct(C $cr)
{
$this->configurationRepository = $cr;
$this->entitlements = [];
$this->initEntitlements();
}

/**
* will init the entitlement keys
*/
private function initEntitlements()
{
$this->entitlements = $this->configurationRepository->getConfig('entitlements');
}

/**
* will return the keys of all entitlements
* @return string[]
*/
public function getEntitlements()
{
return $this->entitlements;
}

/**
* checks if the given key is within the list of entitlements
* @param string $key
* @return boolean
*/
public function isEntitledTo($key)
{
return in_array($key, $this->entitlements);
}
}
Loading