├── .bowerrc ├── .gitignore ├── LICENSE.md ├── README.md ├── assets └── AppAsset.php ├── bootstrap └── ContainerBootstrap.php ├── codeception.yml ├── commands └── HelloController.php ├── composer.json ├── composer.lock ├── config ├── .gitignore ├── console.php ├── params.php ├── test.php └── web.php ├── controllers └── SiteController.php ├── dispatchers ├── DummyEventDispatcher.php └── EventDispatcher.php ├── entities ├── AggregateRoot.php ├── Employee │ ├── Address.php │ ├── Employee.php │ ├── Events │ │ ├── EmployeeAddressChanged.php │ │ ├── EmployeeArchived.php │ │ ├── EmployeeCreated.php │ │ ├── EmployeePhoneAdded.php │ │ ├── EmployeePhoneRemoved.php │ │ ├── EmployeeReinstated.php │ │ ├── EmployeeRemoved.php │ │ └── EmployeeRenamed.php │ ├── Id.php │ ├── Name.php │ ├── Phone.php │ ├── Phones.php │ └── Status.php └── EventTrait.php ├── mail └── layouts │ └── html.php ├── models ├── ContactForm.php ├── LoginForm.php └── User.php ├── repositories ├── EmployeeRepository.php ├── MemoryEmployeeRepository.php └── NotFoundException.php ├── requirements.php ├── runtime └── .gitignore ├── services ├── EmployeeService.php └── dto │ ├── AddressDto.php │ ├── EmployeeArchiveDto.php │ ├── EmployeeCreateDto.php │ ├── EmployeeReinstateDto.php │ ├── NameDto.php │ └── PhoneDto.php ├── tests ├── _bootstrap.php ├── _data │ └── .gitignore ├── _output │ └── .gitignore ├── _support │ ├── AcceptanceTester.php │ ├── FunctionalTester.php │ └── UnitTester.php ├── acceptance.suite.yml.example ├── acceptance │ ├── AboutCest.php │ ├── ContactCest.php │ ├── HomeCest.php │ ├── LoginCest.php │ └── _bootstrap.php ├── bin │ ├── yii │ └── yii.bat ├── functional.suite.yml ├── functional │ ├── ContactFormCest.php │ ├── LoginFormCest.php │ └── _bootstrap.php ├── unit.suite.yml └── unit │ ├── _bootstrap.php │ ├── entities │ └── Employee │ │ ├── ArchiveTest.php │ │ ├── ChangeAddressTest.php │ │ ├── CreateTest.php │ │ ├── EmployeeBuilder.php │ │ ├── PhoneTest.php │ │ ├── ReinstateTest.php │ │ ├── RemoveTest.php │ │ └── RenameTest.php │ ├── models │ ├── ContactFormTest.php │ ├── LoginFormTest.php │ └── UserTest.php │ └── repositories │ ├── BaseRepositoryTest.php │ └── MemoryEmployeeRepositoryTest.php ├── views ├── layouts │ └── main.php └── site │ ├── about.php │ ├── contact.php │ ├── error.php │ ├── index.php │ └── login.php ├── web ├── assets │ └── .gitignore ├── css │ └── site.css ├── favicon.ico ├── index-test.php ├── index.php └── robots.txt ├── widgets └── Alert.php ├── yii └── yii.bat /.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory" : "vendor/bower-asset" 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # phpstorm project files 2 | .idea 3 | 4 | # netbeans project files 5 | nbproject 6 | 7 | # zend studio for eclipse project files 8 | .buildpath 9 | .project 10 | .settings 11 | 12 | # windows thumbnail cache 13 | Thumbs.db 14 | 15 | # composer vendor dir 16 | /vendor 17 | 18 | # composer itself is not needed 19 | composer.phar 20 | 21 | # Mac DS_Store Files 22 | .DS_Store 23 | 24 | # phpunit itself is not needed 25 | phpunit.phar 26 | # local phpunit config 27 | /phpunit.xml 28 | 29 | tests/_output/* 30 | tests/_support/_generated -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The BSD License (BSD) 2 | 3 | Copyright © 2017 by Dmitry Eliseev (ElisDN). All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions 7 | are met: 8 | 9 | * Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | * Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in 13 | the documentation and/or other materials provided with the 14 | distribution. 15 | * Neither the name of Yii Software LLC nor the names of its 16 | contributors may be used to endorse or promote products derived 17 | from this software without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 22 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 23 | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 24 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 25 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 | POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Yii 2 DDD Aggregates Demo 2 | ============================ 3 | 4 | Demonstration of domain entities and repositories. 5 | 6 | INSTALLATION 7 | ------------ 8 | 9 | You can then clone this project template using the following command: 10 | 11 | ~~~ 12 | git clone git@github.com:ElisDN/yii2-demo-aggregates project 13 | cd project 14 | composer install 15 | ~~~ 16 | 17 | CONFIGURATION 18 | ------------- 19 | 20 | Add the file `config/db.php` with real data, for example: 21 | 22 | ```php 23 | 'yii\db\Connection', 26 | 'dsn' => 'mysql:host=localhost;dbname=aggregates', 27 | 'username' => 'root', 28 | 'password' => '1234', 29 | 'charset' => 'utf8', 30 | ]; 31 | ``` 32 | 33 | Add the file `config/test_db.php` for test data, for example: 34 | 35 | ```php 36 | 16 | * @since 2.0 17 | */ 18 | class AppAsset extends AssetBundle 19 | { 20 | public $basePath = '@webroot'; 21 | public $baseUrl = '@web'; 22 | public $css = [ 23 | 'css/site.css', 24 | ]; 25 | public $js = [ 26 | ]; 27 | public $depends = [ 28 | 'yii\web\YiiAsset', 29 | 'yii\bootstrap\BootstrapAsset', 30 | ]; 31 | } 32 | -------------------------------------------------------------------------------- /bootstrap/ContainerBootstrap.php: -------------------------------------------------------------------------------- 1 | setSingleton(EventDispatcher::class, DummyEventDispatcher::class); 16 | } 17 | } -------------------------------------------------------------------------------- /codeception.yml: -------------------------------------------------------------------------------- 1 | actor: Tester 2 | paths: 3 | tests: tests 4 | log: tests/_output 5 | data: tests/_data 6 | helpers: tests/_support 7 | settings: 8 | bootstrap: _bootstrap.php 9 | memory_limit: 1024M 10 | colors: true 11 | modules: 12 | config: 13 | Yii2: 14 | configFile: 'config/test.php' 15 | cleanup: false 16 | 17 | # To enable code coverage: 18 | #coverage: 19 | # #c3_url: http://localhost:8080/index-test.php/ 20 | # enabled: true 21 | # #remote: true 22 | # #remote_config: '../codeception.yml' 23 | # whitelist: 24 | # include: 25 | # - models/* 26 | # - controllers/* 27 | # - commands/* 28 | # - mail/* 29 | # blacklist: 30 | # include: 31 | # - assets/* 32 | # - config/* 33 | # - runtime/* 34 | # - vendor/* 35 | # - views/* 36 | # - web/* 37 | # - tests/* 38 | -------------------------------------------------------------------------------- /commands/HelloController.php: -------------------------------------------------------------------------------- 1 | 19 | * @since 2.0 20 | */ 21 | class HelloController extends Controller 22 | { 23 | /** 24 | * This command echoes what you have entered as the message. 25 | * @param string $message the message to be echoed. 26 | */ 27 | public function actionIndex($message = 'hello world') 28 | { 29 | echo $message . "\n"; 30 | 31 | return ExitCode::OK; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "yiisoft/yii2-app-basic", 3 | "description": "Yii 2 Basic Project Template", 4 | "keywords": ["yii2", "framework", "basic", "project template"], 5 | "homepage": "http://www.yiiframework.com/", 6 | "type": "project", 7 | "license": "BSD-3-Clause", 8 | "support": { 9 | "issues": "https://github.com/yiisoft/yii2/issues?state=open", 10 | "forum": "http://www.yiiframework.com/forum/", 11 | "wiki": "http://www.yiiframework.com/wiki/", 12 | "irc": "irc://irc.freenode.net/yii", 13 | "source": "https://github.com/yiisoft/yii2" 14 | }, 15 | "minimum-stability": "stable", 16 | "require": { 17 | "php": ">=7.1.0", 18 | "yiisoft/yii2": "~2.0.13", 19 | "yiisoft/yii2-bootstrap": "~2.0.0", 20 | "yiisoft/yii2-swiftmailer": "~2.0.0", 21 | "beberlei/assert": "^2.7", 22 | "ramsey/uuid": "^3.6" 23 | }, 24 | "require-dev": { 25 | "yiisoft/yii2-debug": "~2.0.0", 26 | "yiisoft/yii2-gii": "~2.0.0", 27 | "yiisoft/yii2-faker": "~2.0.0", 28 | 29 | "codeception/base": "^2.2.3", 30 | "codeception/verify": "~0.3.1", 31 | "codeception/specify": "~0.4.3" 32 | }, 33 | "config": { 34 | "process-timeout": 1800, 35 | "fxp-asset": { 36 | "enabled": false 37 | } 38 | }, 39 | "scripts": { 40 | "post-install-cmd": [ 41 | "yii\\composer\\Installer::postInstall" 42 | ], 43 | "post-create-project-cmd": [ 44 | "yii\\composer\\Installer::postCreateProject", 45 | "yii\\composer\\Installer::postInstall" 46 | ] 47 | }, 48 | "extra": { 49 | "yii\\composer\\Installer::postCreateProject": { 50 | "setPermission": [ 51 | { 52 | "runtime": "0777", 53 | "web/assets": "0777", 54 | "yii": "0755" 55 | } 56 | ] 57 | }, 58 | "yii\\composer\\Installer::postInstall": { 59 | "generateCookieValidationKey": [ 60 | "config/web.php" 61 | ] 62 | } 63 | }, 64 | "repositories": [ 65 | { 66 | "type": "composer", 67 | "url": "https://asset-packagist.org" 68 | } 69 | ] 70 | } 71 | -------------------------------------------------------------------------------- /config/.gitignore: -------------------------------------------------------------------------------- 1 | /db.php 2 | /test_db.php -------------------------------------------------------------------------------- /config/console.php: -------------------------------------------------------------------------------- 1 | 'basic-console', 8 | 'basePath' => dirname(__DIR__), 9 | 'bootstrap' => [ 10 | 'log', 11 | 'app\bootstrap\ContainerBootstrap', 12 | ], 13 | 'controllerNamespace' => 'app\commands', 14 | 'aliases' => [ 15 | '@bower' => '@vendor/bower-asset', 16 | '@npm' => '@vendor/npm-asset', 17 | ], 18 | 'components' => [ 19 | 'cache' => [ 20 | 'class' => 'yii\caching\FileCache', 21 | ], 22 | 'log' => [ 23 | 'targets' => [ 24 | [ 25 | 'class' => 'yii\log\FileTarget', 26 | 'levels' => ['error', 'warning'], 27 | ], 28 | ], 29 | ], 30 | 'db' => $db, 31 | ], 32 | 'params' => $params, 33 | /* 34 | 'controllerMap' => [ 35 | 'fixture' => [ // Fixture generation command line. 36 | 'class' => 'yii\faker\FixtureController', 37 | ], 38 | ], 39 | */ 40 | ]; 41 | 42 | if (YII_ENV_DEV) { 43 | // configuration adjustments for 'dev' environment 44 | $config['bootstrap'][] = 'gii'; 45 | $config['modules']['gii'] = [ 46 | 'class' => 'yii\gii\Module', 47 | ]; 48 | } 49 | 50 | return $config; 51 | -------------------------------------------------------------------------------- /config/params.php: -------------------------------------------------------------------------------- 1 | 'admin@example.com', 5 | ]; 6 | -------------------------------------------------------------------------------- /config/test.php: -------------------------------------------------------------------------------- 1 | 'basic-tests', 10 | 'basePath' => dirname(__DIR__), 11 | 'aliases' => [ 12 | '@bower' => '@vendor/bower-asset', 13 | '@npm' => '@vendor/npm-asset', 14 | ], 15 | 'language' => 'en-US', 16 | 'bootstrap' => [ 17 | 'app\bootstrap\ContainerBootstrap', 18 | ], 19 | 'components' => [ 20 | 'db' => $db, 21 | 'mailer' => [ 22 | 'useFileTransport' => true, 23 | ], 24 | 'assetManager' => [ 25 | 'basePath' => __DIR__ . '/../web/assets', 26 | ], 27 | 'urlManager' => [ 28 | 'showScriptName' => true, 29 | ], 30 | 'user' => [ 31 | 'identityClass' => 'app\models\User', 32 | ], 33 | 'request' => [ 34 | 'cookieValidationKey' => 'test', 35 | 'enableCsrfValidation' => false, 36 | // but if you absolutely need it set cookie domain to localhost 37 | /* 38 | 'csrfCookie' => [ 39 | 'domain' => 'localhost', 40 | ], 41 | */ 42 | ], 43 | ], 44 | 'params' => $params, 45 | ]; 46 | -------------------------------------------------------------------------------- /config/web.php: -------------------------------------------------------------------------------- 1 | 'basic', 8 | 'basePath' => dirname(__DIR__), 9 | 'bootstrap' => [ 10 | 'log', 11 | 'app\bootstrap\ContainerBootstrap', 12 | ], 13 | 'aliases' => [ 14 | '@bower' => '@vendor/bower-asset', 15 | '@npm' => '@vendor/npm-asset', 16 | ], 17 | 'components' => [ 18 | 'request' => [ 19 | // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation 20 | 'cookieValidationKey' => '', 21 | ], 22 | 'cache' => [ 23 | 'class' => 'yii\caching\FileCache', 24 | ], 25 | 'user' => [ 26 | 'identityClass' => 'app\models\User', 27 | 'enableAutoLogin' => true, 28 | ], 29 | 'errorHandler' => [ 30 | 'errorAction' => 'site/error', 31 | ], 32 | 'mailer' => [ 33 | 'class' => 'yii\swiftmailer\Mailer', 34 | // send all mails to a file by default. You have to set 35 | // 'useFileTransport' to false and configure a transport 36 | // for the mailer to send real emails. 37 | 'useFileTransport' => true, 38 | ], 39 | 'log' => [ 40 | 'traceLevel' => YII_DEBUG ? 3 : 0, 41 | 'targets' => [ 42 | [ 43 | 'class' => 'yii\log\FileTarget', 44 | 'levels' => ['error', 'warning'], 45 | ], 46 | ], 47 | ], 48 | 'db' => $db, 49 | /* 50 | 'urlManager' => [ 51 | 'enablePrettyUrl' => true, 52 | 'showScriptName' => false, 53 | 'rules' => [ 54 | ], 55 | ], 56 | */ 57 | ], 58 | 'params' => $params, 59 | ]; 60 | 61 | if (YII_ENV_DEV) { 62 | // configuration adjustments for 'dev' environment 63 | $config['bootstrap'][] = 'debug'; 64 | $config['modules']['debug'] = [ 65 | 'class' => 'yii\debug\Module', 66 | // uncomment the following to add your IP if you are not connecting from localhost. 67 | //'allowedIPs' => ['127.0.0.1', '::1'], 68 | ]; 69 | 70 | $config['bootstrap'][] = 'gii'; 71 | $config['modules']['gii'] = [ 72 | 'class' => 'yii\gii\Module', 73 | // uncomment the following to add your IP if you are not connecting from localhost. 74 | //'allowedIPs' => ['127.0.0.1', '::1'], 75 | ]; 76 | } 77 | 78 | return $config; 79 | -------------------------------------------------------------------------------- /controllers/SiteController.php: -------------------------------------------------------------------------------- 1 | [ 22 | 'class' => AccessControl::className(), 23 | 'only' => ['logout'], 24 | 'rules' => [ 25 | [ 26 | 'actions' => ['logout'], 27 | 'allow' => true, 28 | 'roles' => ['@'], 29 | ], 30 | ], 31 | ], 32 | 'verbs' => [ 33 | 'class' => VerbFilter::className(), 34 | 'actions' => [ 35 | 'logout' => ['post'], 36 | ], 37 | ], 38 | ]; 39 | } 40 | 41 | /** 42 | * @inheritdoc 43 | */ 44 | public function actions() 45 | { 46 | return [ 47 | 'error' => [ 48 | 'class' => 'yii\web\ErrorAction', 49 | ], 50 | 'captcha' => [ 51 | 'class' => 'yii\captcha\CaptchaAction', 52 | 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, 53 | ], 54 | ]; 55 | } 56 | 57 | /** 58 | * Displays homepage. 59 | * 60 | * @return string 61 | */ 62 | public function actionIndex() 63 | { 64 | return $this->render('index'); 65 | } 66 | 67 | /** 68 | * Login action. 69 | * 70 | * @return Response|string 71 | */ 72 | public function actionLogin() 73 | { 74 | if (!Yii::$app->user->isGuest) { 75 | return $this->goHome(); 76 | } 77 | 78 | $model = new LoginForm(); 79 | if ($model->load(Yii::$app->request->post()) && $model->login()) { 80 | return $this->goBack(); 81 | } 82 | 83 | $model->password = ''; 84 | return $this->render('login', [ 85 | 'model' => $model, 86 | ]); 87 | } 88 | 89 | /** 90 | * Logout action. 91 | * 92 | * @return Response 93 | */ 94 | public function actionLogout() 95 | { 96 | Yii::$app->user->logout(); 97 | 98 | return $this->goHome(); 99 | } 100 | 101 | /** 102 | * Displays contact page. 103 | * 104 | * @return Response|string 105 | */ 106 | public function actionContact() 107 | { 108 | $model = new ContactForm(); 109 | if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) { 110 | Yii::$app->session->setFlash('contactFormSubmitted'); 111 | 112 | return $this->refresh(); 113 | } 114 | return $this->render('contact', [ 115 | 'model' => $model, 116 | ]); 117 | } 118 | 119 | /** 120 | * Displays about page. 121 | * 122 | * @return string 123 | */ 124 | public function actionAbout() 125 | { 126 | return $this->render('about'); 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /dispatchers/DummyEventDispatcher.php: -------------------------------------------------------------------------------- 1 | country = $country; 21 | $this->region = $region; 22 | $this->city = $city; 23 | $this->street = $street; 24 | $this->house = $house; 25 | } 26 | 27 | public function getCountry(): string { return $this->country; } 28 | public function getRegion(): string { return $this->region; } 29 | public function getCity(): string { return $this->city; } 30 | public function getStreet(): string { return $this->street; } 31 | public function getHouse(): string { return $this->house; } 32 | } 33 | -------------------------------------------------------------------------------- /entities/Employee/Employee.php: -------------------------------------------------------------------------------- 1 | id = $id; 41 | $this->name = $name; 42 | $this->address = $address; 43 | $this->phones = new Phones($phones); 44 | $this->createDate = $date; 45 | $this->addStatus(Status::ACTIVE, $date); 46 | $this->recordEvent(new Events\EmployeeCreated($this->id)); 47 | } 48 | 49 | public function rename(Name $name): void 50 | { 51 | $this->name = $name; 52 | $this->recordEvent(new Events\EmployeeRenamed($this->id, $name)); 53 | } 54 | 55 | public function changeAddress(Address $address): void 56 | { 57 | $this->address = $address; 58 | $this->recordEvent(new Events\EmployeeAddressChanged($this->id, $address)); 59 | } 60 | 61 | public function addPhone(Phone $phone): void 62 | { 63 | $this->phones->add($phone); 64 | $this->recordEvent(new Events\EmployeePhoneAdded($this->id, $phone)); 65 | } 66 | 67 | public function removePhone($index): void 68 | { 69 | $phone = $this->phones->remove($index); 70 | $this->recordEvent(new Events\EmployeePhoneRemoved($this->id, $phone)); 71 | } 72 | 73 | public function archive(\DateTimeImmutable $date): void 74 | { 75 | if ($this->isArchived()) { 76 | throw new \DomainException('Employee is already archived.'); 77 | } 78 | $this->addStatus(Status::ARCHIVED, $date); 79 | $this->recordEvent(new Events\EmployeeArchived($this->id, $date)); 80 | } 81 | 82 | public function reinstate(\DateTimeImmutable $date): void 83 | { 84 | if (!$this->isArchived()) { 85 | throw new \DomainException('Employee is not archived.'); 86 | } 87 | $this->addStatus(Status::ACTIVE, $date); 88 | $this->recordEvent(new Events\EmployeeReinstated($this->id, $date)); 89 | } 90 | 91 | public function remove(): void 92 | { 93 | if (!$this->isArchived()) { 94 | throw new \DomainException('Cannot remove active employee.'); 95 | } 96 | $this->recordEvent(new Events\EmployeeRemoved($this->id)); 97 | } 98 | 99 | public function isActive(): bool 100 | { 101 | return $this->getCurrentStatus()->isActive(); 102 | } 103 | 104 | public function isArchived(): bool 105 | { 106 | return $this->getCurrentStatus()->isArchived(); 107 | } 108 | 109 | private function getCurrentStatus(): Status 110 | { 111 | return end($this->statuses); 112 | } 113 | 114 | private function addStatus($value, \DateTimeImmutable $date): void 115 | { 116 | $this->statuses[] = new Status($value, $date); 117 | } 118 | 119 | public function getId(): Id { return $this->id; } 120 | public function getName(): Name { return $this->name; } 121 | public function getPhones(): array { return $this->phones->getAll(); } 122 | public function getAddress(): Address { return $this->address; } 123 | public function getCreateDate(): \DateTimeImmutable { return $this->createDate; } 124 | public function getStatuses(): array { return $this->statuses; } 125 | } 126 | -------------------------------------------------------------------------------- /entities/Employee/Events/EmployeeAddressChanged.php: -------------------------------------------------------------------------------- 1 | employee = $employee; 16 | $this->address = $address; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /entities/Employee/Events/EmployeeArchived.php: -------------------------------------------------------------------------------- 1 | employee = $employee; 15 | $this->date = $date; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /entities/Employee/Events/EmployeeCreated.php: -------------------------------------------------------------------------------- 1 | employee = $employee; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /entities/Employee/Events/EmployeePhoneAdded.php: -------------------------------------------------------------------------------- 1 | employee = $employee; 16 | $this->phone = $phone; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /entities/Employee/Events/EmployeePhoneRemoved.php: -------------------------------------------------------------------------------- 1 | employee = $employee; 16 | $this->phone = $phone; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /entities/Employee/Events/EmployeeReinstated.php: -------------------------------------------------------------------------------- 1 | employee = $employee; 15 | $this->date = $date; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /entities/Employee/Events/EmployeeRemoved.php: -------------------------------------------------------------------------------- 1 | employee = $employee; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /entities/Employee/Events/EmployeeRenamed.php: -------------------------------------------------------------------------------- 1 | employee = $employee; 16 | $this->name = $name; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /entities/Employee/Id.php: -------------------------------------------------------------------------------- 1 | id = $id; 17 | } 18 | 19 | public static function next(): self 20 | { 21 | return new self(Uuid::uuid4()->toString()); 22 | } 23 | 24 | public function getId(): string 25 | { 26 | return $this->id; 27 | } 28 | 29 | public function isEqualTo(self $other): bool 30 | { 31 | return $this->getId() === $other->getId(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /entities/Employee/Name.php: -------------------------------------------------------------------------------- 1 | last = $last; 19 | $this->first = $first; 20 | $this->middle = $middle; 21 | } 22 | 23 | public function getFull(): string 24 | { 25 | return trim($this->last . ' ' . $this->first . ' ' . $this->middle); 26 | } 27 | 28 | public function getFirst(): string { return $this->first; } 29 | public function getMiddle(): ?string { return $this->middle; } 30 | public function getLast(): string { return $this->last; } 31 | } 32 | -------------------------------------------------------------------------------- /entities/Employee/Phone.php: -------------------------------------------------------------------------------- 1 | country = $country; 20 | $this->code = $code; 21 | $this->number = $number; 22 | } 23 | 24 | public function isEqualTo(self $phone): bool 25 | { 26 | return $this->getFull() === $phone->getFull(); 27 | } 28 | 29 | public function getFull(): string 30 | { 31 | return '+' . $this->country . ' (' . $this->code . ') ' . $this->number; 32 | } 33 | 34 | public function getCountry(): int { return $this->country; } 35 | public function getCode(): string { return $this->code; } 36 | public function getNumber(): string { return $this->number; } 37 | } 38 | -------------------------------------------------------------------------------- /entities/Employee/Phones.php: -------------------------------------------------------------------------------- 1 | add($phone); 19 | } 20 | } 21 | 22 | public function add(Phone $phone): void 23 | { 24 | foreach ($this->phones as $item) { 25 | if ($item->isEqualTo($phone)) { 26 | throw new \DomainException('Phone already exists.'); 27 | } 28 | } 29 | $this->phones[] = $phone; 30 | } 31 | 32 | public function remove($index): Phone 33 | { 34 | if (!isset($this->phones[$index])) { 35 | throw new \DomainException('Phone is not found.'); 36 | } 37 | if (\count($this->phones) === 1) { 38 | throw new \DomainException('Cannot remove the last phone.'); 39 | } 40 | $phone = $this->phones[$index]; 41 | unset($this->phones[$index]); 42 | return $phone; 43 | } 44 | 45 | public function getAll(): array 46 | { 47 | return $this->phones; 48 | } 49 | } -------------------------------------------------------------------------------- /entities/Employee/Status.php: -------------------------------------------------------------------------------- 1 | value = $value; 23 | $this->date = $date; 24 | } 25 | 26 | public function isActive(): bool 27 | { 28 | return $this->value === self::ACTIVE; 29 | } 30 | 31 | public function isArchived(): bool 32 | { 33 | return $this->value === self::ARCHIVED; 34 | } 35 | 36 | public function getValue(): string { return $this->value; } 37 | public function getDate(): \DateTimeImmutable { return $this->date; } 38 | } -------------------------------------------------------------------------------- /entities/EventTrait.php: -------------------------------------------------------------------------------- 1 | events[] = $event; 12 | } 13 | 14 | public function releaseEvents(): array 15 | { 16 | $events = $this->events; 17 | $this->events = []; 18 | return $events; 19 | } 20 | } -------------------------------------------------------------------------------- /mail/layouts/html.php: -------------------------------------------------------------------------------- 1 | 8 | beginPage() ?> 9 | 10 | 11 | 12 | 13 | <?= Html::encode($this->title) ?> 14 | head() ?> 15 | 16 | 17 | beginBody() ?> 18 | 19 | endBody() ?> 20 | 21 | 22 | endPage() ?> 23 | -------------------------------------------------------------------------------- /models/ContactForm.php: -------------------------------------------------------------------------------- 1 | 'Verification Code', 42 | ]; 43 | } 44 | 45 | /** 46 | * Sends an email to the specified email address using the information collected by this model. 47 | * @param string $email the target email address 48 | * @return bool whether the model passes validation 49 | */ 50 | public function contact($email) 51 | { 52 | if ($this->validate()) { 53 | Yii::$app->mailer->compose() 54 | ->setTo($email) 55 | ->setFrom([$this->email => $this->name]) 56 | ->setSubject($this->subject) 57 | ->setTextBody($this->body) 58 | ->send(); 59 | 60 | return true; 61 | } 62 | return false; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /models/LoginForm.php: -------------------------------------------------------------------------------- 1 | hasErrors()) { 48 | $user = $this->getUser(); 49 | 50 | if (!$user || !$user->validatePassword($this->password)) { 51 | $this->addError($attribute, 'Incorrect username or password.'); 52 | } 53 | } 54 | } 55 | 56 | /** 57 | * Logs in a user using the provided username and password. 58 | * @return bool whether the user is logged in successfully 59 | */ 60 | public function login() 61 | { 62 | if ($this->validate()) { 63 | return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0); 64 | } 65 | return false; 66 | } 67 | 68 | /** 69 | * Finds user by [[username]] 70 | * 71 | * @return User|null 72 | */ 73 | public function getUser() 74 | { 75 | if ($this->_user === false) { 76 | $this->_user = User::findByUsername($this->username); 77 | } 78 | 79 | return $this->_user; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /models/User.php: -------------------------------------------------------------------------------- 1 | [ 15 | 'id' => '100', 16 | 'username' => 'admin', 17 | 'password' => 'admin', 18 | 'authKey' => 'test100key', 19 | 'accessToken' => '100-token', 20 | ], 21 | '101' => [ 22 | 'id' => '101', 23 | 'username' => 'demo', 24 | 'password' => 'demo', 25 | 'authKey' => 'test101key', 26 | 'accessToken' => '101-token', 27 | ], 28 | ]; 29 | 30 | 31 | /** 32 | * @inheritdoc 33 | */ 34 | public static function findIdentity($id) 35 | { 36 | return isset(self::$users[$id]) ? new static(self::$users[$id]) : null; 37 | } 38 | 39 | /** 40 | * @inheritdoc 41 | */ 42 | public static function findIdentityByAccessToken($token, $type = null) 43 | { 44 | foreach (self::$users as $user) { 45 | if ($user['accessToken'] === $token) { 46 | return new static($user); 47 | } 48 | } 49 | 50 | return null; 51 | } 52 | 53 | /** 54 | * Finds user by username 55 | * 56 | * @param string $username 57 | * @return static|null 58 | */ 59 | public static function findByUsername($username) 60 | { 61 | foreach (self::$users as $user) { 62 | if (strcasecmp($user['username'], $username) === 0) { 63 | return new static($user); 64 | } 65 | } 66 | 67 | return null; 68 | } 69 | 70 | /** 71 | * @inheritdoc 72 | */ 73 | public function getId() 74 | { 75 | return $this->id; 76 | } 77 | 78 | /** 79 | * @inheritdoc 80 | */ 81 | public function getAuthKey() 82 | { 83 | return $this->authKey; 84 | } 85 | 86 | /** 87 | * @inheritdoc 88 | */ 89 | public function validateAuthKey($authKey) 90 | { 91 | return $this->authKey === $authKey; 92 | } 93 | 94 | /** 95 | * Validates password 96 | * 97 | * @param string $password password to validate 98 | * @return bool if password provided is valid for current user 99 | */ 100 | public function validatePassword($password) 101 | { 102 | return $this->password === $password; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /repositories/EmployeeRepository.php: -------------------------------------------------------------------------------- 1 | items[$id->getId()])) { 15 | throw new NotFoundException('Employee not found.'); 16 | } 17 | return clone $this->items[$id->getId()]; 18 | } 19 | 20 | public function add(Employee $employee): void 21 | { 22 | $this->items[$employee->getId()->getId()] = $employee; 23 | } 24 | 25 | public function save(Employee $employee): void 26 | { 27 | $this->items[$employee->getId()->getId()] = $employee; 28 | } 29 | 30 | public function remove(Employee $employee): void 31 | { 32 | if ($this->items[$employee->getId()->getId()]) { 33 | unset($this->items[$employee->getId()->getId()]); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /repositories/NotFoundException.php: -------------------------------------------------------------------------------- 1 | Error\n\n" 33 | . "

