Skip to content

Commit

Permalink
Merge pull request #5 from bartoszkubicki/session-manager-stub
Browse files Browse the repository at this point in the history
Implementing stub for session manager
  • Loading branch information
bartoszkubicki authored Nov 23, 2019
2 parents 0f4dccc + 062a3f3 commit dc5885c
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### 1.1.2 ###
* implementing stub for `Magento\Framework\Session\SessionManagerInterface`
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# BKubicki Magento 2 Unit Tests Doubles


## Overview
Library provides useful doubles of different kinds for unit testing of Magento 2.3+. Some of doubles can be created using
dedicated builders, which helps in configuring desired behavior of doubles.
By using these doubles you can get rid of over-mocking in unit tests and time spent on writing unit should decrease.
It is can be achieved because of the fact that all doubles inherits or implements replaced object or interface,
so type consistency is preserved. Examples in [here](EXAMPLES.md).


## Prerequisites
PHP 7.2
* PHP 7.2


## Installation ###

Expand All @@ -17,25 +20,30 @@ To install the extension use the following commands:
```bash
composer require bkubicki/magento2-unit-tests-doubles
```


## Tests ##


### Unit ###
1. Run command
```
./vendor/bin/phpunit --colors phpunit.xml --testsuite "Unit" --coverage-html coverage/coverage-html
./vendor/bin/phpunit -c phpunit.xml --testsuite "Unit" --coverage-html coverage/coverage-html --colors=always
```

2. You can also use some alias:
- `test-unit-coverage` - _`vendor/bin/phpunit -c phpunit.xml --testsuite 'Unit' --coverage-html coverage/coverage-html --colors=always`_
- `test-unit-coverage` - _`vendor/bin/phpunit -c phpunit.xml --testsuite "Unit" --coverage-html coverage/coverage-html --colors=always`_


### Integration
1. Run command
```
./vendor/bin/phpunit -c phpunit.xml --testsuite "Integration"
./vendor/bin/phpunit -c phpunit.xml --testsuite "Integration" --colors=always
```

2.You can also use alias:
- `test-integration` - _`vendor/bin/phpunit -c phpunit.xml --testsuite 'Integration' --colors=always`_


### Infection tests ###

Expand Down Expand Up @@ -63,11 +71,16 @@ To install the extension use the following commands:
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/bartoszkubicki/background-process-screen/tags).
## Changelog
See changelog [here](CHANGELOG.md).
## Authors
* [Bartosz Kubicki](https://github.com/bartoszkubicki)
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
This project is licensed under the MIT License - see the [LICENSE](LICENSE.md) file for details.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"email": "[email protected]"
}
],
"version": "1.1.1",
"version": "1.1.2",
"minimum-stability": "stable",
"require": {
"php": "^7.2.0",
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<?xml version="1.0" ?>
<!--
@author Bartosz Kubicki [email protected]>
Github: https://github.com/bartoszkubicki
Expand Down
1 change: 0 additions & 1 deletion registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* Github: https://github.com/bartoszkubicki
*/


use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
Expand Down
252 changes: 252 additions & 0 deletions src/Framework/Session/SessionManagerStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
<?php

declare(strict_types=1);

/**
* File: SessionManagerStub.php
*
* @author Bartosz Kubicki [email protected]>
* @copyright Copyright (C) 2019 Lizard Media (http://lizardmedia.pl)
*/

namespace BKubicki\Magento2TestDoubles\Framework\Session;

use Exception;
use InvalidArgumentException;
use Magento\Framework\DataObject;
use Magento\Framework\Session\SessionManagerInterface;

/**
* Class SessionManagerStub
* @package BKubicki\Magento2TestDoubles\Framework\Session
* @codeCoverageIgnore
*/
class SessionManagerStub implements SessionManagerInterface
{
/**
* @var null|string
*/
private $sessionId;

/**
* @var null|string
*/
private $sessionName = '';

/**
* @var array
*/
private $storage;

/**
* SessionManagerStub constructor.
* @param DataObject $storage
*/
public function __construct(DataObject $storage = null)
{
if (!$storage instanceof DataObject) {
$storage = new DataObject();
}

$this->storage = $storage;
}

/**
* @return SessionManagerStub
* @throws Exception
*/
public function start(): self
{
$this->sessionId = $this->generateSid();
return $this;
}

/**
* @return void
*/
public function writeClose(): void
{
}

/**
* @return bool
*/
public function isSessionExists(): bool
{
return !empty($this->sessionId);
}

/**
* @return string
*/
public function getSessionId(): string
{
return (string) $this->sessionId;
}

/**
* @return string
*/
public function getName(): string
{
return (string) $this->sessionName;
}

/**
* @param string $name
* @return SessionManagerStub
*/
public function setName($name): self
{
$this->sessionName = $name;
return $this;
}

/**
* @param array|null $options
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function destroy(array $options = null): void
{
$this->storage->unsetData();
}

/**
* @return SessionManagerStub
*/
public function clearStorage(): self
{
$this->storage->unsetData();
return $this;
}

/**
* @return string
*/
public function getCookieDomain(): string
{
return 'some_domain';
}

/**
* @return string
*/
public function getCookiePath(): string
{
return 'some_path';
}

/**
* @return int
*/
public function getCookieLifetime(): int
{
try {
return random_int(2000, 2020);
} catch (Exception $exception) {
return 86400;
}
}

/**
* @param string|null $sessionId
* @return SessionManagerStub
*/
public function setSessionId($sessionId): self
{
$this->sessionId = (string) $sessionId;
return $this;
}

/**
* @return SessionManagerStub
*/
public function regenerateId(): self
{
return $this;
}

/**
* @return void
*/
public function expireSessionCookie(): void
{
}

/**
* @param string $urlHost
* @return string
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function getSessionIdForHost($urlHost): string
{
return (string) $this->sessionId;
}

/**
* @param string $host
* @return bool
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function isValidForHost($host): bool
{
return true;
}

/**
* @param string $path
* @return bool|void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function isValidForPath($path): bool
{
return true;
}

/**
* @param $method
* @param $args
* @return SessionManagerStub|mixed
*/
public function __call($method, $args)
{
if (!in_array(substr($method, 0, 3), ['get', 'set', 'uns', 'has'])) {
throw new InvalidArgumentException(
sprintf(
'Invalid method %s::%s(%s)',
get_class($this),
$method,
print_r($args, 1)
)
);
}

$return = call_user_func_array([$this->storage, $method], $args);
return $return === $this->storage ? $this : $return;
}

/**
* @param string $key
* @param bool $clear
* @return mixed
*/
public function getData($key = '', $clear = false)
{
$data = $this->storage->getData($key);

if ($clear && isset($data)) {
$this->storage->unsetData($key);
}

return $data;
}

/**
* @return string
*/
private function generateSid(): string
{
return (string) uniqid('session', true);
}
}

0 comments on commit dc5885c

Please sign in to comment.