Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fkupper/private-sniffer-module
Simple module that enables sniffing into private defined elements of an object while running unit tests with Codeception.
https://github.com/fkupper/private-sniffer-module
codeception-module unit-testing
Last synced: about 6 hours ago
JSON representation
Simple module that enables sniffing into private defined elements of an object while running unit tests with Codeception.
- Host: GitHub
- URL: https://github.com/fkupper/private-sniffer-module
- Owner: fkupper
- Created: 2019-07-11T13:46:16.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-08-12T15:27:46.000Z (about 4 years ago)
- Last Synced: 2024-09-18T18:09:43.092Z (about 2 months ago)
- Topics: codeception-module, unit-testing
- Language: PHP
- Size: 4.88 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Private Sniffer
[![Total Downloads](https://poser.pugx.org/fkupper/private-sniffer-module/downloads)](//packagist.org/packages/fkupper/private-sniffer-module)
[![Monthly Downloads](https://poser.pugx.org/fkupper/private-sniffer-module/d/monthly)](//packagist.org/packages/fkupper/private-sniffer-module)Simple Codeception module to check private elements of objects.
# Installation
Can be installed using composer:
```
composer require --dev fkupper/private-sniffer-module
```
# UsageSimple include the module on your unit suite config YML file:
``` yml
modules:
enabled: [PrivateSniffer]
```On your unit tests you can now inspect and test private attributes and methods:
``` php
class Foo
{
private $someInt = 1;private sum(int $a, int $b): int
{
return $a + $b;
}
}class TestFoo extends \Codeception\Test\Unit
{
/**
* @var \UnitTester
*/
protected $tester;// tests
public function testSum()
{
$foo = new Foo();// get the value of the private attribute $someInt
$someInt = $this->tester->getPrivatePropertyValue($foo, 'someInt');
$this->assertEquals(1, $someInt);// get a closure of the private method sum
$sum = $this->tester->getPrivateMethod($foo, 'sum');
$this->assertEquals(2 + 3, $sum(2, 3));
}
}
```
# Is this a solution to test bad/not testable code?Of course not. This is meant to be used when critical and sentive part of your code must be tested and is not possible for any reason.
Best practice still is to develop with unit testing in mind and refactor over time whatever you have that cannot be tested properly.