The path to yii framework seems to be incorrect.

\n" 34 | . '

You need to install Yii framework via composer or adjust the framework path in file ' . basename(__FILE__) . ".

\n" 35 | . '

Please refer to the README on how to install Yii.

\n"; 36 | 37 | if (!empty($_SERVER['argv'])) { 38 | // do not print HTML when used in console mode 39 | echo strip_tags($message); 40 | } else { 41 | echo $message; 42 | } 43 | exit(1); 44 | } 45 | 46 | require_once($frameworkPath . '/requirements/YiiRequirementChecker.php'); 47 | $requirementsChecker = new YiiRequirementChecker(); 48 | 49 | $gdMemo = $imagickMemo = 'Either GD PHP extension with FreeType support or ImageMagick PHP extension with PNG support is required for image CAPTCHA.'; 50 | $gdOK = $imagickOK = false; 51 | 52 | if (extension_loaded('imagick')) { 53 | $imagick = new Imagick(); 54 | $imagickFormats = $imagick->queryFormats('PNG'); 55 | if (in_array('PNG', $imagickFormats)) { 56 | $imagickOK = true; 57 | } else { 58 | $imagickMemo = 'Imagick extension should be installed with PNG support in order to be used for image CAPTCHA.'; 59 | } 60 | } 61 | 62 | if (extension_loaded('gd')) { 63 | $gdInfo = gd_info(); 64 | if (!empty($gdInfo['FreeType Support'])) { 65 | $gdOK = true; 66 | } else { 67 | $gdMemo = 'GD extension should be installed with FreeType support in order to be used for image CAPTCHA.'; 68 | } 69 | } 70 | 71 | /** 72 | * Adjust requirements according to your application specifics. 73 | */ 74 | $requirements = array( 75 | // Database : 76 | array( 77 | 'name' => 'PDO extension', 78 | 'mandatory' => true, 79 | 'condition' => extension_loaded('pdo'), 80 | 'by' => 'All DB-related classes', 81 | ), 82 | array( 83 | 'name' => 'PDO SQLite extension', 84 | 'mandatory' => false, 85 | 'condition' => extension_loaded('pdo_sqlite'), 86 | 'by' => 'All DB-related classes', 87 | 'memo' => 'Required for SQLite database.', 88 | ), 89 | array( 90 | 'name' => 'PDO MySQL extension', 91 | 'mandatory' => false, 92 | 'condition' => extension_loaded('pdo_mysql'), 93 | 'by' => 'All DB-related classes', 94 | 'memo' => 'Required for MySQL database.', 95 | ), 96 | array( 97 | 'name' => 'PDO PostgreSQL extension', 98 | 'mandatory' => false, 99 | 'condition' => extension_loaded('pdo_pgsql'), 100 | 'by' => 'All DB-related classes', 101 | 'memo' => 'Required for PostgreSQL database.', 102 | ), 103 | // Cache : 104 | array( 105 | 'name' => 'Memcache extension', 106 | 'mandatory' => false, 107 | 'condition' => extension_loaded('memcache') || extension_loaded('memcached'), 108 | 'by' => 'MemCache', 109 | 'memo' => extension_loaded('memcached') ? 'To use memcached set MemCache::useMemcached to true.' : '' 110 | ), 111 | // CAPTCHA: 112 | array( 113 | 'name' => 'GD PHP extension with FreeType support', 114 | 'mandatory' => false, 115 | 'condition' => $gdOK, 116 | 'by' => 'Captcha', 117 | 'memo' => $gdMemo, 118 | ), 119 | array( 120 | 'name' => 'ImageMagick PHP extension with PNG support', 121 | 'mandatory' => false, 122 | 'condition' => $imagickOK, 123 | 'by' => 'Captcha', 124 | 'memo' => $imagickMemo, 125 | ), 126 | // PHP ini : 127 | 'phpExposePhp' => array( 128 | 'name' => 'Expose PHP', 129 | 'mandatory' => false, 130 | 'condition' => $requirementsChecker->checkPhpIniOff("expose_php"), 131 | 'by' => 'Security reasons', 132 | 'memo' => '"expose_php" should be disabled at php.ini', 133 | ), 134 | 'phpAllowUrlInclude' => array( 135 | 'name' => 'PHP allow url include', 136 | 'mandatory' => false, 137 | 'condition' => $requirementsChecker->checkPhpIniOff("allow_url_include"), 138 | 'by' => 'Security reasons', 139 | 'memo' => '"allow_url_include" should be disabled at php.ini', 140 | ), 141 | 'phpSmtp' => array( 142 | 'name' => 'PHP mail SMTP', 143 | 'mandatory' => false, 144 | 'condition' => strlen(ini_get('SMTP')) > 0, 145 | 'by' => 'Email sending', 146 | 'memo' => 'PHP mail SMTP server required', 147 | ), 148 | ); 149 | 150 | // OPcache check 151 | if (!version_compare(phpversion(), '5.5', '>=')) { 152 | $requirements[] = array( 153 | 'name' => 'APC extension', 154 | 'mandatory' => false, 155 | 'condition' => extension_loaded('apc'), 156 | 'by' => 'ApcCache', 157 | ); 158 | } 159 | 160 | $requirementsChecker->checkYii()->check($requirements)->render(); 161 | -------------------------------------------------------------------------------- /runtime/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore -------------------------------------------------------------------------------- /services/EmployeeService.php: -------------------------------------------------------------------------------- 1 | employees = $employees; 27 | $this->dispatcher = $dispatcher; 28 | } 29 | 30 | public function create(EmployeeCreateDto $dto): void 31 | { 32 | $employee = new Employee( 33 | Id::next(), 34 | new \DateTimeImmutable(), 35 | new Name( 36 | $dto->name->last, 37 | $dto->name->first, 38 | $dto->name->middle 39 | ), 40 | new Address( 41 | $dto->address->country, 42 | $dto->address->region, 43 | $dto->address->city, 44 | $dto->address->street, 45 | $dto->address->house 46 | ), 47 | array_map(static function (PhoneDto $phone) { 48 | return new Phone( 49 | $phone->country, 50 | $phone->code, 51 | $phone->number 52 | ); 53 | }, $dto->phones) 54 | ); 55 | $this->employees->add($employee); 56 | $this->dispatcher->dispatch($employee->releaseEvents()); 57 | } 58 | 59 | public function rename(Id $id, NameDto $dto): void 60 | { 61 | $employee = $this->employees->get($id); 62 | $employee->rename(new Name( 63 | $dto->last, 64 | $dto->first, 65 | $dto->middle 66 | )); 67 | $this->employees->save($employee); 68 | $this->dispatcher->dispatch($employee->releaseEvents()); 69 | } 70 | 71 | public function changeAddress(Id $id, AddressDto $dto): void 72 | { 73 | $employee = $this->employees->get($id); 74 | $employee->changeAddress(new Address( 75 | $dto->country, 76 | $dto->region, 77 | $dto->city, 78 | $dto->street, 79 | $dto->house 80 | )); 81 | $this->employees->save($employee); 82 | $this->dispatcher->dispatch($employee->releaseEvents()); 83 | } 84 | 85 | public function addPhone(Id $id, PhoneDto $dto): void 86 | { 87 | $employee = $this->employees->get($id); 88 | $employee->addPhone(new Phone( 89 | $dto->country, 90 | $dto->code, 91 | $dto->number 92 | )); 93 | $this->employees->save($employee); 94 | $this->dispatcher->dispatch($employee->releaseEvents()); 95 | } 96 | 97 | public function removePhone(Id $id, $index): void 98 | { 99 | $employee = $this->employees->get($id); 100 | $employee->removePhone($index); 101 | $this->employees->save($employee); 102 | $this->dispatcher->dispatch($employee->releaseEvents()); 103 | } 104 | 105 | public function archive(Id $id, EmployeeArchiveDto $dto): void 106 | { 107 | $employee = $this->employees->get($id); 108 | $employee->archive($dto->date); 109 | $this->employees->save($employee); 110 | $this->dispatcher->dispatch($employee->releaseEvents()); 111 | } 112 | 113 | public function reinstate(Id $id, EmployeeReinstateDto $dto): void 114 | { 115 | $employee = $this->employees->get($id); 116 | $employee->reinstate($dto->date); 117 | $this->employees->save($employee); 118 | $this->dispatcher->dispatch($employee->releaseEvents()); 119 | } 120 | 121 | public function remove(Id $id): void 122 | { 123 | $employee = $this->employees->get($id); 124 | $employee->remove(); 125 | $this->employees->remove($employee); 126 | $this->dispatcher->dispatch($employee->releaseEvents()); 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /services/dto/AddressDto.php: -------------------------------------------------------------------------------- 1 | amOnPage(Url::toRoute('/site/about')); 10 | $I->see('About', 'h1'); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/acceptance/ContactCest.php: -------------------------------------------------------------------------------- 1 | amOnPage(Url::toRoute('/site/contact')); 10 | } 11 | 12 | public function contactPageWorks(AcceptanceTester $I) 13 | { 14 | $I->wantTo('ensure that contact page works'); 15 | $I->see('Contact', 'h1'); 16 | } 17 | 18 | public function contactFormCanBeSubmitted(AcceptanceTester $I) 19 | { 20 | $I->amGoingTo('submit contact form with correct data'); 21 | $I->fillField('#contactform-name', 'tester'); 22 | $I->fillField('#contactform-email', 'tester@example.com'); 23 | $I->fillField('#contactform-subject', 'test subject'); 24 | $I->fillField('#contactform-body', 'test content'); 25 | $I->fillField('#contactform-verifycode', 'testme'); 26 | 27 | $I->click('contact-button'); 28 | 29 | $I->wait(2); // wait for button to be clicked 30 | 31 | $I->dontSeeElement('#contact-form'); 32 | $I->see('Thank you for contacting us. We will respond to you as soon as possible.'); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/acceptance/HomeCest.php: -------------------------------------------------------------------------------- 1 | amOnPage(Url::toRoute('/site/index')); 10 | $I->see('My Company'); 11 | 12 | $I->seeLink('About'); 13 | $I->click('About'); 14 | $I->wait(2); // wait for page to be opened 15 | 16 | $I->see('This is the About page.'); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/acceptance/LoginCest.php: -------------------------------------------------------------------------------- 1 | amOnPage(Url::toRoute('/site/login')); 10 | $I->see('Login', 'h1'); 11 | 12 | $I->amGoingTo('try to login with correct credentials'); 13 | $I->fillField('input[name="LoginForm[username]"]', 'admin'); 14 | $I->fillField('input[name="LoginForm[password]"]', 'admin'); 15 | $I->click('login-button'); 16 | $I->wait(2); // wait for button to be clicked 17 | 18 | $I->expectTo('see user info'); 19 | $I->see('Logout'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/acceptance/_bootstrap.php: -------------------------------------------------------------------------------- 1 | [ 21 | 'db' => require __DIR__ . '/../../config/test_db.php' 22 | ] 23 | ] 24 | ); 25 | 26 | 27 | $application = new yii\console\Application($config); 28 | $exitCode = $application->run(); 29 | exit($exitCode); 30 | -------------------------------------------------------------------------------- /tests/bin/yii.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | rem ------------------------------------------------------------- 4 | rem Yii command line bootstrap script for Windows. 5 | rem 6 | rem @author Qiang Xue 7 | rem @link http://www.yiiframework.com/ 8 | rem @copyright Copyright (c) 2008 Yii Software LLC 9 | rem @license http://www.yiiframework.com/license/ 10 | rem ------------------------------------------------------------- 11 | 12 | @setlocal 13 | 14 | set YII_PATH=%~dp0 15 | 16 | if "%PHP_COMMAND%" == "" set PHP_COMMAND=php.exe 17 | 18 | "%PHP_COMMAND%" "%YII_PATH%yii" %* 19 | 20 | @endlocal 21 | -------------------------------------------------------------------------------- /tests/functional.suite.yml: -------------------------------------------------------------------------------- 1 | # Codeception Test Suite Configuration 2 | 3 | # suite for functional (integration) tests. 4 | # emulate web requests and make application process them. 5 | # (tip: better to use with frameworks). 6 | 7 | # RUN `build` COMMAND AFTER ADDING/REMOVING MODULES. 8 | #basic/web/index.php 9 | class_name: FunctionalTester 10 | modules: 11 | enabled: 12 | - Filesystem 13 | - Yii2 14 | -------------------------------------------------------------------------------- /tests/functional/ContactFormCest.php: -------------------------------------------------------------------------------- 1 | amOnPage(['site/contact']); 8 | } 9 | 10 | public function openContactPage(\FunctionalTester $I) 11 | { 12 | $I->see('Contact', 'h1'); 13 | } 14 | 15 | public function submitEmptyForm(\FunctionalTester $I) 16 | { 17 | $I->submitForm('#contact-form', []); 18 | $I->expectTo('see validations errors'); 19 | $I->see('Contact', 'h1'); 20 | $I->see('Name cannot be blank'); 21 | $I->see('Email cannot be blank'); 22 | $I->see('Subject cannot be blank'); 23 | $I->see('Body cannot be blank'); 24 | $I->see('The verification code is incorrect'); 25 | } 26 | 27 | public function submitFormWithIncorrectEmail(\FunctionalTester $I) 28 | { 29 | $I->submitForm('#contact-form', [ 30 | 'ContactForm[name]' => 'tester', 31 | 'ContactForm[email]' => 'tester.email', 32 | 'ContactForm[subject]' => 'test subject', 33 | 'ContactForm[body]' => 'test content', 34 | 'ContactForm[verifyCode]' => 'testme', 35 | ]); 36 | $I->expectTo('see that email address is wrong'); 37 | $I->dontSee('Name cannot be blank', '.help-inline'); 38 | $I->see('Email is not a valid email address.'); 39 | $I->dontSee('Subject cannot be blank', '.help-inline'); 40 | $I->dontSee('Body cannot be blank', '.help-inline'); 41 | $I->dontSee('The verification code is incorrect', '.help-inline'); 42 | } 43 | 44 | public function submitFormSuccessfully(\FunctionalTester $I) 45 | { 46 | $I->submitForm('#contact-form', [ 47 | 'ContactForm[name]' => 'tester', 48 | 'ContactForm[email]' => 'tester@example.com', 49 | 'ContactForm[subject]' => 'test subject', 50 | 'ContactForm[body]' => 'test content', 51 | 'ContactForm[verifyCode]' => 'testme', 52 | ]); 53 | $I->seeEmailIsSent(); 54 | $I->dontSeeElement('#contact-form'); 55 | $I->see('Thank you for contacting us. We will respond to you as soon as possible.'); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /tests/functional/LoginFormCest.php: -------------------------------------------------------------------------------- 1 | amOnRoute('site/login'); 8 | } 9 | 10 | public function openLoginPage(\FunctionalTester $I) 11 | { 12 | $I->see('Login', 'h1'); 13 | 14 | } 15 | 16 | // demonstrates `amLoggedInAs` method 17 | public function internalLoginById(\FunctionalTester $I) 18 | { 19 | $I->amLoggedInAs(100); 20 | $I->amOnPage('/'); 21 | $I->see('Logout (admin)'); 22 | } 23 | 24 | // demonstrates `amLoggedInAs` method 25 | public function internalLoginByInstance(\FunctionalTester $I) 26 | { 27 | $I->amLoggedInAs(\app\models\User::findByUsername('admin')); 28 | $I->amOnPage('/'); 29 | $I->see('Logout (admin)'); 30 | } 31 | 32 | public function loginWithEmptyCredentials(\FunctionalTester $I) 33 | { 34 | $I->submitForm('#login-form', []); 35 | $I->expectTo('see validations errors'); 36 | $I->see('Username cannot be blank.'); 37 | $I->see('Password cannot be blank.'); 38 | } 39 | 40 | public function loginWithWrongCredentials(\FunctionalTester $I) 41 | { 42 | $I->submitForm('#login-form', [ 43 | 'LoginForm[username]' => 'admin', 44 | 'LoginForm[password]' => 'wrong', 45 | ]); 46 | $I->expectTo('see validations errors'); 47 | $I->see('Incorrect username or password.'); 48 | } 49 | 50 | public function loginSuccessfully(\FunctionalTester $I) 51 | { 52 | $I->submitForm('#login-form', [ 53 | 'LoginForm[username]' => 'admin', 54 | 'LoginForm[password]' => 'admin', 55 | ]); 56 | $I->see('Logout (admin)'); 57 | $I->dontSeeElement('form#login-form'); 58 | } 59 | } -------------------------------------------------------------------------------- /tests/functional/_bootstrap.php: -------------------------------------------------------------------------------- 1 | build(); 13 | 14 | $this->assertTrue($employee->isActive()); 15 | $this->assertFalse($employee->isArchived()); 16 | 17 | $employee->archive($date = new \DateTimeImmutable('2011-06-15')); 18 | 19 | $this->assertFalse($employee->isActive()); 20 | $this->assertTrue($employee->isArchived()); 21 | 22 | $this->assertNotEmpty($statuses = $employee->getStatuses()); 23 | $this->assertTrue(end($statuses)->isArchived()); 24 | 25 | $this->assertNotEmpty($events = $employee->releaseEvents()); 26 | $this->assertInstanceOf(EmployeeArchived::class, end($events)); 27 | } 28 | 29 | public function testAlreadyArchived(): void 30 | { 31 | $employee = (new EmployeeBuilder())->archived()->build(); 32 | 33 | $this->expectExceptionMessage('Employee is already archived.'); 34 | $employee->archive(new \DateTimeImmutable('2011-06-15')); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/unit/entities/Employee/ChangeAddressTest.php: -------------------------------------------------------------------------------- 1 | build(); 14 | 15 | $employee->changeAddress($address = new Address('New', 'Test', 'Address', 'Street', '25a')); 16 | $this->assertEquals($address, $employee->getAddress()); 17 | 18 | $this->assertNotEmpty($events = $employee->releaseEvents()); 19 | $this->assertInstanceOf(EmployeeAddressChanged::class, end($events)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/unit/entities/Employee/CreateTest.php: -------------------------------------------------------------------------------- 1 | assertEquals($id, $employee->getId()); 30 | $this->assertEquals($date, $employee->getCreateDate()); 31 | $this->assertEquals($name, $employee->getName()); 32 | $this->assertEquals($address, $employee->getAddress()); 33 | $this->assertEquals($phones, $employee->getPhones()); 34 | 35 | $this->assertNotNull($employee->getCreateDate()); 36 | 37 | $this->assertTrue($employee->isActive()); 38 | 39 | $this->assertCount(1, $statuses = $employee->getStatuses()); 40 | $this->assertTrue(end($statuses)->isActive()); 41 | 42 | $this->assertNotEmpty($events = $employee->releaseEvents()); 43 | $this->assertInstanceOf(EmployeeCreated::class, end($events)); 44 | } 45 | 46 | public function testWithoutPhones(): void 47 | { 48 | $this->expectExceptionMessage('Employee must contain at least one phone.'); 49 | 50 | new Employee( 51 | Id::next(), 52 | new \DateTimeImmutable(), 53 | new Name('Пупкин', 'Василий', 'Петрович'), 54 | new Address('Россия', 'Липецкая обл.', 'г. Пушкин', 'ул. Ленина', 25), 55 | [] 56 | ); 57 | } 58 | 59 | public function testWithSamePhoneNumbers(): void 60 | { 61 | $this->expectExceptionMessage('Phone already exists.'); 62 | 63 | new Employee( 64 | Id::next(), 65 | new \DateTimeImmutable(), 66 | new Name('Пупкин', 'Василий', 'Петрович'), 67 | new Address('Россия', 'Липецкая обл.', 'г. Пушкин', 'ул. Ленина', 25), 68 | [ 69 | new Phone(7, '920', '00000001'), 70 | new Phone(7, '920', '00000001'), 71 | ] 72 | ); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /tests/unit/entities/Employee/EmployeeBuilder.php: -------------------------------------------------------------------------------- 1 | id = Id::next(); 23 | $this->date = new \DateTimeImmutable(); 24 | $this->name = new Name('Пупкин', 'Василий', 'Петрович'); 25 | $this->address = new Address('Россия', 'Липецкая обл.', 'г. Пушкин', 'ул. Ленина', 25); 26 | $this->phones[] = new Phone(7, '000', '00000000'); 27 | } 28 | 29 | public function withId(Id $id): self 30 | { 31 | $clone = clone $this; 32 | $clone->id = $id; 33 | return $clone; 34 | } 35 | 36 | public function withPhones(array $phones): self 37 | { 38 | $clone = clone $this; 39 | $clone->phones = $phones; 40 | return $clone; 41 | } 42 | 43 | public function archived(): self 44 | { 45 | $clone = clone $this; 46 | $clone->archived = true; 47 | return $clone; 48 | } 49 | 50 | public function build(): Employee 51 | { 52 | $employee = new Employee( 53 | $this->id, 54 | $this->date, 55 | $this->name, 56 | $this->address, 57 | $this->phones 58 | ); 59 | if ($this->archived) { 60 | $employee->archive(new \DateTimeImmutable()); 61 | } 62 | return $employee; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /tests/unit/entities/Employee/PhoneTest.php: -------------------------------------------------------------------------------- 1 | build(); 15 | 16 | $employee->addPhone($phone = new Phone(7, '888', '00000001')); 17 | 18 | $this->assertNotEmpty($phones = $employee->getPhones()); 19 | $this->assertEquals($phone, end($phones)); 20 | 21 | $this->assertNotEmpty($events = $employee->releaseEvents()); 22 | $this->assertInstanceOf(EmployeePhoneAdded::class, end($events)); 23 | } 24 | 25 | public function testAddExists(): void 26 | { 27 | $employee = (new EmployeeBuilder()) 28 | ->withPhones([$phone = new Phone(7, '888', '00000001')]) 29 | ->build(); 30 | 31 | $this->expectExceptionMessage('Phone already exists.'); 32 | 33 | $employee->addPhone($phone); 34 | } 35 | 36 | public function testRemove(): void 37 | { 38 | $employee = (new EmployeeBuilder()) 39 | ->withPhones([ 40 | new Phone(7, '888', '00000001'), 41 | new Phone(7, '888', '00000002'), 42 | ]) 43 | ->build(); 44 | 45 | $this->assertCount(2, $employee->getPhones()); 46 | 47 | $employee->removePhone(1); 48 | 49 | $this->assertCount(1, $employee->getPhones()); 50 | 51 | $this->assertNotEmpty($events = $employee->releaseEvents()); 52 | $this->assertInstanceOf(EmployeePhoneRemoved::class, end($events)); 53 | } 54 | 55 | public function testRemoveNotExists(): void 56 | { 57 | $employee = (new EmployeeBuilder())->build(); 58 | 59 | $this->expectExceptionMessage('Phone is not found.'); 60 | 61 | $employee->removePhone(42); 62 | } 63 | 64 | public function testRemoveLast(): void 65 | { 66 | $employee = (new EmployeeBuilder()) 67 | ->withPhones([ 68 | new Phone(7, '888', '00000001'), 69 | ]) 70 | ->build(); 71 | 72 | $this->expectExceptionMessage('Cannot remove the last phone.'); 73 | 74 | $employee->removePhone(0); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /tests/unit/entities/Employee/ReinstateTest.php: -------------------------------------------------------------------------------- 1 | archived()->build(); 14 | 15 | $this->assertFalse($employee->isActive()); 16 | $this->assertTrue($employee->isArchived()); 17 | 18 | $employee->reinstate($date = new \DateTimeImmutable('2011-06-15')); 19 | 20 | $this->assertTrue($employee->isActive()); 21 | $this->assertFalse($employee->isArchived()); 22 | 23 | $this->assertNotEmpty($statuses = $employee->getStatuses()); 24 | $this->assertTrue(end($statuses)->isActive()); 25 | 26 | $this->assertNotEmpty($events = $employee->releaseEvents()); 27 | $this->assertInstanceOf(EmployeeReinstated::class, end($events)); 28 | } 29 | 30 | public function testNotArchived(): void 31 | { 32 | $employee = (new EmployeeBuilder())->build(); 33 | 34 | $this->expectExceptionMessage('Employee is not archived.'); 35 | 36 | $employee->reinstate($date = new \DateTimeImmutable('2011-06-15')); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /tests/unit/entities/Employee/RemoveTest.php: -------------------------------------------------------------------------------- 1 | archived()->build(); 13 | 14 | $employee->remove(); 15 | 16 | $this->assertNotEmpty($events = $employee->releaseEvents()); 17 | $this->assertInstanceOf(EmployeeRemoved::class, end($events)); 18 | } 19 | 20 | public function testNotArchived(): void 21 | { 22 | $employee = (new EmployeeBuilder())->build(); 23 | 24 | $this->expectExceptionMessage('Cannot remove active employee.'); 25 | 26 | $employee->remove(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/unit/entities/Employee/RenameTest.php: -------------------------------------------------------------------------------- 1 | build(); 14 | 15 | $employee->rename($name = new Name('New', 'Test', 'Name')); 16 | $this->assertEquals($name, $employee->getName()); 17 | 18 | $this->assertNotEmpty($events = $employee->releaseEvents()); 19 | $this->assertInstanceOf(EmployeeRenamed::class, end($events)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/unit/models/ContactFormTest.php: -------------------------------------------------------------------------------- 1 | model = $this->getMockBuilder('app\models\ContactForm') 17 | ->setMethods(['validate']) 18 | ->getMock(); 19 | 20 | $this->model->expects($this->once()) 21 | ->method('validate') 22 | ->will($this->returnValue(true)); 23 | 24 | $this->model->attributes = [ 25 | 'name' => 'Tester', 26 | 'email' => 'tester@example.com', 27 | 'subject' => 'very important letter subject', 28 | 'body' => 'body of current message', 29 | ]; 30 | 31 | expect_that($this->model->contact('admin@example.com')); 32 | 33 | // using Yii2 module actions to check email was sent 34 | $this->tester->seeEmailIsSent(); 35 | 36 | $emailMessage = $this->tester->grabLastSentEmail(); 37 | expect('valid email is sent', $emailMessage)->isInstanceOf('yii\mail\MessageInterface'); 38 | expect($emailMessage->getTo())->hasKey('admin@example.com'); 39 | expect($emailMessage->getFrom())->hasKey('tester@example.com'); 40 | expect($emailMessage->getSubject())->equals('very important letter subject'); 41 | expect($emailMessage->toString())->contains('body of current message'); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /tests/unit/models/LoginFormTest.php: -------------------------------------------------------------------------------- 1 | user->logout(); 14 | } 15 | 16 | public function testLoginNoUser() 17 | { 18 | $this->model = new LoginForm([ 19 | 'username' => 'not_existing_username', 20 | 'password' => 'not_existing_password', 21 | ]); 22 | 23 | expect_not($this->model->login()); 24 | expect_that(\Yii::$app->user->isGuest); 25 | } 26 | 27 | public function testLoginWrongPassword() 28 | { 29 | $this->model = new LoginForm([ 30 | 'username' => 'demo', 31 | 'password' => 'wrong_password', 32 | ]); 33 | 34 | expect_not($this->model->login()); 35 | expect_that(\Yii::$app->user->isGuest); 36 | expect($this->model->errors)->hasKey('password'); 37 | } 38 | 39 | public function testLoginCorrect() 40 | { 41 | $this->model = new LoginForm([ 42 | 'username' => 'demo', 43 | 'password' => 'demo', 44 | ]); 45 | 46 | expect_that($this->model->login()); 47 | expect_not(\Yii::$app->user->isGuest); 48 | expect($this->model->errors)->hasntKey('password'); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /tests/unit/models/UserTest.php: -------------------------------------------------------------------------------- 1 | username)->equals('admin'); 13 | 14 | expect_not(User::findIdentity(999)); 15 | } 16 | 17 | public function testFindUserByAccessToken() 18 | { 19 | expect_that($user = User::findIdentityByAccessToken('100-token')); 20 | expect($user->username)->equals('admin'); 21 | 22 | expect_not(User::findIdentityByAccessToken('non-existing')); 23 | } 24 | 25 | public function testFindUserByUsername() 26 | { 27 | expect_that($user = User::findByUsername('admin')); 28 | expect_not(User::findByUsername('not-admin')); 29 | } 30 | 31 | /** 32 | * @depends testFindUserByUsername 33 | */ 34 | public function testValidateUser($user) 35 | { 36 | $user = User::findByUsername('admin'); 37 | expect_that($user->validateAuthKey('test100key')); 38 | expect_not($user->validateAuthKey('test102key')); 39 | 40 | expect_that($user->validatePassword('admin')); 41 | expect_not($user->validatePassword('123456')); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /tests/unit/repositories/BaseRepositoryTest.php: -------------------------------------------------------------------------------- 1 | repository->add($employee = (new EmployeeBuilder())->build()); 24 | 25 | $found = $this->repository->get($employee->getId()); 26 | 27 | $this->assertNotNull($found); 28 | $this->assertEquals($employee->getId(), $found->getId()); 29 | } 30 | 31 | public function testGetNotFound(): void 32 | { 33 | $this->expectException(NotFoundException::class); 34 | 35 | $this->repository->get(new Id(uniqid())); 36 | } 37 | 38 | public function testAdd(): void 39 | { 40 | $employee = (new EmployeeBuilder()) 41 | ->withPhones([ 42 | new Phone(7, '888', '00000001'), 43 | new Phone(7, '888', '00000002'), 44 | ]) 45 | ->build(); 46 | 47 | $this->repository->add($employee); 48 | 49 | $found = $this->repository->get($employee->getId()); 50 | 51 | $this->assertEquals($employee->getId(), $found->getId()); 52 | $this->assertEquals($employee->getName(), $found->getName()); 53 | $this->assertEquals($employee->getAddress(), $found->getAddress()); 54 | 55 | $this->assertEquals( 56 | $employee->getCreateDate()->getTimestamp(), 57 | $found->getCreateDate()->getTimestamp() 58 | ); 59 | 60 | $this->checkPhones($employee->getPhones(), $found->getPhones()); 61 | $this->checkStatuses($employee->getStatuses(), $found->getStatuses()); 62 | } 63 | 64 | public function testSave(): void 65 | { 66 | $employee = (new EmployeeBuilder()) 67 | ->withPhones([ 68 | new Phone(7, '888', '00000001'), 69 | new Phone(7, '888', '00000002'), 70 | ]) 71 | ->build(); 72 | 73 | $this->repository->add($employee); 74 | 75 | $edit = $this->repository->get($employee->getId()); 76 | 77 | $edit->rename($name = new Name('New', 'Test', 'Name')); 78 | $edit->addPhone($phone = new Phone(7, '888', '00000003')); 79 | $edit->archive(new \DateTimeImmutable()); 80 | 81 | $this->repository->save($edit); 82 | 83 | $found = $this->repository->get($employee->getId()); 84 | 85 | $this->assertTrue($found->isArchived()); 86 | $this->assertEquals($name, $found->getName()); 87 | 88 | $this->checkPhones($edit->getPhones(), $found->getPhones()); 89 | $this->checkStatuses($edit->getStatuses(), $found->getStatuses()); 90 | } 91 | 92 | public function testRemove(): void 93 | { 94 | $id = new Id(uniqid()); 95 | $employee = (new EmployeeBuilder())->withId($id)->build(); 96 | $this->repository->add($employee); 97 | 98 | $this->repository->remove($employee); 99 | 100 | $this->expectException(NotFoundException::class); 101 | 102 | $this->repository->get($id); 103 | } 104 | 105 | private function checkPhones(array $expected, array $actual): void 106 | { 107 | $phoneData = static function (Phone $phone) { 108 | return $phone->getFull(); 109 | }; 110 | 111 | $this->assertEquals( 112 | array_map($phoneData, $expected), 113 | array_map($phoneData, $actual) 114 | ); 115 | } 116 | 117 | private function checkStatuses(array $expected, array $actual): void 118 | { 119 | $statusData = static function (Status $status) { 120 | return [ 121 | 'value' => $status->getValue(), 122 | 'date' => $status->getDate()->getTimestamp(), 123 | ]; 124 | }; 125 | 126 | $this->assertEquals( 127 | array_map($statusData, $expected), 128 | array_map($statusData, $actual) 129 | ); 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /tests/unit/repositories/MemoryEmployeeRepositoryTest.php: -------------------------------------------------------------------------------- 1 | repository = new MemoryEmployeeRepository(); 17 | } 18 | } -------------------------------------------------------------------------------- /views/layouts/main.php: -------------------------------------------------------------------------------- 1 | 15 | beginPage() ?> 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | <?= Html::encode($this->title) ?> 24 | head() ?> 25 | 26 | 27 | beginBody() ?> 28 | 29 |
30 | Yii::$app->name, 33 | 'brandUrl' => Yii::$app->homeUrl, 34 | 'options' => [ 35 | 'class' => 'navbar-inverse navbar-fixed-top', 36 | ], 37 | ]); 38 | echo Nav::widget([ 39 | 'options' => ['class' => 'navbar-nav navbar-right'], 40 | 'items' => [ 41 | ['label' => 'Home', 'url' => ['/site/index']], 42 | ['label' => 'About', 'url' => ['/site/about']], 43 | ['label' => 'Contact', 'url' => ['/site/contact']], 44 | Yii::$app->user->isGuest ? ( 45 | ['label' => 'Login', 'url' => ['/site/login']] 46 | ) : ( 47 | '
  • ' 48 | . Html::beginForm(['/site/logout'], 'post') 49 | . Html::submitButton( 50 | 'Logout (' . Yii::$app->user->identity->username . ')', 51 | ['class' => 'btn btn-link logout'] 52 | ) 53 | . Html::endForm() 54 | . '
  • ' 55 | ) 56 | ], 57 | ]); 58 | NavBar::end(); 59 | ?> 60 | 61 |
    62 | isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [], 64 | ]) ?> 65 | 66 | 67 |
    68 |
    69 | 70 | 77 | 78 | endBody() ?> 79 | 80 | 81 | endPage() ?> 82 | -------------------------------------------------------------------------------- /views/site/about.php: -------------------------------------------------------------------------------- 1 | title = 'About'; 8 | $this->params['breadcrumbs'][] = $this->title; 9 | ?> 10 |
    11 |

    title) ?>

    12 | 13 |

    14 | This is the About page. You may modify the following file to customize its content: 15 |

    16 | 17 | 18 |
    19 | -------------------------------------------------------------------------------- /views/site/contact.php: -------------------------------------------------------------------------------- 1 | title = 'Contact'; 12 | $this->params['breadcrumbs'][] = $this->title; 13 | ?> 14 |
    15 |

    title) ?>

    16 | 17 | session->hasFlash('contactFormSubmitted')): ?> 18 | 19 |
    20 | Thank you for contacting us. We will respond to you as soon as possible. 21 |
    22 | 23 |

    24 | Note that if you turn on the Yii debugger, you should be able 25 | to view the mail message on the mail panel of the debugger. 26 | mailer->useFileTransport): ?> 27 | Because the application is in development mode, the email is not sent but saved as 28 | a file under mailer->fileTransportPath) ?>. 29 | Please configure the useFileTransport property of the mail 30 | application component to be false to enable email sending. 31 | 32 |

    33 | 34 | 35 | 36 |

    37 | If you have business inquiries or other questions, please fill out the following form to contact us. 38 | Thank you. 39 |

    40 | 41 |
    42 |
    43 | 44 | 'contact-form']); ?> 45 | 46 | field($model, 'name')->textInput(['autofocus' => true]) ?> 47 | 48 | field($model, 'email') ?> 49 | 50 | field($model, 'subject') ?> 51 | 52 | field($model, 'body')->textarea(['rows' => 6]) ?> 53 | 54 | field($model, 'verifyCode')->widget(Captcha::className(), [ 55 | 'template' => '
    {image}
    {input}
    ', 56 | ]) ?> 57 | 58 |
    59 | 'btn btn-primary', 'name' => 'contact-button']) ?> 60 |
    61 | 62 | 63 | 64 |
    65 |
    66 | 67 | 68 |
    69 | -------------------------------------------------------------------------------- /views/site/error.php: -------------------------------------------------------------------------------- 1 | title = $name; 11 | ?> 12 |
    13 | 14 |

    title) ?>

    15 | 16 |
    17 | 18 |
    19 | 20 |

    21 | The above error occurred while the Web server was processing your request. 22 |

    23 |

    24 | Please contact us if you think this is a server error. Thank you. 25 |

    26 | 27 |
    28 | -------------------------------------------------------------------------------- /views/site/index.php: -------------------------------------------------------------------------------- 1 | title = 'My Yii Application'; 6 | ?> 7 |
    8 | 9 |
    10 |

    Congratulations!

    11 | 12 |

    You have successfully created your Yii-powered application.

    13 | 14 |

    Get started with Yii

    15 |
    16 | 17 |
    18 | 19 |
    20 |
    21 |

    Heading

    22 | 23 |

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et 24 | dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip 25 | ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu 26 | fugiat nulla pariatur.

    27 | 28 |

    Yii Documentation »

    29 |
    30 |
    31 |

    Heading

    32 | 33 |

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et 34 | dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip 35 | ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu 36 | fugiat nulla pariatur.

    37 | 38 |

    Yii Forum »

    39 |
    40 |
    41 |

    Heading

    42 | 43 |

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et 44 | dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip 45 | ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu 46 | fugiat nulla pariatur.

    47 | 48 |

    Yii Extensions »

    49 |
    50 |
    51 | 52 |
    53 |
    54 | -------------------------------------------------------------------------------- /views/site/login.php: -------------------------------------------------------------------------------- 1 | title = 'Login'; 11 | $this->params['breadcrumbs'][] = $this->title; 12 | ?> 13 |
    14 |

    title) ?>

    15 | 16 |

    Please fill out the following fields to login:

    17 | 18 | 'login-form', 20 | 'layout' => 'horizontal', 21 | 'fieldConfig' => [ 22 | 'template' => "{label}\n
    {input}
    \n
    {error}
    ", 23 | 'labelOptions' => ['class' => 'col-lg-1 control-label'], 24 | ], 25 | ]); ?> 26 | 27 | field($model, 'username')->textInput(['autofocus' => true]) ?> 28 | 29 | field($model, 'password')->passwordInput() ?> 30 | 31 | field($model, 'rememberMe')->checkbox([ 32 | 'template' => "
    {input} {label}
    \n
    {error}
    ", 33 | ]) ?> 34 | 35 |
    36 |
    37 | 'btn btn-primary', 'name' => 'login-button']) ?> 38 |
    39 |
    40 | 41 | 42 | 43 |
    44 | You may login with admin/admin or demo/demo.
    45 | To modify the username/password, please check out the code app\models\User::$users. 46 |
    47 |
    48 | -------------------------------------------------------------------------------- /web/assets/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /web/css/site.css: -------------------------------------------------------------------------------- 1 | html, 2 | body { 3 | height: 100%; 4 | } 5 | 6 | .wrap { 7 | min-height: 100%; 8 | height: auto; 9 | margin: 0 auto -60px; 10 | padding: 0 0 60px; 11 | } 12 | 13 | .wrap > .container { 14 | padding: 70px 15px 20px; 15 | } 16 | 17 | .footer { 18 | height: 60px; 19 | background-color: #f5f5f5; 20 | border-top: 1px solid #ddd; 21 | padding-top: 20px; 22 | } 23 | 24 | .jumbotron { 25 | text-align: center; 26 | background-color: transparent; 27 | } 28 | 29 | .jumbotron .btn { 30 | font-size: 21px; 31 | padding: 14px 24px; 32 | } 33 | 34 | .not-set { 35 | color: #c55; 36 | font-style: italic; 37 | } 38 | 39 | /* add sorting icons to gridview sort links */ 40 | a.asc:after, a.desc:after { 41 | position: relative; 42 | top: 1px; 43 | display: inline-block; 44 | font-family: 'Glyphicons Halflings'; 45 | font-style: normal; 46 | font-weight: normal; 47 | line-height: 1; 48 | padding-left: 5px; 49 | } 50 | 51 | a.asc:after { 52 | content: /*"\e113"*/ "\e151"; 53 | } 54 | 55 | a.desc:after { 56 | content: /*"\e114"*/ "\e152"; 57 | } 58 | 59 | .sort-numerical a.asc:after { 60 | content: "\e153"; 61 | } 62 | 63 | .sort-numerical a.desc:after { 64 | content: "\e154"; 65 | } 66 | 67 | .sort-ordinal a.asc:after { 68 | content: "\e155"; 69 | } 70 | 71 | .sort-ordinal a.desc:after { 72 | content: "\e156"; 73 | } 74 | 75 | .grid-view th { 76 | white-space: nowrap; 77 | } 78 | 79 | .hint-block { 80 | display: block; 81 | margin-top: 5px; 82 | color: #999; 83 | } 84 | 85 | .error-summary { 86 | color: #a94442; 87 | background: #fdf7f7; 88 | border-left: 3px solid #eed3d7; 89 | padding: 10px 20px; 90 | margin: 0 0 15px 0; 91 | } 92 | 93 | /* align the logout "link" (button in form) of the navbar */ 94 | .nav li > form > button.logout { 95 | padding: 15px; 96 | border: none; 97 | } 98 | 99 | @media(max-width:767px) { 100 | .nav li > form > button.logout { 101 | display:block; 102 | text-align: left; 103 | width: 100%; 104 | padding: 10px 15px; 105 | } 106 | } 107 | 108 | .nav > li > form > button.logout:focus, 109 | .nav > li > form > button.logout:hover { 110 | text-decoration: none; 111 | } 112 | 113 | .nav > li > form > button.logout:focus { 114 | outline: none; 115 | } 116 | -------------------------------------------------------------------------------- /web/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ElisDN/yii2-demo-aggregates/4702af4154c722f94e73c759548a5cd071a59381/web/favicon.ico -------------------------------------------------------------------------------- /web/index-test.php: -------------------------------------------------------------------------------- 1 | run(); 17 | -------------------------------------------------------------------------------- /web/index.php: -------------------------------------------------------------------------------- 1 | run(); 13 | -------------------------------------------------------------------------------- /web/robots.txt: -------------------------------------------------------------------------------- 1 | User-agent: * 2 | Disallow: -------------------------------------------------------------------------------- /widgets/Alert.php: -------------------------------------------------------------------------------- 1 | session->setFlash('error', 'This is the message'); 12 | * Yii::$app->session->setFlash('success', 'This is the message'); 13 | * Yii::$app->session->setFlash('info', 'This is the message'); 14 | * ``` 15 | * 16 | * Multiple messages could be set as follows: 17 | * 18 | * ```php 19 | * Yii::$app->session->setFlash('error', ['Error 1', 'Error 2']); 20 | * ``` 21 | * 22 | * @author Kartik Visweswaran 23 | * @author Alexander Makarov 24 | */ 25 | class Alert extends \yii\bootstrap\Widget 26 | { 27 | /** 28 | * @var array the alert types configuration for the flash messages. 29 | * This array is setup as $key => $value, where: 30 | * - key: the name of the session flash variable 31 | * - value: the bootstrap alert type (i.e. danger, success, info, warning) 32 | */ 33 | public $alertTypes = [ 34 | 'error' => 'alert-danger', 35 | 'danger' => 'alert-danger', 36 | 'success' => 'alert-success', 37 | 'info' => 'alert-info', 38 | 'warning' => 'alert-warning' 39 | ]; 40 | /** 41 | * @var array the options for rendering the close button tag. 42 | * Array will be passed to [[\yii\bootstrap\Alert::closeButton]]. 43 | */ 44 | public $closeButton = []; 45 | 46 | 47 | /** 48 | * {@inheritdoc} 49 | */ 50 | public function run() 51 | { 52 | $session = Yii::$app->session; 53 | $flashes = $session->getAllFlashes(); 54 | $appendClass = isset($this->options['class']) ? ' ' . $this->options['class'] : ''; 55 | 56 | foreach ($flashes as $type => $flash) { 57 | if (!isset($this->alertTypes[$type])) { 58 | continue; 59 | } 60 | 61 | foreach ((array) $flash as $i => $message) { 62 | echo \yii\bootstrap\Alert::widget([ 63 | 'body' => $message, 64 | 'closeButton' => $this->closeButton, 65 | 'options' => array_merge($this->options, [ 66 | 'id' => $this->getId() . '-' . $type . '-' . $i, 67 | 'class' => $this->alertTypes[$type] . $appendClass, 68 | ]), 69 | ]); 70 | } 71 | 72 | $session->removeFlash($type); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /yii: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | run(); 21 | exit($exitCode); 22 | -------------------------------------------------------------------------------- /yii.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | rem ------------------------------------------------------------- 4 | rem Yii command line bootstrap script for Windows. 5 | rem 6 | rem @author Qiang Xue 7 | rem @link http://www.yiiframework.com/ 8 | rem @copyright Copyright (c) 2008 Yii Software LLC 9 | rem @license http://www.yiiframework.com/license/ 10 | rem ------------------------------------------------------------- 11 | 12 | @setlocal 13 | 14 | set YII_PATH=%~dp0 15 | 16 | if "%PHP_COMMAND%" == "" set PHP_COMMAND=php.exe 17 | 18 | "%PHP_COMMAND%" "%YII_PATH%yii" %* 19 | 20 | @endlocal 21 | --------------------------------------------------------------------------------