├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .phpunit.result.cache ├── CONTRIBUTING.md ├── LICENSE ├── composer.json ├── composer.lock ├── phpunit.xml ├── readme.md ├── src ├── Meta.php ├── Meta │ ├── Config │ │ └── meta.php │ ├── Contracts │ │ ├── MetaCreator.php │ │ └── MetaUpdator.php │ ├── Database │ │ ├── Factories │ │ │ ├── ExampleModelFactory.php │ │ │ └── MetaFactory.php │ │ ├── Migrations │ │ │ └── 2018_04_02_110607_create_meta_table.php │ │ ├── TestCaseMigrations │ │ │ └── 2018_04_02_110607_create_model_table.php │ │ └── database.sqlite │ ├── Facades │ │ └── MetaFacade.php │ ├── Helpers │ │ ├── CreateMetaHelper.php │ │ ├── MetaCollection.php │ │ ├── MetaHelper.php │ │ ├── SetMetaHelper.php │ │ └── UpdateMetaHelper.php │ ├── MetaServiceProvider.php │ ├── Models │ │ ├── CustomMetaTableModel.php │ │ ├── ExampleModel.php │ │ └── Meta.php │ └── Traits │ │ ├── DeleteMeta.php │ │ ├── GetMeta.php │ │ ├── MetaClauses.php │ │ ├── MetableBase.php │ │ └── SetMeta.php ├── Metable.php └── MetableModel.php └── tests ├── Clauses ├── TestOrderByMetaMethod.php ├── TestWhereMetaBetweenMethod.php ├── TestWhereMetaDoesntHaveMethod.php ├── TestWhereMetaHasMethod.php ├── TestWhereMetaInMethod.php ├── TestWhereMetaMethod.php └── TestWhereMetaNull.php ├── SetAndGet ├── TestCascadeMeta.php ├── TestCreateMetaMethod.php ├── TestDecreaseMetaMethod.php ├── TestDeleteMetaMethod.php ├── TestGetMetaMethod.php ├── TestGetMetasMethod.php ├── TestGettingMetaWithProperty.php ├── TestHasMetaMethod.php ├── TestIncreaseMetaMethod.php ├── TestMetaMethod.php ├── TestSetMetaMethod.php ├── TestSetMetaUsingProperty.php ├── TestTruncateMetaMethod.php └── TestUpdateMetaMethod.php ├── StaticMethods ├── TestConvertValueMetaToType.php └── TestGuessType.php ├── TestCase.php ├── TestCustomMetaTable.php ├── TestFactories.php └── TestingHelpers.php /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: Laravel meta package CI 2 | 3 | on: 4 | push: 5 | branches: [master, dev] 6 | pull_request: 7 | branches: [master] 8 | 9 | jobs: 10 | ci: 11 | runs-on: ubuntu-latest 12 | 13 | strategy: 14 | fail-fast: true 15 | matrix: 16 | php: [8, "8.1", "8.2"] 17 | laravel: [8, 9, 10] 18 | exclude: 19 | - php: 8 20 | laravel: 10 21 | # - php: 8 22 | # laravel: 11 23 | # - php: 8.1 24 | # laravel: 11 25 | 26 | steps: 27 | - name: Checkout code 28 | uses: actions/checkout@v2 29 | - name: Setup PHP 30 | uses: shivammathur/setup-php@v2 31 | with: 32 | php-version: ${{ matrix.php }} 33 | extensions: dom, curl, libxml, mbstring, zip 34 | tools: composer:v2 35 | coverage: none 36 | - name: install dependencies 37 | run: | 38 | composer require "laravel/framework=^${{ matrix.laravel }}" --no-update 39 | composer update --prefer-dist --no-interaction --no-progress 40 | - name: Execute tests 41 | run: vendor/bin/phpunit --verbose 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | .idea/ 3 | meta-package-gif.gif 4 | notes.txt 5 | .vscode/ 6 | .phpunit.result.cache -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contribute to laravel-meta 2 | 3 | laravel-meta is an open-source package for laravel that allows you to add meta-data for your models and records 4 | 5 | this package is fully open to contribute, if you want to please get started ;) 6 | 7 | ## Before raising pr 8 | 9 | we will review your pull request and either merge it, request changes, or close it. 10 | 11 | please be sure that your feature is valuable to this package before working on it, so we suggest you first create an issue to discuss about it. 12 | 13 | 14 | - If you've fixed a bug or added code that should be tested, please make sure to add tests 15 | - Please make sure that your new code passes all the existing tests 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Zoha Banam 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "zoha/laravel-meta", 3 | "description": "a package for working with models meta", 4 | "keywords": [ 5 | "meta", 6 | "laravel", 7 | "data", 8 | "package" 9 | ], 10 | "type": "package", 11 | "license": "MIT", 12 | "source": { 13 | "url": "https://github.com/ZohaBanam/laravel-meta.git", 14 | "type": "git", 15 | "reference": "0ac3f7b" 16 | }, 17 | "require": { 18 | "laravel/framework": ">=8.0" 19 | }, 20 | "require-dev": { 21 | "php": ">=8", 22 | "phpunit/phpunit": "^9", 23 | "orchestra/testbench": ">=6.0" 24 | }, 25 | "authors": [ 26 | { 27 | "name": "Zoha Banam", 28 | "email": "zoha.banam@gmail.com" 29 | } 30 | ], 31 | "autoload": { 32 | "psr-4": { 33 | "Zoha\\": "src/" 34 | } 35 | }, 36 | "autoload-dev": { 37 | "psr-4": { 38 | "Zoha\\Meta\\Tests\\": "tests/" 39 | } 40 | }, 41 | "extra": { 42 | "laravel": { 43 | "providers": [ 44 | "Zoha\\Meta\\MetaServiceProvider" 45 | ], 46 | "aliases": { 47 | "Meta": "Zoha\\Meta\\Facades\\MetaFacade" 48 | } 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 20 | 21 | 22 | 23 | 24 | 25 | ./tests 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/Meta.php: -------------------------------------------------------------------------------- 1 | [ 21 | 22 | // default table for all models 23 | 24 | 'default' => 'meta', 25 | 26 | // custom tables list 27 | 28 | 'custom' => [ 29 | 30 | // example : 'posts_meta' , 'users_meta' 31 | 32 | ], 33 | ] 34 | ]; -------------------------------------------------------------------------------- /src/Meta/Contracts/MetaCreator.php: -------------------------------------------------------------------------------- 1 | key of meta

15 | * @param mixed $value 16 | * @param $type

this type must be const from Meta class

17 | * @return bool 18 | */ 19 | public static function createMeta($instance, $key, $value, $type); 20 | } -------------------------------------------------------------------------------- /src/Meta/Contracts/MetaUpdator.php: -------------------------------------------------------------------------------- 1 | key of meta

14 | * @param mixed $value 15 | * @param $type

this type must be const from Meta class

16 | * @return bool : true on success false if meta not founded 17 | */ 18 | public static function updateMeta($instance, $key, $value, $type); 19 | } -------------------------------------------------------------------------------- /src/Meta/Database/Factories/ExampleModelFactory.php: -------------------------------------------------------------------------------- 1 | $this->faker->sentence(10), 26 | ]; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Meta/Database/Factories/MetaFactory.php: -------------------------------------------------------------------------------- 1 | $this->faker->unique()->word, 28 | 'value' => $this->faker->sentence(3), 29 | 'type' => MetaHelper::META_TYPE_STRING, 30 | 'status' => true 31 | ]; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Meta/Database/Migrations/2018_04_02_110607_create_meta_table.php: -------------------------------------------------------------------------------- 1 | charset = 'utf8'; 19 | $table->collation = 'utf8_unicode_ci'; 20 | $table->increments('id'); 21 | $table->string('key', 110); 22 | $table->text('value')->nullable(); 23 | $table->string('type')->default(Meta::META_TYPE_STRING); 24 | $table->boolean('status')->default(true); 25 | $table->string('owner_type', 80); 26 | $table->integer('owner_id'); 27 | $table->unique(['key', 'owner_type', 'owner_id']); 28 | $table->timestamps(); 29 | }); 30 | if (!is_null(config('meta.tables.custom')) && is_array(config('meta.tables.custom'))) { 31 | foreach (config('meta.tables.custom') as $customTable) { 32 | Schema::create($customTable, function (Blueprint $table) { 33 | $table->charset = 'utf8'; 34 | $table->collation = 'utf8_unicode_ci'; 35 | $table->increments('id'); 36 | $table->string('key', 110); 37 | $table->text('value')->nullable(); 38 | $table->string('type')->default(Meta::META_TYPE_STRING); 39 | $table->boolean('status')->default(true); 40 | $table->string('owner_type', 80); 41 | $table->integer('owner_id'); 42 | $table->unique(['key', 'owner_type', 'owner_id']); 43 | $table->timestamps(); 44 | }); 45 | } 46 | } 47 | } 48 | 49 | /** 50 | * Reverse the Migrations. 51 | * 52 | * @return void 53 | */ 54 | public function down() 55 | { 56 | Schema::dropIfExists(config('meta.tables.default', 'meta')); 57 | if (!is_null(config('meta.tables.custom')) && is_array(config('meta.tables.custom'))) { 58 | foreach (config('meta.tables.custom') as $customTable) { 59 | Schema::dropIfExists($customTable); 60 | } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/Meta/Database/TestCaseMigrations/2018_04_02_110607_create_model_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 19 | $table->string('title'); 20 | $table->timestamps(); 21 | }); 22 | Schema::create('tests_meta', function (Blueprint $table) { 23 | $table->charset = 'utf8'; 24 | $table->collation = 'utf8_unicode_ci'; 25 | $table->increments('id'); 26 | $table->string('key', 110); 27 | $table->text('value')->nullable(); 28 | $table->string('type')->default(Meta::META_TYPE_STRING); 29 | $table->boolean('status')->default(true); 30 | $table->string('owner_type', 80); 31 | $table->integer('owner_id'); 32 | $table->unique(['key', 'owner_type', 'owner_id']); 33 | $table->timestamps(); 34 | }); 35 | } 36 | 37 | /** 38 | * Reverse the Migrations. 39 | * 40 | * @return void 41 | */ 42 | public function down() 43 | { 44 | Schema::dropIfExists('model'); 45 | Schema::dropIfExists('tests_meta'); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Meta/Database/database.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zoha/laravel-meta/4d3d46d532ceb4c63cd119e9437464bd5e407385/src/Meta/Database/database.sqlite -------------------------------------------------------------------------------- /src/Meta/Facades/MetaFacade.php: -------------------------------------------------------------------------------- 1 | key of meta

16 | * @param mixed $value 17 | * @param $type

this type must be const from Meta class

18 | * @return bool 19 | */ 20 | public static function createMeta($instance, $key, $value, $type) 21 | { 22 | if (!is_string($key) && !is_array($key)) { 23 | return false; 24 | } 25 | // check if meta already exists 26 | $success = static::checkMetaAlreadyExistsOrNot($instance, $key); 27 | if (!$success) { 28 | return false; 29 | } 30 | 31 | //determine types 32 | list($success, $type, $types) = static::determineMetaTypes($key, $value, $type); 33 | if (!$success) { 34 | return false; 35 | } 36 | 37 | //convert arrays and json's to collection 38 | list($success, $key, $value) = static::convertArrayAndCollectionsMetaToJson($key, $value, $type, $types); 39 | if (!$success) { 40 | return false; 41 | } 42 | 43 | static::executeCreateMeta($instance, $key, $value, $type, $types); 44 | return true; 45 | } 46 | 47 | /** 48 | * @override 49 | * check meta already exists or not 50 | * 51 | * @param $instance 52 | * @param $key 53 | * @return array | bool 54 | */ 55 | public static function checkMetaAlreadyExistsOrNot($instance, $key) 56 | { 57 | if (is_array($key)) { 58 | $currentMetaItem = []; 59 | foreach ($key as $keyItem => $keyItemValue) { 60 | if (!is_string($keyItem) || is_int($keyItem)) { 61 | return false; 62 | } 63 | $currentMetaItem[$keyItem] = $instance->getLoadedMeta()->where('key', $keyItem); 64 | if ($currentMetaItem[$keyItem]->count()) { // if meta not founded 65 | return false; 66 | } 67 | } 68 | } else { 69 | $currentMeta = $instance->getLoadedMeta()->where('key', $key); 70 | if ($currentMeta->count()) { // if meta not founded 71 | return false; 72 | } 73 | } 74 | return true; 75 | } 76 | } -------------------------------------------------------------------------------- /src/Meta/Helpers/MetaCollection.php: -------------------------------------------------------------------------------- 1 | model = $model; 28 | $this->temporaryItems = clone $model->getLoadedMeta(); 29 | parent::__construct($this->model->getLoadedMeta()->pluck('value', 'key')); 30 | } 31 | 32 | /** 33 | * return meta values 34 | * 35 | * @param string $property 36 | * @return string|collection 37 | */ 38 | public function __get($property) 39 | { 40 | $checkHasMeta = $this->model->getLoadedMeta() 41 | ->where('key', $property)->where('value', '!=', null); 42 | 43 | if ($checkHasMeta->count()) { 44 | return Meta::returnValue($this->model->getLoadedMeta(), $property); 45 | } 46 | return $this->searchMetaName($property); 47 | } 48 | 49 | /** 50 | * set new meta value 51 | * 52 | * @param $property 53 | * @param $value 54 | * @return void 55 | */ 56 | public function __set($property, $value) 57 | { 58 | $inTemporaryItems = $this->temporaryItems->where('key', $property); 59 | if ($inTemporaryItems->count()) { 60 | $this->updateTemporaryMeta($value, $inTemporaryItems); 61 | } else { 62 | $this->createTemporaryMeta($property, $value, $inTemporaryItems); 63 | } 64 | } 65 | 66 | /** 67 | * save meta changes 68 | * 69 | * return bool 70 | */ 71 | public function save() 72 | { 73 | $changedItems = $this->temporaryItems->where('changed', true); 74 | if ($changedItems->count()) { 75 | return $this->updateDatabaseAndLoadedMetaList($changedItems); 76 | } 77 | return false; 78 | } 79 | 80 | /** 81 | * find proper meta name from given property to __get method 82 | * 83 | * @param $property 84 | * @return Collection|null|string 85 | */ 86 | protected function searchMetaName($property) 87 | { 88 | return (strpos($property, '_')) ? 89 | $this->__get(str_replace('_', ' ', $property)) ? 90 | $this->__get(str_replace('_', ' ', $property)) : 91 | $this->__get(str_replace('_', '-', $property)) 92 | : null; 93 | } 94 | 95 | /** 96 | * updating an existing meta ( temporary ) 97 | * 98 | * @param $value 99 | * @param $inTemporaryItems 100 | */ 101 | protected function updateTemporaryMeta($value, $inTemporaryItems): void 102 | { 103 | $inTemporaryItems = $inTemporaryItems->first(); 104 | $inTemporaryItems->value = $value; 105 | $inTemporaryItems->changed = true; 106 | $inTemporaryItems->alreadyExists = true; 107 | } 108 | 109 | /** 110 | * create new temporary meta 111 | * 112 | * @param $property 113 | * @param $value 114 | * @param $inTemporaryItems 115 | */ 116 | protected function createTemporaryMeta($property, $value, $inTemporaryItems): void 117 | { 118 | $meta = new \Zoha\Meta\Models\Meta(); 119 | $meta->key = $property; 120 | $meta->value = $value; 121 | $meta->changed = true; 122 | $inTemporaryItems->alreadyExists = false; 123 | $this->temporaryItems->add($meta); 124 | } 125 | 126 | /** 127 | * create new items in database from temporary list 128 | * 129 | * @param $newItems 130 | */ 131 | protected function finalCreateMetaFromTemporary($newItems): void 132 | { 133 | foreach ($newItems as $newItemKey => $newItemValue) { 134 | $this->model->loadedMeta = $this->model->getLoadedMeta()->reject(function ($value) use ($newItemKey 135 | ) { 136 | return $value->key === $newItemKey; 137 | }); 138 | } 139 | $this->model->createMeta($newItems); 140 | } 141 | 142 | /** 143 | * update existing meta items in database and update loaded meta 144 | * 145 | * @param $existsItems 146 | * @param $newItems 147 | * @return mixed 148 | */ 149 | protected function updateExistingItemsFromTemporary($existsItems, $newItems) 150 | { 151 | foreach ($existsItems as $existsItemKey => $existsItemValue) { 152 | $onProcessMetaItem = $this->model->getLoadedMeta()->where('key', $existsItemKey)->first(); 153 | unset($onProcessMetaItem->changed); 154 | unset($onProcessMetaItem->alreadyExists); 155 | $result = $this->model->updateMeta($existsItemKey, $existsItemValue); 156 | if ($result == false) { 157 | $newItems[$existsItemKey] = $existsItemValue; 158 | } 159 | } 160 | return $newItems; 161 | } 162 | 163 | /** 164 | * separate items to create and update . 165 | * and send them to other methods to save in db 166 | * 167 | * @param $changedItems 168 | * @return bool 169 | */ 170 | protected function updateDatabaseAndLoadedMetaList($changedItems): bool 171 | { 172 | $existsItems = []; 173 | $newItems = []; 174 | //separate update items and create items . 175 | foreach ($changedItems as $changedItem) { 176 | if ($changedItem->alreadyExists === true) { 177 | $existsItems[$changedItem->key] = $changedItem->value; 178 | } else { 179 | $newItems[$changedItem->key] = $changedItem->value; 180 | } 181 | } 182 | 183 | // update results in database 184 | if (count($existsItems)) { 185 | $newItems = $this->updateExistingItemsFromTemporary($existsItems, $newItems); 186 | } 187 | if (count($newItems)) { 188 | $this->finalCreateMetaFromTemporary($newItems); 189 | } 190 | return true; 191 | } 192 | } -------------------------------------------------------------------------------- /src/Meta/Helpers/MetaHelper.php: -------------------------------------------------------------------------------- 1 | collection of returned meta from db

29 | * @param string $key 30 | * @param string $type 31 | * @return mixed 32 | */ 33 | public static function returnValue(Collection $collection, $key, $type = null) 34 | { 35 | $databaseResult = $collection->where('key', $key); 36 | if ($databaseResult->count()) { 37 | $databaseResult = $databaseResult->first(); 38 | } else { 39 | return null; 40 | } 41 | if ($type !== null) { 42 | return static::convertMetaValueToType($databaseResult->value, $type); 43 | } 44 | if ($databaseResult->type == null) { 45 | $databaseResult->type = static::guessType($databaseResult->value); 46 | } 47 | return static::convertMetaValueToType($databaseResult->value, $databaseResult->type); 48 | } 49 | 50 | /** 51 | * guess type of given meta value 52 | * 53 | * @param mixed $value

value to guess

54 | * @return string 55 | */ 56 | public static function guessType($value) 57 | { 58 | if ($value === null) { 59 | return MetaHelper::META_TYPE_NULL; 60 | } 61 | if (is_array($value)) { 62 | return MetaHelper::META_TYPE_ARRAY; 63 | } 64 | if ( 65 | is_string($value) && 66 | !is_object(json_decode($value)) && 67 | !is_array(json_decode($value)) && 68 | !preg_match('/^[0-9\.]+$/', $value)) { 69 | return MetaHelper::META_TYPE_STRING; 70 | } 71 | if (is_string($value) && (is_object(json_decode($value)) || is_array(json_decode($value)))) { 72 | return MetaHelper::META_TYPE_JSON; 73 | } 74 | if ($value instanceof Collection || $value instanceof \Illuminate\Support\Collection) { 75 | return MetaHelper::META_TYPE_COLLECTION; 76 | } 77 | if (($value === true || $value === false) && $value !== 1 && $value !== 0) { 78 | return MetaHelper::META_TYPE_BOOLEAN; 79 | } 80 | if (preg_match('/^[0-9]+\.[0-9]+$/', $value)) { 81 | return MetaHelper::META_TYPE_FLOAT; 82 | } 83 | if (preg_match('/^\d+$/', $value)) { 84 | return MetaHelper::META_TYPE_INTEGER; 85 | } 86 | return MetaHelper::META_TYPE_STRING; 87 | } 88 | 89 | /** 90 | * convert given value to proper type by guessing or custom type 91 | * 92 | * @param mixed $value 93 | * @param string $type

custom value type : should be a const in meta class

94 | * @return mixed 95 | */ 96 | public static function convertMetaValueToType($value, $type = null) 97 | { 98 | $finalType = MetaHelper::guessType($value); 99 | if ($type !== null) { 100 | $finalType = $type; 101 | } 102 | switch ($finalType) { 103 | case MetaHelper::META_TYPE_STRING : 104 | $value = self::convertToStringType($value); 105 | break; 106 | case MetaHelper::META_TYPE_INTEGER: 107 | $value = self::convertToIntegerType($value); 108 | break; 109 | case MetaHelper::META_TYPE_FLOAT: 110 | $value = self::convertToFloatType($value); 111 | break; 112 | case MetaHelper::META_TYPE_COLLECTION : 113 | $value = self::convertToCollectionType($value); 114 | break; 115 | case MetaHelper::META_TYPE_JSON: 116 | $value = self::convertToJsonType($value); 117 | break; 118 | case MetaHelper::META_TYPE_ARRAY: 119 | $value = self::convertToArrayType($value); 120 | break; 121 | case MetaHelper::META_TYPE_BOOLEAN : 122 | $value = self::convertToBooleanType($value); 123 | break; 124 | case MetaHelper::META_TYPE_NULL : 125 | $value = null; 126 | break; 127 | } 128 | return $value; 129 | } 130 | 131 | /** 132 | * return true if value is empty for different meta type 133 | * 134 | * @param mixed $value 135 | * @return bool 136 | */ 137 | public static function isNullValue($value) 138 | { 139 | return ( 140 | $value !== 0 && ( 141 | $value === [] || 142 | $value === null || 143 | $value === collect([]) || 144 | empty($value) || 145 | $value === '{}' || 146 | $value === '[]') 147 | ); 148 | } 149 | 150 | /** 151 | * convert given value to json if is collection or array 152 | * 153 | * @param mixed $value 154 | * @return mixed 155 | */ 156 | public static function convertMetaValueForSearch($value) 157 | { 158 | if (is_array($value)) { 159 | return json_encode($value); 160 | } elseif ($value instanceof Collection || $value instanceof \Illuminate\Support\Collection) { 161 | return $value->toJson(); 162 | } elseif (is_bool($value)) { 163 | return ($value == true) ? '1' : '0'; 164 | } 165 | return $value; 166 | } 167 | 168 | /** 169 | * convert given value to string 170 | * 171 | * @param $value 172 | * @return string 173 | */ 174 | protected static function convertToStringType($value): string 175 | { 176 | if (is_array($value)) { 177 | $value = json_encode($value); 178 | } elseif ($value instanceof Collection || $value instanceof \Illuminate\Support\Collection) { 179 | $value = $value->toJson(); 180 | } elseif (is_bool($value)) { 181 | $value = $value == true ? 'true' : 'false'; 182 | } 183 | $value = (string)$value; 184 | return $value; 185 | } 186 | 187 | /** 188 | * convert given value to integer 189 | * 190 | * @param $value 191 | * @return float|int 192 | */ 193 | protected static function convertToIntegerType($value) 194 | { 195 | if ($value instanceof Collection || $value instanceof \Illuminate\Support\Collection || is_array($value)) { 196 | $value = 0; 197 | } 198 | return (int)$value; 199 | } 200 | 201 | /** 202 | * convert given value to integer 203 | * 204 | * @param $value 205 | * @return float|int 206 | */ 207 | protected static function convertToFloatType($value) 208 | { 209 | if ($value instanceof Collection || $value instanceof \Illuminate\Support\Collection || is_array($value)) { 210 | $value = 0; 211 | } 212 | return (float)$value; 213 | } 214 | 215 | /** 216 | * convert given value to collection 217 | * 218 | * @param $value 219 | * @return Collection|\Illuminate\Support\Collection|string 220 | */ 221 | protected static function convertToCollectionType($value) 222 | { 223 | if (is_string($value) && (is_object(json_decode($value)) || is_array(json_decode($value)))) { 224 | $value = collect(json_decode($value, true)); 225 | } elseif (is_array($value)) { 226 | $value = collect($value); 227 | } else { 228 | $value = ( 229 | $value instanceof Collection || $value instanceof \Illuminate\Support\Collection 230 | ) ? $value : collect([]); 231 | } 232 | return $value; 233 | } 234 | 235 | /** 236 | * convert given value to json 237 | * 238 | * @param $value 239 | * @return string 240 | */ 241 | protected static function convertToJsonType($value): string 242 | { 243 | if ($value instanceof Collection || $value instanceof \Illuminate\Support\Collection) { 244 | $value = $value->toJson(); 245 | } elseif (is_array($value)) { 246 | $value = json_encode($value); 247 | } else { 248 | $value = is_string($value) && (is_object(json_decode($value)) || is_array(json_decode($value))) ? $value : '{}'; 249 | } 250 | return $value; 251 | } 252 | 253 | /** 254 | * convert given value to array 255 | * 256 | * @param $value 257 | * @return array|mixed|string 258 | */ 259 | protected static function convertToArrayType($value) 260 | { 261 | if ($value instanceof Collection || $value instanceof \Illuminate\Support\Collection) { 262 | $value = $value->toArray(); 263 | } elseif (is_string($value) && (is_object(json_decode($value)) || is_array(json_decode($value)))) { 264 | $value = json_decode($value, true); 265 | } else { 266 | $value = is_array($value) ? $value : []; 267 | } 268 | return $value; 269 | } 270 | 271 | /** 272 | * convert given value to boolean 273 | * 274 | * @param $value 275 | * @return bool 276 | */ 277 | protected static function convertToBooleanType($value): bool 278 | { 279 | if ($value instanceof Collection || $value instanceof \Illuminate\Support\Collection) { 280 | $value = true; 281 | } elseif (is_string($value)) { 282 | if ($value === 'true') { 283 | $value = true; 284 | } 285 | if ($value === 'false') { 286 | $value = false; 287 | } 288 | } 289 | $value = (boolean)$value; 290 | return $value; 291 | } 292 | } 293 | -------------------------------------------------------------------------------- /src/Meta/Helpers/SetMetaHelper.php: -------------------------------------------------------------------------------- 1 | $keyItemValue) { 28 | if ($value === null) { 29 | $thisItemType = Meta::guessType($keyItemValue); 30 | if ($thisItemType == Meta::META_TYPE_COLLECTION || $thisItemType == Meta::META_TYPE_ARRAY || $thisItemType == Meta::META_TYPE_JSON) { 31 | $thisItemType = Meta::META_TYPE_COLLECTION; 32 | } 33 | $types[$keyItem] = $thisItemType; 34 | } else { 35 | $types[$keyItem] = $value; 36 | } 37 | } 38 | } 39 | return [true, $type, $types]; 40 | } 41 | 42 | /** 43 | * convert collections and collections to json for insert in database 44 | * 45 | * @param $key 46 | * @param $value 47 | * @param $type 48 | * @param $types 49 | * @return array 50 | */ 51 | public static function convertArrayAndCollectionsMetaToJson($key, $value, $type, $types) 52 | { 53 | if (is_array($key)) { 54 | foreach ($key as $keyItem => $keyItemValue) { 55 | if ($types[$keyItem] == Meta::META_TYPE_COLLECTION || $types[$keyItem] == Meta::META_TYPE_ARRAY || $types[$keyItem] == Meta::META_TYPE_JSON) { 56 | $key[$keyItem] = Meta::convertMetaValueToType($keyItemValue, Meta::META_TYPE_JSON); 57 | } else { 58 | $key[$keyItem] = Meta::convertMetaValueToType($keyItemValue, $types[$keyItem]); 59 | } 60 | } 61 | } else { 62 | if ($type == Meta::META_TYPE_COLLECTION || $type == Meta::META_TYPE_ARRAY || $type == Meta::META_TYPE_JSON) { 63 | $value = Meta::convertMetaValueToType($value, Meta::META_TYPE_JSON); 64 | } else { 65 | $value = Meta::convertMetaValueToType($value, $type); 66 | } 67 | } 68 | return [true, $key, $value]; 69 | } 70 | 71 | /** 72 | * check meta already exists or not 73 | * 74 | * @param $instance 75 | * @param $key 76 | * @return array | bool 77 | */ 78 | public static function checkMetaAlreadyExistsOrNot($instance, $key) 79 | { 80 | $currentMeta = null; 81 | $currentMetaItem = null; 82 | if (is_array($key)) { 83 | $currentMetaItem = []; 84 | foreach ($key as $keyItem => $keyItemValue) { 85 | if (!is_string($keyItem) || is_int($keyItem)) { 86 | return [false, null, null]; 87 | } 88 | $currentMetaItem[$keyItem] = $instance->getLoadedMeta()->where('key', $keyItem); 89 | if (!$currentMetaItem[$keyItem]->count()) { // if meta not founded 90 | return [false, null, null]; 91 | } 92 | } 93 | } else { 94 | $currentMeta = $instance->getLoadedMeta()->where('key', $key); 95 | if (!$currentMeta->count()) { // if meta not founded 96 | return [false, null, null]; 97 | } 98 | } 99 | return [true, $currentMeta, $currentMetaItem]; 100 | } 101 | 102 | /** 103 | * final step for update single or multiple meta 104 | * execute update and update values in to database 105 | * 106 | * @param $instance 107 | * @param $key 108 | * @param $value 109 | * @param $type 110 | * @param $types 111 | * @param $currentMetaItem 112 | * @param $currentMeta 113 | */ 114 | public static function executeUpdateMeta( 115 | $instance, 116 | $key, 117 | $value, 118 | $type, 119 | $types, 120 | $currentMetaItem, 121 | $currentMeta 122 | ) { 123 | if (!is_array($key)) { 124 | $currentMeta = $currentMeta->first(); 125 | $currentMeta->type = $type; 126 | $currentMeta->value = $value; 127 | $currentMeta->save(); 128 | $singleUpdatedMeta = $instance->getLoadedMeta()->where('id', $currentMeta->id)->first(); 129 | $singleUpdatedMeta->type = $type; 130 | $singleUpdatedMeta->value = $value; 131 | $instance->refreshLoadedMetaItems(); 132 | } else { 133 | foreach ($key as $keyItem => $keyItemValue) { 134 | $currentMetaItem[$keyItem] = $currentMetaItem[$keyItem]->first(); 135 | $currentMetaItem[$keyItem]->type = $types[$keyItem]; 136 | $currentMetaItem[$keyItem]->value = $keyItemValue; 137 | $currentMetaItem[$keyItem]->save(); 138 | } 139 | foreach ($key as $keyItem => $keyItemValue) { 140 | $singleUpdatedMeta = $instance->getLoadedMeta()->where('id', $currentMetaItem[$keyItem]->id)->first(); 141 | $singleUpdatedMeta->type = $types[$keyItem]; 142 | $singleUpdatedMeta->value = $keyItemValue; 143 | } 144 | $instance->refreshLoadedMetaItems(); 145 | } 146 | } 147 | 148 | /** 149 | * final step for create single or multiple meta 150 | * execute create and insert values in to database 151 | * 152 | * @param $instance 153 | * @param $key 154 | * @param $value 155 | * @param $type 156 | * @param $types 157 | * @param $currentMetaItem 158 | * @param $currentMeta 159 | */ 160 | public static function executeCreateMeta( 161 | $instance, 162 | $key, 163 | $value, 164 | $type, 165 | $types 166 | ) { 167 | if (!is_array($key)) { 168 | $newMeta = new \Zoha\Meta\Models\Meta; 169 | $newMeta->setTable($instance->getMetaTable()); 170 | $newMeta->status = true; 171 | $newMeta->type = $type; 172 | $newMeta->key = $key; 173 | $newMeta->value = $value; 174 | $instance->meta()->save($newMeta); 175 | $instance->getLoadedMeta()->add($newMeta); 176 | $instance->refreshLoadedMetaItems(); 177 | } else { 178 | $currentMetaItem = []; 179 | foreach ($key as $keyItem => $keyItemValue) { 180 | $currentMetaItemTemporary = new \Zoha\Meta\Models\Meta(); 181 | $currentMetaItemTemporary->setTable($instance->getMetaTable()); 182 | $currentMetaItemTemporary->status = true; 183 | $currentMetaItemTemporary->type = $types[$keyItem]; 184 | $currentMetaItemTemporary->key = $keyItem; 185 | $currentMetaItemTemporary->value = $keyItemValue; 186 | $currentMetaItem[] = $currentMetaItemTemporary; 187 | } 188 | $instance->meta()->saveMany($currentMetaItem); 189 | foreach ($currentMetaItem as $singleCreatedMetaItem) { 190 | $instance->getLoadedMeta()->add($singleCreatedMetaItem); 191 | } 192 | $instance->refreshLoadedMetaItems(); 193 | } 194 | } 195 | } -------------------------------------------------------------------------------- /src/Meta/Helpers/UpdateMetaHelper.php: -------------------------------------------------------------------------------- 1 | key of meta

16 | * @param mixed $value 17 | * @param $type

this type must be const from Meta class

18 | * @return bool : true on success false if meta not founded 19 | */ 20 | public static function updateMeta($instance, $key, $value, $type) 21 | { 22 | if (!is_string($key) && !is_array($key)) { 23 | return false; 24 | } 25 | 26 | // check if meta already exists 27 | list($success, $currentMeta, $currentMetaItem) = static::checkMetaAlreadyExistsOrNot($instance, $key); 28 | if (!$success) { 29 | return false; 30 | } 31 | 32 | //determine types 33 | list($success, $type, $types) = static::determineMetaTypes($key, $value, $type); 34 | if (!$success) { 35 | return false; 36 | } 37 | 38 | //convert arrays and json's to collection 39 | list($success, $key, $value) = static::convertArrayAndCollectionsMetaToJson($key, $value, $type, $types); 40 | if (!$success) { 41 | return false; 42 | } 43 | static::executeUpdateMeta($instance, $key, $value, $type, $types, $currentMetaItem, $currentMeta); 44 | return true; 45 | } 46 | } -------------------------------------------------------------------------------- /src/Meta/MetaServiceProvider.php: -------------------------------------------------------------------------------- 1 | registerMigrations(); 42 | 43 | $this->registerPublishes(); 44 | } 45 | 46 | /** 47 | * register migrations for this package 48 | * 49 | * @return void 50 | */ 51 | private function registerMigrations() 52 | { 53 | $this->loadMigrationsFrom($this->migrations); 54 | } 55 | 56 | /** 57 | * register package migrations 58 | * 59 | * @return void 60 | */ 61 | private function registerPublishes() 62 | { 63 | $this->publishes([ 64 | __DIR__ . '/Config/meta.php' => config_path('meta.php'), 65 | ], 'config'); 66 | } 67 | } -------------------------------------------------------------------------------- /src/Meta/Models/CustomMetaTableModel.php: -------------------------------------------------------------------------------- 1 | 'boolean' 16 | ]; 17 | protected $fillable = [ 18 | 'key', 19 | 'value', 20 | 'owner_type', 21 | 'owner_id', 22 | 'status', 23 | 'type' 24 | ]; 25 | 26 | //------------------------------------------ Methods --------------------------------------------// 27 | 28 | /** 29 | * morphTo relation with other models 30 | * 31 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 32 | */ 33 | public function owner() 34 | { 35 | return $this->morphTo(); 36 | } 37 | 38 | public function getMetaTableName() 39 | { 40 | return $this->table; 41 | } 42 | 43 | public function __construct(array $attributes = []) 44 | { 45 | parent::__construct($attributes); 46 | $this->setTable(config('meta.tables.default', 'meta')); 47 | } 48 | 49 | public static function factory() 50 | { 51 | return MetaFactory::new(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/Meta/Traits/DeleteMeta.php: -------------------------------------------------------------------------------- 1 | truncateMeta(); 20 | } 21 | $currentMeta = $this->getLoadedMeta()->where('key', $key); 22 | if (!$currentMeta->count()) { // if meta not founded 23 | return false; 24 | } 25 | $currentMeta = $currentMeta->first(); 26 | 27 | $this->loadedMeta = $this->getLoadedMeta()->reject(function ($value) use ($currentMeta) { 28 | return $value->id === $currentMeta->id; 29 | }); 30 | $currentMeta->delete(); 31 | 32 | $this->refreshLoadedMetaItems(); 33 | return true; 34 | } 35 | 36 | /** 37 | * alias for deleteMeta Method 38 | * 39 | * @param string $key 40 | * @return bool 41 | */ 42 | public function unsetMeta($key = null) 43 | { 44 | return $this->deleteMeta($key); 45 | } 46 | 47 | /** 48 | * delete all meta records for current model 49 | * 50 | * @return bool 51 | */ 52 | public function truncateMeta() 53 | { 54 | foreach ($this->getLoadedMeta() as $meta) { 55 | $meta->delete(); 56 | } 57 | $this->loadedMeta = new Collection([]); 58 | return true; 59 | } 60 | } -------------------------------------------------------------------------------- /src/Meta/Traits/GetMeta.php: -------------------------------------------------------------------------------- 1 | getloadedMeta(), $key, $customType); 25 | if (Meta::isNullValue($metaResult)) { 26 | if ($defaultValue !== Meta::NO_VALUE_FOR_PARAMETER) { 27 | return $defaultValue; 28 | } 29 | 30 | return $metaResult; 31 | } 32 | 33 | return $metaResult; 34 | } 35 | 36 | /** 37 | * get all metas's value and format the value 38 | * 39 | * @return mixed 40 | */ 41 | public function getMetas() 42 | { 43 | return $this->getLoadedMeta()->toBase()->mapWithKeys(function ($item) { 44 | return [$item->key => Meta::returnValue($this->getLoadedMeta(), $item->key)]; 45 | }); 46 | } 47 | } -------------------------------------------------------------------------------- /src/Meta/Traits/MetaClauses.php: -------------------------------------------------------------------------------- 1 | with('metarelation'); 20 | } 21 | 22 | /** 23 | * add scope where clause for filter collection data using meta 24 | * 25 | * @examples 26 | * whereMeta('test' , 'test value') 27 | * whereMeta('test' , '!=' null) 28 | * whereMeta(['test1' => 'test1 value' , 'test2' => 'test2 value' ]) 29 | * whereMeta(['test1' => 'test1 value' , 'test2' => 'test2 value' ]) 30 | * whereMeta([['test1' , '=' , 'test1 value' ] , ['test2' , 'test2 value']]) 31 | * 32 | * @param \Illuminate\Database\Eloquent\Builder $query 33 | * @param string|array $key 34 | * @param string $operator 35 | * @param string $value 36 | * @param boolean $orWhere 37 | * @return \Illuminate\Database\Eloquent\Builder 38 | */ 39 | public function scopeWhereMeta( 40 | $query, 41 | $key, 42 | $operator = Meta::NO_VALUE_FOR_PARAMETER, 43 | $value = Meta::NO_VALUE_FOR_PARAMETER, 44 | $orWhere = false 45 | ) { 46 | $type = 'where'; 47 | if ($orWhere) { 48 | $type = 'orWhere'; 49 | } 50 | if (!is_string($key) && !is_array($key)) { 51 | return $query; 52 | } 53 | if (is_array($key)) { 54 | foreach ($key as $conditionKey => $conditionGroup) { 55 | if (!is_array($conditionGroup)) { // if conditions are just key and value 56 | $query = call_user_func_array( 57 | [$this, 'scopeWhereMeta'], 58 | [$query, $conditionKey, '=', $conditionGroup, $orWhere] 59 | ); 60 | } else { 61 | array_unshift($conditionGroup, $query); 62 | if (count($conditionGroup) == 2) { 63 | $conditionGroup[] = Meta::NO_VALUE_FOR_PARAMETER; 64 | $conditionGroup[] = Meta::NO_VALUE_FOR_PARAMETER; 65 | } 66 | if (count($conditionGroup) == 3) { 67 | $conditionGroup[] = Meta::NO_VALUE_FOR_PARAMETER; 68 | } 69 | $conditionGroup[] = $orWhere; 70 | $query = call_user_func_array([$this, 'scopeWhereMeta'], $conditionGroup); 71 | } 72 | } 73 | return $query; 74 | } 75 | $query->{$type . 'Has'}('meta', function ($query) use ($key, $operator, $value) { 76 | $query->where('key', $key); 77 | if ($operator === Meta::NO_VALUE_FOR_PARAMETER && $value === Meta::NO_VALUE_FOR_PARAMETER) { 78 | $query->where('value', null); 79 | } elseif ($value === Meta::NO_VALUE_FOR_PARAMETER) { 80 | $query->where('value', Meta::convertMetaValueForSearch($operator)); 81 | } else { 82 | $query->where('value', $operator, Meta::convertMetaValueForSearch($value)); 83 | } 84 | }); 85 | return $query; 86 | } 87 | 88 | /** 89 | * add scope orWhere clause for filter collection data using meta 90 | * 91 | * @examples 92 | * orWhereMeta('test' , 'test value') 93 | * orWhereMeta('test' , '!=' null) 94 | * orWhereMeta(['test1' => 'test1 value' , 'test2' => 'test2 value' ]) 95 | * orWhereMeta(['test1' => 'test1 value' , 'test2' => 'test2 value' ]) 96 | * orWhereMeta([['test1' , '=' , 'test1 value' ] , ['test2' , 'test2 value']]) 97 | * 98 | * @param \Illuminate\Database\Eloquent\Builder $query 99 | * @param string|array $key 100 | * @param string $operator 101 | * @param string $value 102 | * @return \Illuminate\Database\Eloquent\Builder 103 | */ 104 | public function scopeOrWhereMeta($query, $key, $operator = null, $value = null) 105 | { 106 | return $this->scopeWhereMeta($query, $key, $operator, $value, true); 107 | } 108 | 109 | /** 110 | * clause for filter items that has given meta and value is between defined values 111 | * 112 | * @param \Illuminate\Database\Eloquent\Builder $query 113 | * @param string $key 114 | * @param array $values of min and max value 115 | * @param boolean $orWhere 116 | * @return \Illuminate\Database\Eloquent\Builder 117 | */ 118 | public function scopeWhereMetaBetween($query, $key, $values = [0, 100], $orWhere = false) 119 | { 120 | if (count($values) != 2 || !isset($values[0]) || !isset($values[1]) || !is_int($values[0]) || !is_int($values[1])) { 121 | return $query; 122 | } 123 | $type = 'where'; 124 | if ($orWhere) { 125 | $type = 'orWhere'; 126 | } 127 | $query->{$type . 'Has'}('meta', function ($query) use ($key, $values) { 128 | $query->where('key', $key); 129 | $query->whereBetween('value', $values); 130 | }); 131 | return $query; 132 | } 133 | 134 | /** 135 | * clause (or) for filter items that has given meta and value is between defined values 136 | * 137 | * @param \Illuminate\Database\Eloquent\Builder $query 138 | * @param string $key 139 | * @param array $values of min and max value 140 | * @return \Illuminate\Database\Eloquent\Builder 141 | */ 142 | public function scopeOrWhereMetaBetween($query, $key, $values = [0, 100]) 143 | { 144 | return $this->scopeWhereMetaBetween($query, $key, $values, true); 145 | } 146 | 147 | /** 148 | * clause for filter items that has given meta and value is not between defined values 149 | * 150 | * @param \Illuminate\Database\Eloquent\Builder $query 151 | * @param string $key 152 | * @param array $values of min and max value 153 | * @param boolean $orWhere 154 | * @return \Illuminate\Database\Eloquent\Builder 155 | */ 156 | public function scopeWhereMetaNotBetween($query, $key, $values = [0, 100], $orWhere = false) 157 | { 158 | if (count($values) != 2 || !isset($values[0]) || !isset($values[1]) || !is_int($values[0]) || !is_int($values[1])) { 159 | return $query; 160 | } 161 | $type = 'where'; 162 | if ($orWhere) { 163 | $type = 'orWhere'; 164 | } 165 | $query->{$type . 'Has'}('meta', function ($query) use ($key, $values) { 166 | $query->where('key', $key); 167 | $query->whereNotBetween('value', $values); 168 | }); 169 | return $query; 170 | } 171 | 172 | /** 173 | * clause (or) for filter items that has given meta and value is not between defined values 174 | * 175 | * @param \Illuminate\Database\Eloquent\Builder $query 176 | * @param string $key 177 | * @param array $values of min and max value 178 | * @return \Illuminate\Database\Eloquent\Builder 179 | */ 180 | public function scopeOrWhereMetaNotBetween($query, $key, $values = [0, 100]) 181 | { 182 | return $this->scopeWhereMetaNotBetween($query, $key, $values, true); 183 | } 184 | 185 | /** 186 | * filter items that have one of the given values 187 | * 188 | * @param \Illuminate\Database\Eloquent\Builder $query 189 | * @param string $key 190 | * @param array $values 191 | * @param boolean $orWhere 192 | * @return \Illuminate\Database\Eloquent\Builder 193 | */ 194 | public function scopeWhereMetaIn($query, $key, $values = [], $orWhere = false) 195 | { 196 | if (!is_array($values)) { 197 | return $query; 198 | } 199 | $type = 'where'; 200 | if ($orWhere) { 201 | $type = 'orWhere'; 202 | } 203 | $query->{$type . 'Has'}('meta', function ($query) use ($key, $values) { 204 | $query->where('key', $key); 205 | $query->whereIn('value', $values); 206 | }); 207 | return $query; 208 | } 209 | 210 | /** 211 | * filter items that have one of the given values ( or clause ) 212 | * 213 | * @param \Illuminate\Database\Eloquent\Builder $query 214 | * @param string $key 215 | * @param array $values 216 | * @return \Illuminate\Database\Eloquent\Builder 217 | */ 218 | public function scopeOrWhereMetaIn($query, $key, $values = []) 219 | { 220 | return $this->scopeWhereMetaIn($query, $key, $values, true); 221 | } 222 | 223 | /** 224 | * filter items that don't have one of the given values 225 | * 226 | * @param \Illuminate\Database\Eloquent\Builder $query 227 | * @param string $key 228 | * @param array $values 229 | * @param boolean $orWhere 230 | * @return \Illuminate\Database\Eloquent\Builder 231 | */ 232 | public function scopeWhereMetaNotIn($query, $key, $values = [], $orWhere = false) 233 | { 234 | if (!is_array($values)) { 235 | return $query; 236 | } 237 | $type = 'where'; 238 | if ($orWhere) { 239 | $type = 'orWhere'; 240 | } 241 | $query->{$type . 'Has'}('meta', function ($query) use ($key, $values) { 242 | $query->where('key', $key); 243 | $query->whereNotIn('value', $values); 244 | }); 245 | return $query; 246 | } 247 | 248 | /** 249 | * filter items that don't have one of the given values ( or clause ) 250 | * 251 | * @param \Illuminate\Database\Eloquent\Builder $query 252 | * @param string $key 253 | * @param array $values 254 | * @return \Illuminate\Database\Eloquent\Builder 255 | */ 256 | public function scopeOrWhereMetaNotIn($query, $key, $values = []) 257 | { 258 | return $this->scopeWhereMetaNotIn($query, $key, $values, true); 259 | } 260 | 261 | /** 262 | * filter items that have null value for specific key 263 | * 264 | * @param \Illuminate\Database\Eloquent\Builder $query 265 | * @param string $key 266 | * @param boolean $orWhere 267 | * @return \Illuminate\Database\Eloquent\Builder 268 | */ 269 | public function scopeWhereMetaNull($query, $key, $orWhere = false) 270 | { 271 | $type = 'where'; 272 | if ($orWhere) { 273 | $type = 'orWhere'; 274 | } 275 | $query->{$type . 'Has'}('meta', function ($query) use ($key) { 276 | $query->where('key', $key); 277 | $query->whereNull('value'); 278 | }); 279 | return $query; 280 | } 281 | 282 | /** 283 | * filter items that have null value for specific key ( or clause ) 284 | * 285 | * @param \Illuminate\Database\Eloquent\Builder $query 286 | * @param string $key 287 | * @return \Illuminate\Database\Eloquent\Builder 288 | */ 289 | public function scopeOrWhereMetaNull($query, $key) 290 | { 291 | return $this->scopeWhereMetaNull($query, $key, true); 292 | } 293 | 294 | /** 295 | * filter items that have not null value for specific key 296 | * 297 | * @param \Illuminate\Database\Eloquent\Builder $query 298 | * @param string $key 299 | * @param boolean $orWhere 300 | * @return \Illuminate\Database\Eloquent\Builder 301 | */ 302 | public function scopeWhereMetaNotNull($query, $key, $orWhere = false) 303 | { 304 | $type = 'where'; 305 | if ($orWhere) { 306 | $type = 'orWhere'; 307 | } 308 | $query->{$type . 'Has'}('meta', function ($query) use ($key) { 309 | $query->where('key', $key); 310 | $query->whereNotNull('value'); 311 | }); 312 | return $query; 313 | } 314 | 315 | /** 316 | * filter items that have not null value for specific key ( or clause ) 317 | * 318 | * @param \Illuminate\Database\Eloquent\Builder $query 319 | * @param string $key 320 | * @return \Illuminate\Database\Eloquent\Builder 321 | */ 322 | public function scopeOrWhereMetaNotNull($query, $key) 323 | { 324 | return $this->scopeWhereMetaNotNull($query, $key, true); 325 | } 326 | 327 | /** 328 | * filter items that have specific meta . 329 | * 330 | * @param Builder $query 331 | * @param string $key 332 | * @param bool $countNull 333 | * @param null $type 334 | * @param bool $orWhere 335 | * @return Builder 336 | */ 337 | public function scopeWhereMetaHas($query, $key = null, $countNull = false, $type = null, $orWhere = false) 338 | { 339 | $typeWhere = 'where'; 340 | if ($orWhere) { 341 | $typeWhere = 'orWhere'; 342 | } 343 | $query->{$typeWhere . 'Has'}('meta', function ($query) use ($key, $countNull, $type) { 344 | if ($key === null) { 345 | return $query; 346 | } 347 | $query->where('key', $key); 348 | if ($countNull === false) { 349 | $query->where('value', '!=', null); 350 | } 351 | if ($type !== null) { 352 | $query->where('type', '=', $type); 353 | } 354 | return $query; 355 | }); 356 | return $query; 357 | } 358 | 359 | /** 360 | * filter items that have specific meta ( or clause ) 361 | * 362 | * @param Builder $query 363 | * @param string $key 364 | * @param bool $countNull 365 | * @param null $type 366 | * @return Builder 367 | */ 368 | public function scopeOrWhereMetaHas($query, $key = null, $countNull = false, $type = null) 369 | { 370 | return $this->scopeWhereMetaHas($query, $key, $countNull, $type, true); 371 | } 372 | 373 | /** 374 | * filter items that doesn't have given meta . 375 | * 376 | * @param Builder $query 377 | * @param string $key 378 | * @param bool $countNull 379 | * @param null $type 380 | * @param bool $orWhere 381 | * @return Builder 382 | */ 383 | public function scopeWhereMetaDoesntHave($query, $key = null, $countNull = false, $type = null, $orWhere = false) 384 | { 385 | $typeWhere = 'where'; 386 | if ($orWhere) { 387 | $typeWhere = 'orWhere'; 388 | } 389 | $query->{$typeWhere . 'DoesntHave'}('meta', function ($query) use ($key, $countNull, $type) { 390 | if ($key === null) { 391 | return $query; 392 | } 393 | $query->where('key', $key); 394 | if ($countNull === false) { 395 | $query->where('value', '!=', null); 396 | } 397 | if ($type !== null) { 398 | $query->where('type', '=', $type); 399 | } 400 | return $query; 401 | }); 402 | return $query; 403 | } 404 | 405 | /** 406 | * filter items that doesn't have given meta .( or clause ) 407 | * 408 | * @param Builder $query 409 | * @param string $key 410 | * @param bool $countNull 411 | * @param null $type 412 | * @return Builder 413 | */ 414 | public function scopeOrWhereMetaDoesntHave($query, $key = null, $countNull = false, $type = null) 415 | { 416 | return $this->scopeWhereMetaDoesntHave($query, $key, $countNull, $type, true); 417 | } 418 | 419 | /** 420 | * filter items using value of meta 421 | * 422 | * 423 | * @param $query 424 | * @param $key 425 | * @param string $direction 426 | * @return Builder 427 | */ 428 | public function scopeOrderByMeta($query, $key, $direction = 'asc') 429 | { 430 | $this->countOfMetaJoins += 1; 431 | $table = $this->getMetaTable(); 432 | return $query->leftJoin($table . ' as meta' . $this->countOfMetaJoins, function ($q) use ($key) { 433 | $q->on('meta' . $this->countOfMetaJoins . '.owner_id', '=', $this->getTable() . ".id"); 434 | $q->where('meta' . $this->countOfMetaJoins . '.owner_type', '=', static::class); 435 | $q->where('meta' . $this->countOfMetaJoins . '.key', $key); 436 | }) 437 | ->orderByRaw("CASE (meta" . $this->countOfMetaJoins . ".key) 438 | WHEN '$key' THEN 1 439 | ELSE 0 440 | END 441 | DESC") 442 | ->orderBy('meta' . $this->countOfMetaJoins . '.value', strtoupper($direction)) 443 | ->select($this->getTable() . ".*"); 444 | } 445 | } 446 | -------------------------------------------------------------------------------- /src/Meta/Traits/MetableBase.php: -------------------------------------------------------------------------------- 1 | isForceDeleting())) { 32 | \Zoha\Meta\Models\Meta::where('owner_type', static::class)->where('owner_id', $modelItem->id)->delete(); 33 | } 34 | }); 35 | } 36 | 37 | /** 38 | * for get meta morphMany relation 39 | * or return meta value by given key 40 | * or add|update meta by given key and value 41 | * 42 | * @example : meta('key') ___> return test meta value 43 | * @example : meta('test','test value') ___> create or update test meta 44 | * @example : meta(['my key' => 'my value']] __> setting new metas 45 | * 46 | * @param mixed $key

name of key for return value or new meta key or array of keys and values

47 | * @param mixed $value

new value of meta

48 | * @param string $type 49 | * @return \Illuminate\Database\Eloquent\Relations\morphMany 50 | */ 51 | public function meta($key = null, $value = Meta::NO_VALUE_FOR_PARAMETER, $type = null) 52 | { 53 | if ($key !== null) { 54 | return $this->processMetaRequest($key, $value, $type); 55 | } 56 | $instance = new \Zoha\Meta\Models\Meta(); 57 | $instance->setTable($this->getMetaTable()); 58 | list($type, $id) = $this->getMorphs('owner', null, null); 59 | $table = $instance->getTable(); 60 | return $this->newMorphMany($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $this->getKeyName()); 61 | } 62 | 63 | /** 64 | * for get meta morphMany relation 65 | * 66 | * @return \Illuminate\Database\Eloquent\Relations\morphMany 67 | */ 68 | public function metarelation() 69 | { 70 | $instance = new \Zoha\Meta\Models\Meta(); 71 | $instance->setTable($this->getMetaTable()); 72 | list($type, $id) = $this->getMorphs('owner', null, null); 73 | $table = $instance->getTable(); 74 | return $this->newMorphMany($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $this->getKeyName()); 75 | } 76 | 77 | /** 78 | * get meta table name 79 | * 80 | * @return string 81 | */ 82 | public function getMetaTable() 83 | { 84 | if(is_null(config('meta.tables.default'))){ 85 | return 'meta'; 86 | }else{ 87 | if($this->metaTable){ 88 | return $this->metaTable; 89 | }else{ 90 | return config('meta.tables.default'); 91 | } 92 | } 93 | } 94 | 95 | /** 96 | * return loadedMeta Items in meta collection type 97 | * 98 | * @return MetaCollection 99 | */ 100 | public function getLoadedMetaItems() 101 | { 102 | if ($this->loadedMetaItems == null) { 103 | $this->refreshLoadedMetaItems(); 104 | } 105 | return $this->loadedMetaItems; 106 | 107 | } 108 | 109 | /** 110 | * return loadedMeta property 111 | * get from database if loadedMeta is null 112 | * 113 | * @return Collection 114 | */ 115 | public function getLoadedMeta() 116 | { 117 | if ($this->loadedMeta == null) { 118 | $this->loadedMeta = $this->metarelation; 119 | } 120 | return $this->loadedMeta; 121 | 122 | } 123 | 124 | /** 125 | * force to refresh current loadedMeta property 126 | * 127 | * @return void 128 | */ 129 | public function refreshLoadedMeta() 130 | { 131 | $this->loadedMeta = $this->meta()->get(); 132 | $this->refreshLoadedMetaItems(); 133 | } 134 | 135 | /** 136 | * refresh loadedMetaItems property list 137 | * 138 | * @return void 139 | */ 140 | public function refreshLoadedMetaItems() 141 | { 142 | $this->loadedMetaItems = new MetaCollection($this); 143 | } 144 | 145 | /** 146 | * process given parameters an acording to theme return meta or create | update a new 147 | * 148 | * @param mixed $key 149 | * @param mixed $value 150 | * @param $type 151 | * @return mixed 152 | */ 153 | private function processMetaRequest($key, $value, $type) 154 | { 155 | if (is_array($key)) { 156 | foreach ($key as $keyItem => $value) { 157 | $setMetaResult = $this->setMeta($keyItem, $value, $type); 158 | if ($setMetaResult === false) { 159 | return false; 160 | } 161 | } 162 | } elseif ($value === Meta::NO_VALUE_FOR_PARAMETER) { 163 | return $this->getMeta($key, null, $type); 164 | } else { 165 | return $this->setMeta($key, $value, $type); 166 | } 167 | return true; 168 | } 169 | 170 | /** 171 | * return meta data for this model by meta property 172 | * 173 | * @param $property 174 | * @return MetaCollection 175 | */ 176 | public function __get($property) 177 | { 178 | if ($property === "meta") { 179 | return $this->getLoadedMetaItems(); 180 | } 181 | return parent::__get($property); 182 | } 183 | 184 | /** 185 | * check an perticular meta exists for this model item 186 | * 187 | * @param string $key 188 | * @param bool $acceptNull 189 | * @param null $type 190 | * @return bool 191 | */ 192 | public function hasMeta($key = null, $acceptNull = false, $type = null) 193 | { 194 | if ($key === null) { 195 | if ($acceptNull == true) { 196 | return !!$this->getLoadedMeta()->count(); 197 | } 198 | return !!$this->getLoadedMeta()->where('value', '!=', null)->count(); 199 | } 200 | $meta = $this->getLoadedMeta()->where('key', $key); 201 | if (!$meta->count()) { 202 | return false; 203 | } 204 | if ($type !== null) { 205 | $returnedType = $meta->first()->type; 206 | if ($type !== $returnedType) { 207 | return false; 208 | } 209 | } 210 | $meta = $this->getMeta($key, null); 211 | if ($meta === null && $acceptNull === false) { 212 | return false; 213 | } 214 | return true; 215 | } 216 | 217 | /** 218 | * check an perticular meta exists for this model item even if value was null 219 | * 220 | * @param string $key 221 | * @return bool 222 | */ 223 | public function existsMeta($key = null) 224 | { 225 | if ($key === null) { 226 | return !!$this->getLoadedMeta()->count(); 227 | } 228 | return $this->hasMeta($key, true); 229 | } 230 | } -------------------------------------------------------------------------------- /src/Meta/Traits/SetMeta.php: -------------------------------------------------------------------------------- 1 | 'testvalue1', 17 | * 'test2' => 'testvalue2', 18 | * 'test3' => 'testvalue3' 19 | * ]); 20 | * @example : setMeta('test1','value1') 21 | * 22 | * @param string|array $key

key of meta

23 | * @param mixed $value 24 | * @param $type

this type must be const from Meta class

25 | * @param null|bool $createMeta 26 | * @return bool : true 27 | */ 28 | public function setMeta($key, $value = null, $type = null, $createMeta = null) 29 | { 30 | // If the type of operation is specified ( createMeta | updateMeta ) 31 | if ($createMeta !== null) { 32 | if ($createMeta === false) { 33 | return UpdateMetaHelper::updateMeta($this, $key, $value, $type); 34 | } elseif ($createMeta === true) { 35 | return CreateMetaHelper::createMeta($this, $key, $value, $type); 36 | } 37 | } 38 | 39 | // handle array of values that passed for the first argument 40 | if (is_array($key)) { 41 | return $this->setMultipleMeta($key, $value); 42 | } 43 | 44 | // check meta exists or not and execute update or create meta operation 45 | $currentMeta = $this->getLoadedMeta()->where('key', $key); 46 | if ($currentMeta->count()) { // if a meta with this key already exists 47 | return UpdateMetaHelper::updateMeta($this, $key, $value, $type); 48 | } else { 49 | return CreateMetaHelper::createMeta($this, $key, $value, $type); 50 | } 51 | } 52 | 53 | /** 54 | * set multiple mta 55 | * 56 | * @param $key 57 | * @param null $value 58 | * @return bool 59 | */ 60 | public function setMultipleMeta($key, $value = null) 61 | { 62 | if (!is_array($key)) { 63 | return false; 64 | } 65 | $results = []; 66 | foreach ($key as $keyItem => $valueItem) { 67 | $results[] = $this->setMeta($keyItem, $valueItem, $value); 68 | } 69 | foreach ($results as $result) { 70 | if ($result === false) { 71 | return false; 72 | } 73 | } 74 | return true; 75 | } 76 | 77 | /** 78 | * updating an existing meta 79 | * 80 | * @param string|array $key

key of meta

81 | * @param mixed $value 82 | * @param $type

this type must be const from Meta class

83 | * @return bool : true on success false if meta not founded 84 | */ 85 | public function updateMeta($key, $value = null, $type = null) 86 | { 87 | return $this->setMeta($key, $value, $type, false); 88 | } 89 | 90 | /** 91 | * create new meta . return false if meta with this features already exists 92 | * note : $key value can not be array in this method 93 | * 94 | * @param string $key

key of meta

95 | * @param mixed $value 96 | * @param $type

this type must be const from Meta class

97 | * @return bool 98 | */ 99 | public function createMeta($key, $value = null, $type = null) 100 | { 101 | return $this->setMeta($key, $value, $type, true); 102 | } 103 | 104 | /** 105 | * alias of createMeta() method 106 | * create new meta. return false if meta with this features already exists 107 | * 108 | * @param string $key

key of meta

109 | * @param mixed $value 110 | * @param $type

this type must be const from Meta class

111 | * @return bool 112 | */ 113 | public function addMeta($key, $value = null, $type = null) 114 | { 115 | return CreateMetaHelper::createMeta($this, $key, $value, $type); 116 | } 117 | 118 | /** 119 | * increase a meta value , create it if not exists 120 | * 121 | * @param string $key 122 | * @param integer $increaseStep 123 | * @return bool 124 | */ 125 | public function increaseMeta($key, $increaseStep = 1) 126 | { 127 | $currentValue = $this->getMeta($key, '000'); 128 | if ($currentValue === '000') { 129 | return $this->createMeta($key, 0); 130 | } elseif (is_int($currentValue) || is_float($currentValue)) { 131 | $currentValue += $increaseStep; 132 | return $this->updateMeta($key, $currentValue); 133 | } 134 | return false; 135 | } 136 | 137 | /** 138 | * decrease a meta value , create it if not exists 139 | * 140 | * @param string $key 141 | * @param int $decreaseStep 142 | * @return bool 143 | */ 144 | public function decreaseMeta($key, $decreaseStep = 1) 145 | { 146 | $currentValue = $this->getMeta($key, '000'); 147 | if ($currentValue === '000') { 148 | return $this->createMeta($key, 0, Meta::META_TYPE_INTEGER); 149 | } elseif (is_int($currentValue) || is_float($currentValue)) { 150 | $currentValue -= $decreaseStep; 151 | return $this->updateMeta($key, $currentValue); 152 | } 153 | return false; 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /src/Metable.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 16 | $this->metaTruncate(); 17 | $this->seeding(); 18 | } 19 | 20 | public function test_order_by_meta_method() 21 | { 22 | $results = ExampleModel::orderByMeta('key1', 'desc')->pluck('id')->toArray(); 23 | $this->assertEquals([4, 2, 3, 1, 5], $results); 24 | 25 | $results = ExampleModel::orderByMeta('key1', 'desc')->orderBy('id', 'desc')->pluck('id')->toArray(); 26 | $this->assertEquals([4, 3, 2, 5, 1], $results); 27 | 28 | $results = ExampleModel::orderByMeta('key1', 'asc')->pluck('id')->toArray(); 29 | $this->assertEquals([1, 5, 2, 3, 4], $results); 30 | 31 | ExampleModel::factory()->create(); 32 | 33 | $results = ExampleModel::orderByMeta('key1', 'desc')->pluck('id')->toArray(); 34 | $this->assertEquals([4, 2, 3, 1, 5, 6], $results); 35 | 36 | $results = ExampleModel::orderByMeta('key1', 'asc')->pluck('id')->toArray(); 37 | $this->assertEquals([1, 5, 2, 3, 4, 6], $results); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/Clauses/TestWhereMetaBetweenMethod.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 21 | $this->metaTruncate(); 22 | $this->seeding(); 23 | } 24 | 25 | public function test_filter_meta_using_where_meta_between_method() 26 | { 27 | $this->assertEquals(5, ExampleModel::count()); 28 | 29 | $result = ExampleModel::whereMetaBetween('key1', [1, 2]); 30 | $this->assertEquals(4, $result->count()); 31 | $this->assertEquals([1, 2, 3, 5], $result->pluck('id')->toArray()); 32 | 33 | $result = ExampleModel::whereMetaBetween('key1', [2, 3]); 34 | $this->assertEquals(3, $result->count()); 35 | $this->assertEquals([2, 3, 4], $result->pluck('id')->toArray()); 36 | } 37 | 38 | public function test_filter_meta_using_or_where_meta_between_method() 39 | { 40 | $this->assertEquals(5, ExampleModel::count()); 41 | 42 | $result = ExampleModel::whereMetaBetween('key1', [1, 2])->orWhereMetaBetween('key1', [2, 3]); 43 | $this->assertEquals(5, $result->count()); 44 | $this->assertEquals([1, 2, 3, 4, 5], $result->pluck('id')->toArray()); 45 | 46 | $result = ExampleModel::whereMetaBetween('key1', [2, 3])->orWhereMetaBetween('key1', [5, 8]); 47 | $this->assertEquals(3, $result->count()); 48 | $this->assertEquals([2, 3, 4], $result->pluck('id')->toArray()); 49 | } 50 | 51 | public function test_filter_meta_using_where_meta_not_between_method() 52 | { 53 | $this->assertEquals(5, ExampleModel::count()); 54 | 55 | $result = ExampleModel::whereMetaNotBetween('key1', [1, 2]); 56 | $this->assertEquals(1, $result->count()); 57 | $this->assertEquals([4], $result->pluck('id')->toArray()); 58 | 59 | $result = ExampleModel::whereMetaNotBetween('key1', [2, 3]); 60 | $this->assertEquals(2, $result->count()); 61 | $this->assertEquals([1, 5], $result->pluck('id')->toArray()); 62 | } 63 | 64 | public function test_filter_meta_using_or_where_meta_not_between_method() 65 | { 66 | $this->assertEquals(5, ExampleModel::count()); 67 | 68 | $result = ExampleModel::whereMetaNotBetween('key1', [1, 2])->orWhereMetaNotBetween('key1', [2, 3]); 69 | $this->assertEquals(3, $result->count()); 70 | $this->assertEquals([1, 4, 5], $result->pluck('id')->toArray()); 71 | 72 | $result = ExampleModel::whereMetaNotBetween('key1', [2, 3])->orWhereMetaNotBetween('key1', [5, 8]); 73 | $this->assertEquals(5, $result->count()); 74 | $this->assertEquals([1, 2, 3, 4, 5], $result->pluck('id')->toArray()); 75 | } 76 | } -------------------------------------------------------------------------------- /tests/Clauses/TestWhereMetaDoesntHaveMethod.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 23 | $this->metaTruncate(); 24 | $this->seeding(); 25 | } 26 | 27 | public function test_meta_has_method() 28 | { 29 | $results = ExampleModel::whereMetaDoesntHave(); 30 | $this->assertEquals(0 , $results->count()); 31 | 32 | $results = ExampleModel::whereMetaDoesntHave('key7'); 33 | $this->assertEquals(4 , $results->count()); 34 | 35 | $results = ExampleModel::whereMetaDoesntHave('key6'); 36 | $this->assertEquals(5 , $results->count()); 37 | 38 | $results = ExampleModel::whereMetaDoesntHave('key6' , true ); 39 | $this->assertEquals(4 , $results->count()); 40 | 41 | $results = ExampleModel::whereMetaDoesntHave('key8' , false , MetaFacade::META_TYPE_INTEGER); 42 | $this->assertEquals(5 , $results->count()); 43 | 44 | $results = ExampleModel::whereMetaDoesntHave('key8' , false , MetaFacade::META_TYPE_ARRAY); 45 | $this->assertEquals(4 , $results->count()); 46 | } 47 | 48 | public function test_meta_has_method_multiple() 49 | { 50 | 51 | $results = ExampleModel::whereMetaDoesntHave('key7')->orWhereMetaDoesntHave(); 52 | $this->assertEquals(4 , $results->count()); 53 | 54 | $results = ExampleModel::whereMetaDoesntHave('key7')->orWhereMetaDoesntHave('key6',true); 55 | $this->assertEquals(4 , $results->count()); 56 | } 57 | } -------------------------------------------------------------------------------- /tests/Clauses/TestWhereMetaHasMethod.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 23 | $this->metaTruncate(); 24 | $this->seeding(); 25 | } 26 | 27 | public function test_meta_has_method() 28 | { 29 | $results = ExampleModel::whereMetaHas(); 30 | $this->assertEquals(5 , $results->count()); 31 | 32 | $results = ExampleModel::whereMetaHas('key7'); 33 | $this->assertEquals(1 , $results->count()); 34 | $this->assertEquals(5 , $results->first()->id); 35 | 36 | $results = ExampleModel::whereMetaHas('key6'); 37 | $this->assertEquals(0 , $results->count()); 38 | 39 | $results = ExampleModel::whereMetaHas('key6' , true ); 40 | $this->assertEquals(1 , $results->count()); 41 | $this->assertEquals(5 , $results->first()->id); 42 | 43 | $results = ExampleModel::whereMetaHas('key8' , false , MetaFacade::META_TYPE_INTEGER); 44 | $this->assertEquals(0 , $results->count()); 45 | 46 | $results = ExampleModel::whereMetaHas('key8' , false , MetaFacade::META_TYPE_ARRAY); 47 | $this->assertEquals(1 , $results->count()); 48 | $this->assertEquals(5 , $results->first()->id); 49 | } 50 | 51 | public function test_meta_has_method_multiple() 52 | { 53 | 54 | $results = ExampleModel::whereMetaHas('key7')->orWhereMetaHas(); 55 | $this->assertEquals(5 , $results->count()); 56 | 57 | $results = ExampleModel::whereMetaHas('key7')->orWhereMetaHas('key6',true); 58 | $this->assertEquals(1 , $results->count()); 59 | $this->assertEquals(5 , $results->first()->id); 60 | 61 | $results = ExampleModel::whereMetaHas('key6' , true )->whereMetaHas('key14'); 62 | $this->assertEquals(0 , $results->count()); 63 | } 64 | } -------------------------------------------------------------------------------- /tests/Clauses/TestWhereMetaInMethod.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 20 | $this->metaTruncate(); 21 | $this->seeding(); 22 | } 23 | 24 | public function test_filter_meta_using_where_meta_in_method() 25 | { 26 | $this->assertEquals(5, ExampleModel::count()); 27 | 28 | $result = ExampleModel::whereMetaIn('key1', [1, 3]); 29 | $this->assertEquals(3, $result->count()); 30 | $this->assertEquals([1, 4, 5], $result->pluck('id')->toArray()); 31 | 32 | $result = ExampleModel::whereMetaIn('key1', [2,3]); 33 | $this->assertEquals(3, $result->count()); 34 | $this->assertEquals([2, 3, 4], $result->pluck('id')->toArray()); 35 | } 36 | 37 | public function test_filter_meta_using_or_where_meta_in_method() 38 | { 39 | $result = ExampleModel::whereMetaIn('key1', [1, 2])->orWhereMetaIn('key1', [2, 3]); 40 | $this->assertEquals(5, $result->count()); 41 | $this->assertEquals([1, 2, 3, 4, 5], $result->pluck('id')->toArray()); 42 | 43 | $result = ExampleModel::whereMetaIn('key1', [1, 3])->orWhereMetaIn('key1', [5, 8]); 44 | $this->assertEquals(3, $result->count()); 45 | $this->assertEquals([1, 4, 5], $result->pluck('id')->toArray()); 46 | } 47 | 48 | public function test_filter_meta_using_where_meta_not_in_method() 49 | { 50 | $this->assertEquals(5, ExampleModel::count()); 51 | 52 | $result = ExampleModel::whereMetaNotIn('key1', [1, 3]); 53 | $this->assertEquals(2, $result->count()); 54 | $this->assertEquals([2,3], $result->pluck('id')->toArray()); 55 | 56 | $result = ExampleModel::whereMetaNotIn('key1', [2, 3]); 57 | $this->assertEquals(2, $result->count()); 58 | $this->assertEquals([1, 5], $result->pluck('id')->toArray()); 59 | } 60 | 61 | public function test_filter_meta_using_or_where_meta_not_in_method() 62 | { 63 | $this->assertEquals(5, ExampleModel::count()); 64 | 65 | $result = ExampleModel::whereMetaNotIn('key1', [1, 2])->orWhereMetaNotIn('key1', [2, 3]); 66 | $this->assertEquals(3, $result->count()); 67 | $this->assertEquals([1,4,5], $result->pluck('id')->toArray()); 68 | 69 | $result = ExampleModel::whereMetaNotIn('key1', [2, 3])->orWhereMetaNotIn('key1', [5, 8]); 70 | $this->assertEquals(5, $result->count()); 71 | $this->assertEquals([1,2,3,4,5], $result->pluck('id')->toArray()); 72 | } 73 | } -------------------------------------------------------------------------------- /tests/Clauses/TestWhereMetaMethod.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 24 | $this->metaTruncate(); 25 | $this->seeding(); 26 | } 27 | 28 | public function test_where_meta_clause_with_all_types() 29 | { 30 | $this->assertEquals(5 , ExampleModel::count()); 31 | 32 | $result = ExampleModel::whereMeta('key3' , 'test2'); 33 | $this->assertEquals(1 , $result->count()); 34 | $this->assertEquals([3] , $result->pluck('id')->toArray()); 35 | 36 | $result = ExampleModel::whereMeta('key1' , 1); 37 | $this->assertEquals(2 , $result->count()); 38 | $this->assertEquals([1,5] , $result->pluck('id')->toArray()); 39 | 40 | $result = ExampleModel::whereMeta('key6' , null); 41 | $this->assertEquals(1 , $result->count()); 42 | $this->assertEquals(5 , $result->first()->id); 43 | 44 | $result = ExampleModel::whereMeta('key7' , ['test1' , 'test2']); 45 | $this->assertEquals(1 , $result->count()); 46 | $this->assertEquals(5 , $result->first()->id); 47 | 48 | $result = ExampleModel::whereMeta('key4' , true); 49 | $this->assertEquals(3 , $result->count()); 50 | $this->assertEquals([1,2,5] , $result->pluck('id')->toArray()); 51 | 52 | $result = ExampleModel::whereMeta('key4' , null); 53 | $this->assertEquals(1 , $result->count()); 54 | $this->assertEquals([4] , $result->pluck('id')->toArray()); 55 | } 56 | 57 | public function test_where_meta_clause_with_all_types_using_operator() 58 | { 59 | $result = ExampleModel::whereMeta('key3' , '!=' , 'test2'); 60 | $this->assertEquals(3 , $result->count()); 61 | $this->assertEquals([1,2,5] , $result->pluck('id')->toArray()); 62 | 63 | $result = ExampleModel::whereMeta('key3' , '<>' , 'test2'); 64 | $this->assertEquals(3 , $result->count()); 65 | $this->assertEquals([1,2,5] , $result->pluck('id')->toArray()); 66 | 67 | $result = ExampleModel::whereMeta('key1' , '>' , 2); 68 | $this->assertEquals(1 , $result->count()); 69 | $this->assertEquals([4] , $result->pluck('id')->toArray()); 70 | 71 | $result = ExampleModel::whereMeta('key3' , 'like' , 'test%'); 72 | $this->assertEquals(4 , $result->count()); 73 | $this->assertEquals([1,2,3,5] , $result->pluck('id')->toArray()); 74 | } 75 | 76 | public function test_branched_filter_meta_with_where_meta_clause() 77 | { 78 | $result = ExampleModel::where(function ($query){ 79 | $query->whereMeta('key1' , 2); 80 | $query->whereMeta('key2' , 1); 81 | }); 82 | $this->assertEquals(1 , $result->count()); 83 | $this->assertEquals(2 , $result->first()->id); 84 | } 85 | 86 | public function test_or_where_meta_clause_with_all_types() 87 | { 88 | 89 | $result = ExampleModel::whereMeta('key3' , '!=' , 'test2')->orWhereMeta('key3' , null); 90 | $this->assertEquals(4 , $result->count()); 91 | $this->assertEquals([1,2,4,5] , $result->pluck('id')->toArray()); 92 | 93 | $result = ExampleModel::orWhereMeta('key3' , null); 94 | $this->assertEquals(1 , $result->count()); 95 | $this->assertEquals([4] , $result->pluck('id')->toArray()); 96 | } 97 | 98 | public function test_branched_filter_meta_with_or_where_meta_clause() 99 | { 100 | $result = ExampleModel::where(function ($query){ 101 | $query->where('id' , 1); 102 | $query->orWhereMeta('key3' , null); 103 | $query->orwhereMeta('key3' , 'test2'); 104 | }); 105 | $this->assertEquals(3 , $result->count()); 106 | $this->assertEquals([1,3,4] , $result->pluck('id')->toArray()); 107 | } 108 | 109 | public function test_multiple_clause_condition_in_one_where_meta_method() 110 | { 111 | $result = ExampleModel::whereMeta([ 112 | 'key1' => 2, 113 | 'key3' => 'test2' 114 | ]); 115 | $this->assertEquals(1 , $result->count()); 116 | $this->assertEquals([3] , $result->pluck('id')->toArray()); 117 | 118 | $result = ExampleModel::whereMeta([ 119 | ['key1' , '=' , 2,], 120 | 'key3' => 'test2' 121 | ]); 122 | $this->assertEquals(1 , $result->count()); 123 | $this->assertEquals([3] , $result->pluck('id')->toArray()); 124 | } 125 | 126 | public function test_multiple_clause_condition_in_one_or_where_meta_method() 127 | { 128 | $result = ExampleModel::orWhereMeta([ 129 | 'key1' => 1, 130 | 'key4' => false 131 | ]); 132 | $this->assertEquals(3 , $result->count()); 133 | $this->assertEquals([1,3,5] , $result->pluck('id')->toArray()); 134 | 135 | $result = ExampleModel::orWhereMeta([ 136 | ['key1' , '=' , 1], 137 | ['key4' , '=' , false], 138 | ]); 139 | $this->assertEquals(3 , $result->count()); 140 | $this->assertEquals([1,3,5] , $result->pluck('id')->toArray()); 141 | } 142 | } -------------------------------------------------------------------------------- /tests/Clauses/TestWhereMetaNull.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 21 | $this->metaTruncate(); 22 | $this->seeding(); 23 | } 24 | 25 | public function test_filter_meta_using_where_meta_null_method() 26 | { 27 | $this->assertEquals(5, ExampleModel::count()); 28 | 29 | $result = ExampleModel::whereMetaNull('key3'); 30 | $this->assertEquals(1, $result->count()); 31 | $this->assertEquals([4], $result->pluck('id')->toArray()); 32 | 33 | $result = ExampleModel::whereMetaNotNull('key3'); 34 | $this->assertEquals(4, $result->count()); 35 | $this->assertEquals([1,2,3,5], $result->pluck('id')->toArray()); 36 | } 37 | 38 | public function test_filter_meta_using_or_where_meta_null_method() 39 | { 40 | $result = ExampleModel::whereMetaNull('key3')->orWhereMetaNull('key6'); 41 | $this->assertEquals(2, $result->count()); 42 | $this->assertEquals([4,5], $result->pluck('id')->toArray()); 43 | 44 | $result = ExampleModel::whereMetaNotNull('key1', [1, 3])->orWhereMetaNotNull('key6'); 45 | $this->assertEquals(5, $result->count()); 46 | $this->assertEquals([1,2,3,4,5], $result->pluck('id')->toArray()); 47 | } 48 | } -------------------------------------------------------------------------------- /tests/SetAndGet/TestCascadeMeta.php: -------------------------------------------------------------------------------- 1 | create(); 15 | $model->setMeta([ 16 | 'key1' => 'value1', 17 | 'key2' => 'value2', 18 | ]); 19 | $this->assertEquals(1, \Zoha\Meta\Models\ExampleModel::count()); 20 | $this->assertEquals(2, Meta::count()); 21 | $model->delete(); 22 | $this->assertEquals(0, \Zoha\Meta\Models\ExampleModel::count()); 23 | $this->assertEquals(0, Meta::count()); 24 | } 25 | 26 | public function if_a_model_item_was_deleted_other_items_model_will_not_be_deleted() 27 | { 28 | $model = \Zoha\Meta\Models\ExampleModel::factory()->create(); 29 | $model2 = \Zoha\Meta\Models\ExampleModel::factory()->create(); 30 | $model2->setMeta([ 31 | 'key1' => 'value1', 32 | 'key2' => 'value2', 33 | ]); 34 | $this->assertEquals(1, \Zoha\Meta\Models\ExampleModel::count()); 35 | $this->assertEquals(2, Meta::count()); 36 | $model->delete(); 37 | $this->assertEquals(0, \Zoha\Meta\Models\ExampleModel::count()); 38 | $this->assertEquals(2, Meta::count()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/SetAndGet/TestCreateMetaMethod.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 24 | $this->metaTruncate(); 25 | $this->model = ExampleModel::factory()->create(); 26 | } 27 | 28 | public function test_add_single_meta_using_create_meta_method() 29 | { 30 | $this->model->createMeta('test', 'testvalue'); 31 | $this->assertEquals(1, Meta::count()); 32 | $meta = Meta::first(); 33 | $this->assertEqualsMeta($meta); 34 | $this->metaTruncate(); 35 | } 36 | 37 | public function test_add_single_meta_with_custom_type_using_create_meta_method() 38 | { 39 | //string 40 | 41 | $this->model->createMeta('test', 'testvalue', MetaFacade::META_TYPE_STRING); 42 | $this->assertEquals(1, Meta::count()); 43 | $meta = Meta::first(); 44 | $this->assertEqualsMeta($meta, 'test', 'testvalue', MetaFacade::META_TYPE_STRING); 45 | $this->metaTruncate(); 46 | 47 | $this->model->createMeta('test', '[1,2,3]', MetaFacade::META_TYPE_STRING); 48 | $this->assertEquals(1, Meta::count()); 49 | $meta = Meta::first(); 50 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_STRING); 51 | $this->metaTruncate(); 52 | 53 | $this->model->createMeta('test', true, MetaFacade::META_TYPE_STRING); 54 | $this->assertEquals(1, Meta::count()); 55 | $meta = Meta::first(); 56 | $this->assertEqualsMeta($meta, 'test', 'true', MetaFacade::META_TYPE_STRING); 57 | $this->metaTruncate(); 58 | 59 | $this->model->createMeta('test', null, MetaFacade::META_TYPE_STRING); 60 | $this->assertEquals(1, Meta::count()); 61 | $meta = Meta::first(); 62 | $this->assertEqualsMeta($meta, 'test', '', MetaFacade::META_TYPE_STRING); 63 | $this->metaTruncate(); 64 | 65 | //array 66 | 67 | $this->model->createMeta('test', [1, 2, 3], MetaFacade::META_TYPE_ARRAY); 68 | $this->assertEquals(1, Meta::count()); 69 | $meta = Meta::first(); 70 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_ARRAY); 71 | $this->metaTruncate(); 72 | 73 | $this->model->createMeta('test', collect([1, 2, 3]), MetaFacade::META_TYPE_ARRAY); 74 | $this->assertEquals(1, Meta::count()); 75 | $meta = Meta::first(); 76 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_ARRAY); 77 | $this->metaTruncate(); 78 | 79 | $this->model->createMeta('test', '[1,2,3]', MetaFacade::META_TYPE_ARRAY); 80 | $this->assertEquals(1, Meta::count()); 81 | $meta = Meta::first(); 82 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_ARRAY); 83 | $this->metaTruncate(); 84 | 85 | $this->model->createMeta('test', true, MetaFacade::META_TYPE_ARRAY); 86 | $this->assertEquals(1, Meta::count()); 87 | $meta = Meta::first(); 88 | $this->assertEqualsMeta($meta, 'test', '{}', MetaFacade::META_TYPE_ARRAY); 89 | $this->metaTruncate(); 90 | 91 | //json 92 | 93 | $this->model->createMeta('test', '[1,2,3]', MetaFacade::META_TYPE_JSON); 94 | $this->assertEquals(1, Meta::count()); 95 | $meta = Meta::first(); 96 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_JSON); 97 | $this->metaTruncate(); 98 | 99 | //collection 100 | 101 | $this->model->createMeta('test', collect([1, 2, 3]), MetaFacade::META_TYPE_COLLECTION); 102 | $this->assertEquals(1, Meta::count()); 103 | $meta = Meta::first(); 104 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 105 | $this->metaTruncate(); 106 | 107 | $this->model->createMeta('test', [1, 2, 3], MetaFacade::META_TYPE_COLLECTION); 108 | $this->assertEquals(1, Meta::count()); 109 | $meta = Meta::first(); 110 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 111 | $this->metaTruncate(); 112 | 113 | //integer 114 | 115 | $this->model->createMeta('test', 123, MetaFacade::META_TYPE_INTEGER); 116 | $this->assertEquals(1, Meta::count()); 117 | $meta = Meta::first(); 118 | $this->assertEqualsMeta($meta, 'test', '123', MetaFacade::META_TYPE_INTEGER); 119 | $this->metaTruncate(); 120 | 121 | $this->model->createMeta('test', '123', MetaFacade::META_TYPE_INTEGER); 122 | $this->assertEquals(1, Meta::count()); 123 | $meta = Meta::first(); 124 | $this->assertEqualsMeta($meta, 'test', '123', MetaFacade::META_TYPE_INTEGER); 125 | $this->metaTruncate(); 126 | 127 | //float 128 | 129 | $this->model->createMeta('test', 12.34, MetaFacade::META_TYPE_FLOAT); 130 | $this->assertEquals(1, Meta::count()); 131 | $meta = Meta::first(); 132 | $this->assertEqualsMeta($meta, 'test', '12.34', MetaFacade::META_TYPE_FLOAT); 133 | $this->metaTruncate(); 134 | 135 | $this->model->createMeta('test', '12.34', MetaFacade::META_TYPE_FLOAT); 136 | $this->assertEquals(1, Meta::count()); 137 | $meta = Meta::first(); 138 | $this->assertEqualsMeta($meta, 'test', '12.34', MetaFacade::META_TYPE_FLOAT); 139 | $this->metaTruncate(); 140 | 141 | $this->model->createMeta('test', '12.34'); 142 | $this->assertEquals(1, Meta::count()); 143 | $meta = Meta::first(); 144 | $this->assertEqualsMeta($meta, 'test', '12.34', MetaFacade::META_TYPE_FLOAT); 145 | $this->metaTruncate(); 146 | 147 | //boolean 148 | 149 | $this->model->createMeta('test', 'false', MetaFacade::META_TYPE_BOOLEAN); 150 | $this->assertEquals(1, Meta::count()); 151 | $meta = Meta::first(); 152 | $this->assertEqualsMeta($meta, 'test', '0', MetaFacade::META_TYPE_BOOLEAN); 153 | $this->metaTruncate(); 154 | 155 | $this->model->createMeta('test', 1, MetaFacade::META_TYPE_BOOLEAN); 156 | $this->assertEquals(1, Meta::count()); 157 | $meta = Meta::first(); 158 | $this->assertEqualsMeta($meta, 'test', '1', MetaFacade::META_TYPE_BOOLEAN); 159 | $this->metaTruncate(); 160 | 161 | $this->model->createMeta('test', false, MetaFacade::META_TYPE_BOOLEAN); 162 | $this->assertEquals(1, Meta::count()); 163 | $meta = Meta::first(); 164 | $this->assertEqualsMeta($meta, 'test', '0', MetaFacade::META_TYPE_BOOLEAN); 165 | $this->metaTruncate(); 166 | 167 | //null 168 | 169 | $this->model->createMeta('test', 'testvalue', MetaFacade::META_TYPE_NULL); 170 | $this->assertEquals(1, Meta::count()); 171 | $meta = Meta::first(); 172 | $this->assertEqualsMeta($meta, 'test', null, MetaFacade::META_TYPE_NULL); 173 | $this->metaTruncate(); 174 | } 175 | 176 | public function test_add_single_meta_with_best_guess_for_type_using_create_meta_method() 177 | { 178 | //string 179 | 180 | $this->model->createMeta('test', 'testvalue'); 181 | $this->assertEquals(1, Meta::count()); 182 | $meta = Meta::first(); 183 | $this->assertEqualsMeta($meta, 'test', 'testvalue', MetaFacade::META_TYPE_STRING); 184 | $this->metaTruncate(); 185 | 186 | //array 187 | 188 | $this->model->createMeta('test', [1, 2, 3]); 189 | $this->assertEquals(1, Meta::count()); 190 | $meta = Meta::first(); 191 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 192 | $this->metaTruncate(); 193 | 194 | //json 195 | 196 | $this->model->createMeta('test', '[1,2,3]'); 197 | $this->assertEquals(1, Meta::count()); 198 | $meta = Meta::first(); 199 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 200 | $this->metaTruncate(); 201 | 202 | //collection 203 | 204 | $this->model->createMeta('test', collect([1, 2, 3])); 205 | $this->assertEquals(1, Meta::count()); 206 | $meta = Meta::first(); 207 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 208 | $this->metaTruncate(); 209 | 210 | //integer 211 | 212 | $this->model->createMeta('test', 123); 213 | $this->assertEquals(1, Meta::count()); 214 | $meta = Meta::first(); 215 | $this->assertEqualsMeta($meta, 'test', '123', MetaFacade::META_TYPE_INTEGER); 216 | $this->metaTruncate(); 217 | 218 | $this->model->createMeta('test', '123'); 219 | $this->assertEquals(1, Meta::count()); 220 | $meta = Meta::first(); 221 | $this->assertEqualsMeta($meta, 'test', '123', MetaFacade::META_TYPE_INTEGER); 222 | $this->metaTruncate(); 223 | 224 | //boolean 225 | 226 | $this->model->createMeta('test', false, MetaFacade::META_TYPE_BOOLEAN); 227 | $this->assertEquals(1, Meta::count()); 228 | $meta = Meta::first(); 229 | $this->assertEqualsMeta($meta, 'test', '0', MetaFacade::META_TYPE_BOOLEAN); 230 | $this->metaTruncate(); 231 | 232 | //null 233 | 234 | $this->model->createMeta('test', 'testvalue', MetaFacade::META_TYPE_NULL); 235 | $this->assertEquals(1, Meta::count()); 236 | $meta = Meta::first(); 237 | $this->assertEqualsMeta($meta, 'test', null, MetaFacade::META_TYPE_NULL); 238 | $this->metaTruncate(); 239 | } 240 | 241 | public function test_add_multiple_meta_using_create_meta_method() 242 | { 243 | $this->model->createMeta([ 244 | 'test1' => 'testvalue1', 245 | 'test2' => 'testvalue2' 246 | ]); 247 | $this->assertEquals(2, Meta::count()); 248 | $meta = Meta::all(); 249 | $this->assertEqualsMeta($meta[0], 'test1', 'testvalue1', MetaFacade::META_TYPE_STRING); 250 | $this->assertEqualsMeta($meta[1], 'test2', 'testvalue2', MetaFacade::META_TYPE_STRING); 251 | $this->metaTruncate(); 252 | } 253 | 254 | public function test_add_multiple_meta_with_custom_type_using_create_meta_method() 255 | { 256 | $this->model->createMeta([ 257 | 'test1' => 'testvalue1', 258 | 'test2' => true, 259 | 'test3' => [1, 2, 3], 260 | 'test4' => 123 261 | ], MetaFacade::META_TYPE_STRING); 262 | $this->assertEquals(4, Meta::count()); 263 | $meta = Meta::all(); 264 | $this->assertEqualsMeta($meta[0], 'test1', 'testvalue1', MetaFacade::META_TYPE_STRING); 265 | $this->assertEqualsMeta($meta[1], 'test2', 'true', MetaFacade::META_TYPE_STRING); 266 | $this->assertEqualsMeta($meta[2], 'test3', '[1,2,3]', MetaFacade::META_TYPE_STRING); 267 | $this->assertEqualsMeta($meta[3], 'test4', '123', MetaFacade::META_TYPE_STRING); 268 | $this->metaTruncate(); 269 | } 270 | 271 | public function test_add_multiple_meta_with_best_guess_using_create_meta_method() 272 | { 273 | $this->model->createMeta([ 274 | 'test1' => 'testvalue1', 275 | 'test2' => true, 276 | 'test3' => [1, 2, 3], 277 | 'test4' => 123 278 | ]); 279 | $this->assertEquals(4, Meta::count()); 280 | $meta = Meta::all(); 281 | $this->assertEqualsMeta($meta[0], 'test1', 'testvalue1', MetaFacade::META_TYPE_STRING); 282 | $this->assertEqualsMeta($meta[1], 'test2', '1', MetaFacade::META_TYPE_BOOLEAN); 283 | $this->assertEqualsMeta($meta[2], 'test3', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 284 | $this->assertEqualsMeta($meta[3], 'test4', '123', MetaFacade::META_TYPE_INTEGER); 285 | $this->metaTruncate(); 286 | } 287 | 288 | public function test_fail_single_meta_if_key_is_invalid() 289 | { 290 | $meta = $this->model->createMeta(collect([1, 2, 3]), 'testvalue'); 291 | $this->assertFalse($meta); 292 | $this->assertEquals(0, Meta::count()); 293 | $this->metaTruncate(); 294 | 295 | $meta = $this->model->createMeta(false, 'testvalue'); 296 | $this->assertFalse($meta); 297 | $this->assertEquals(0, Meta::count()); 298 | $this->metaTruncate(); 299 | } 300 | 301 | public function test_fail_multiple_meta_if_key_is_invalid() 302 | { 303 | $meta = $this->model->createMeta([ 304 | 'test1' => 'testvalue1', 305 | true => true, 306 | 'test3' => [1, 2, 3], 307 | 'test4' => 123 308 | ]); 309 | $this->assertFalse($meta); 310 | $this->assertEquals(0, Meta::count()); 311 | $this->metaTruncate(); 312 | } 313 | 314 | public function test_fail_if_meta_already_exists_using_create_meta() 315 | { 316 | $this->model->createMeta('test', 'testvalue'); 317 | $this->assertEquals(1, Meta::count()); 318 | $meta = $this->model->createMeta('test', 'testvalue2'); 319 | $this->assertFalse($meta); 320 | $this->assertEquals(1, Meta::count()); 321 | $this->assertEqualsMeta(Meta::first(), 'test', 'testvalue'); 322 | $this->metaTruncate(); 323 | } 324 | 325 | public function test_fail_if_meta_already_exists_using_create_meta_multiple_create() 326 | { 327 | $this->model->createMeta('test2', 'testvalue2'); 328 | $meta = $this->model->createMeta([ 329 | 'test1' => false, 330 | 'test2' => true, 331 | 'test3' => [1, 2, 3], 332 | 'test4' => 123 333 | ]); 334 | $this->assertFalse($meta); 335 | $this->assertEquals(1, Meta::count()); 336 | $meta = Meta::all(); 337 | $this->assertEqualsMeta($meta[0], 'test2', 'testvalue2', MetaFacade::META_TYPE_STRING); 338 | $this->metaTruncate(); 339 | } 340 | } 341 | -------------------------------------------------------------------------------- /tests/SetAndGet/TestDecreaseMetaMethod.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 22 | $this->metaTruncate(); 23 | $this->model = ExampleModel::factory()->create(); 24 | } 25 | 26 | public function test_decrease_meta_method() 27 | { 28 | $this->model->setMeta('test', 1); 29 | $this->model->decreaseMeta('test'); 30 | $this->assertEquals(0, $this->model->meta->test); 31 | 32 | $this->model->decreaseMeta('test'); 33 | $this->assertEquals(-1, $this->model->meta->test); 34 | 35 | $this->model->setMeta('test', 10); 36 | $this->model->decreaseMeta('test', 4); 37 | $this->assertEquals(6, $this->model->meta->test); 38 | 39 | $this->model->setMeta('test', 'testvalue'); 40 | $this->model->decreaseMeta('test'); 41 | $this->assertEquals('testvalue', $this->model->meta->test); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /tests/SetAndGet/TestDeleteMetaMethod.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 24 | $this->metaTruncate(); 25 | $this->model = ExampleModel::factory()->create(); 26 | } 27 | 28 | public function test_delete_meta_method() 29 | { 30 | $this->model->createMeta('test', 'testvalue'); 31 | $this->assertEquals('testvalue', $this->model->meta->test); 32 | 33 | $this->model->deleteMeta('test'); 34 | $this->assertEquals(0, $this->model->getLoadedMeta()->count()); 35 | $this->assertEquals(0, Meta::count()); 36 | } 37 | 38 | public function test_truncate_meta_using_delete_meta_method() 39 | { 40 | $this->model->createMeta([ 41 | 'test' => 'testvalue', 42 | 'test2' => 'testvalue2', 43 | 'test3' => 'testvalue3' 44 | ]); 45 | $this->assertEquals(3, Meta::count()); 46 | 47 | $this->model->deleteMeta(); 48 | 49 | $this->assertEquals(0, $this->model->getLoadedMeta()->count()); 50 | $this->assertEquals(0, Meta::count()); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /tests/SetAndGet/TestGetMetaMethod.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 25 | $this->metaTruncate(); 26 | $this->seeding(); 27 | $this->model = ExampleModel::get()->last(); 28 | } 29 | 30 | public function test_get_meta_using_get_meta_method() 31 | { 32 | $this->assertEquals(5 , $this->model->id); 33 | 34 | $value = $this->model->getMeta('key3'); 35 | $this->assertEquals('test', $value); 36 | $this->assertTrue(is_string($value)); 37 | 38 | $value = $this->model->getMeta('key1'); 39 | $this->assertEquals(1, $value); 40 | $this->assertTrue(is_int($value)); 41 | 42 | $value = $this->model->getMeta('key6'); 43 | $this->assertEquals(null, $value); 44 | $this->assertTrue(is_null($value)); 45 | 46 | $value = $this->model->getMeta('key8'); 47 | $this->assertEquals(['test1', 'test2'], $value); 48 | $this->assertTrue(is_array($value)); 49 | 50 | $value = $this->model->getMeta('key5'); 51 | $this->assertEquals(collect(['test1', 'test2']), $value); 52 | $this->assertTrue($value instanceof Collection); 53 | 54 | $value = $this->model->getMeta('key7'); 55 | $this->assertEquals('["test1","test2"]', $value); 56 | $this->assertTrue( 57 | is_string($value) && 58 | (is_object(json_decode($value)) || 59 | is_array(json_decode($value)))); 60 | 61 | $value = $this->model->getMeta('key4'); 62 | $this->assertEquals(true, $value); 63 | $this->assertTrue(is_bool($value)); 64 | } 65 | 66 | public function test_get_meta_using_get_meta_method_with_default_values() 67 | { 68 | $value = $this->model->getMeta('notExistsKey' , 'default'); 69 | $this->assertEquals('default', $value); 70 | $this->assertTrue(is_string($value)); 71 | 72 | $value = $this->model->getMeta('notExistsKey' , true); 73 | $this->assertEquals(true, $value); 74 | $this->assertTrue(is_bool($value)); 75 | 76 | $value = $this->model->getMeta('notExistsKey' , 123); 77 | $this->assertEquals(123, $value); 78 | $this->assertTrue(is_int($value)); 79 | 80 | $value = $this->model->getMeta('notExistsKey' , collect([1,2,3])); 81 | $this->assertEquals(collect([1,2,3]) , $value); 82 | $this->assertTrue($value instanceof Collection); 83 | 84 | $value = $this->model->getMeta('notExistsKey' , []); 85 | $this->assertEquals([], $value); 86 | $this->assertTrue(is_array($value)); 87 | 88 | $value = $this->model->getMeta('notExistsKey' , '[1,2,3]'); 89 | $this->assertEquals('[1,2,3]', $value); 90 | 91 | $value = $this->model->getMeta('notExistsKey' , null); 92 | $this->assertEquals(null, $value); 93 | $this->assertTrue(is_null($value)); 94 | } 95 | 96 | public function test_get_meta_using_get_meta_method_with_custom_type_for_string() 97 | { 98 | $value = $this->model->getMeta('key3' , null , MetaFacade::META_TYPE_STRING); 99 | $this->assertEquals('test', $value); 100 | $this->assertTrue(is_string($value)); 101 | 102 | $value = $this->model->getMeta('key1' , null , MetaFacade::META_TYPE_STRING); 103 | $this->assertEquals('1', $value); 104 | $this->assertTrue(is_string($value)); 105 | 106 | $value = $this->model->getMeta('key6' , 'default' , MetaFacade::META_TYPE_STRING); 107 | $this->assertEquals('default', $value); 108 | $this->assertTrue(is_string($value)); 109 | 110 | $value = $this->model->getMeta('key8' , null , MetaFacade::META_TYPE_STRING); 111 | $this->assertEquals('["test1","test2"]', $value); 112 | $this->assertTrue(is_string($value)); 113 | 114 | $value = $this->model->getMeta('key5' , null , MetaFacade::META_TYPE_STRING); 115 | $this->assertEquals('["test1","test2"]', $value); 116 | $this->assertTrue(is_string($value)); 117 | 118 | $value = $this->model->getMeta('key7' , null , MetaFacade::META_TYPE_STRING); 119 | $this->assertEquals('["test1","test2"]', $value); 120 | $this->assertTrue(is_string($value)); 121 | 122 | $value = $this->model->getMeta('key4' , null , MetaFacade::META_TYPE_STRING); 123 | $this->assertEquals('1', $value); 124 | $this->assertTrue(is_string($value)); 125 | } 126 | 127 | public function test_get_meta_using_get_meta_method_after_change_database_results_by_other_methods() 128 | { 129 | $this->model->meta->test = 'testvalue'; 130 | $this->assertEquals(null , $this->model->getMeta('test')); 131 | $this->metaTruncate(); 132 | 133 | $this->model->createMeta('test' , 'testvalue'); 134 | $this->assertEquals('testvalue' , $this->model->getMeta('test')); 135 | 136 | $this->model->updateMeta('test' , 'testvalue2'); 137 | $this->assertEquals('testvalue2' , $this->model->getMeta('test')); 138 | $this->metaTruncate(); 139 | } 140 | 141 | public function test_get_meta_method_for_other_cases() 142 | { 143 | $this->model->setMeta('test',0); 144 | $meta = Meta::where('key' , 'test')->first(); 145 | $this->assertEqualsMeta($meta ,'test' , 0 , MetaFacade::META_TYPE_INTEGER); 146 | 147 | $this->model->setMeta('test',0); 148 | $this->assertEquals(0 , $this->model->getMeta('test' , 'no')); 149 | $this->model->deleteMeta('test'); 150 | 151 | } 152 | } -------------------------------------------------------------------------------- /tests/SetAndGet/TestGetMetasMethod.php: -------------------------------------------------------------------------------- 1 | create(); 16 | $model->setMeta([ 17 | 'key1' => 'value1', 18 | 'key2' => 'value2', 19 | ]); 20 | 21 | $metas = $model->getMetas(); 22 | 23 | $this->assertTrue($metas instanceof Collection); 24 | $this->assertCount(2, $metas); 25 | } 26 | 27 | public function test_that_get_metas_method_will_return_an_empty_collection_if_no_metas_was_set_yet() 28 | { 29 | $model = \Zoha\Meta\Models\ExampleModel::factory()->create(); 30 | 31 | $metas = $model->getMetas(); 32 | 33 | $this->assertTrue($metas instanceof Collection); 34 | $this->assertCount(0, $metas); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/SetAndGet/TestGettingMetaWithProperty.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 23 | $this->metaTruncate(); 24 | $this->seeding(); 25 | $this->model = ExampleModel::get()->last(); 26 | } 27 | 28 | public function test_get_meta_using_property() 29 | { 30 | $this->assertEquals(5 , $this->model->id); 31 | 32 | $value = $this->model->meta->key3; 33 | $this->assertEquals('test', $value); 34 | $this->assertTrue(is_string($value)); 35 | 36 | $value = $this->model->meta->key1; 37 | $this->assertEquals(1, $value); 38 | $this->assertTrue(is_int($value)); 39 | 40 | $value = $this->model->meta->key6; 41 | $this->assertEquals(null, $value); 42 | $this->assertTrue(is_null($value)); 43 | 44 | $value = $this->model->meta->key8; 45 | $this->assertEquals(['test1', 'test2'], $value); 46 | $this->assertTrue(is_array($value)); 47 | 48 | $value = $this->model->meta->key5; 49 | $this->assertEquals(collect(['test1', 'test2']), $value); 50 | $this->assertTrue($value instanceof Collection); 51 | 52 | $value = $this->model->meta->key7; 53 | $this->assertEquals('["test1","test2"]', $value); 54 | $this->assertTrue( 55 | is_string($value) && 56 | (is_object(json_decode($value)) || 57 | is_array(json_decode($value)))); 58 | 59 | $value = $this->model->meta->key4; 60 | $this->assertEquals(true, $value); 61 | $this->assertTrue(is_bool($value)); 62 | } 63 | 64 | public function test_search_proper_name_by_get_meta_with_property() 65 | { 66 | $this->model->setMeta('test key' , 6); 67 | $this->assertEquals(6 , $this->model->meta->test_key); 68 | 69 | $this->model->setMeta('test-key2' , 10); 70 | $this->assertEquals(10 , $this->model->meta->test_key2); 71 | } 72 | } -------------------------------------------------------------------------------- /tests/SetAndGet/TestHasMetaMethod.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 24 | $this->metaTruncate(); 25 | $this->seeding(); 26 | $this->model = ExampleModel::get()->last(); 27 | } 28 | 29 | public function test_has_meta_method() 30 | { 31 | $result = $this->model->hasMeta('key8'); 32 | $this->assertEquals(true , $result); 33 | 34 | $result = $this->model->hasMeta('key40'); 35 | $this->assertEquals(false , $result); 36 | 37 | $result = $this->model->hasMeta('key6'); 38 | $this->assertEquals(false , $result); 39 | 40 | $this->model->setMeta('key10' , 0); 41 | $result = $this->model->hasMeta('key10'); 42 | $this->assertEquals(true , $result); 43 | $this->model->deleteMeta('key10'); 44 | 45 | $result = $this->model->hasMeta('key6' , true); 46 | $this->assertEquals(true , $result); 47 | 48 | $result = $this->model->hasMeta('key8' , true , MetaFacade::META_TYPE_ARRAY); 49 | $this->assertEquals(true , $result); 50 | 51 | $result = $this->model->hasMeta('key8' , false , MetaFacade::META_TYPE_ARRAY); 52 | $this->assertEquals(true , $result); 53 | 54 | $result = $this->model->hasMeta('key8' , true , MetaFacade::META_TYPE_INTEGER); 55 | $this->assertEquals(false , $result); 56 | } 57 | } -------------------------------------------------------------------------------- /tests/SetAndGet/TestIncreaseMetaMethod.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 22 | $this->metaTruncate(); 23 | $this->model = ExampleModel::factory()->create(); 24 | } 25 | 26 | public function test_increase_meta_method() 27 | { 28 | $this->model->setMeta('test', 1); 29 | $this->model->increaseMeta('test'); 30 | $this->assertEquals(2, $this->model->meta->test); 31 | 32 | $this->model->increaseMeta('test'); 33 | $this->assertEquals(3, $this->model->meta->test); 34 | 35 | $this->model->increaseMeta('test', 4); 36 | $this->assertEquals(7, $this->model->meta->test); 37 | 38 | $this->model->setMeta('test', 0); 39 | $this->model->increaseMeta('test'); 40 | $this->assertEquals(1, $this->model->meta->test); 41 | 42 | $this->model->setMeta('test', 'testvalue'); 43 | $this->model->increaseMeta('test'); 44 | $this->assertEquals('testvalue', $this->model->meta->test); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/SetAndGet/TestMetaMethod.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 26 | $this->metaTruncate(); 27 | $this->seeding(); 28 | $this->model = ExampleModel::get()->last(); 29 | } 30 | 31 | public function test_that_meta_method_return_morphMany_relation_on_model() 32 | { 33 | $meta = $this->model->meta(); 34 | $this->assertTrue($meta instanceof MorphMany); 35 | } 36 | 37 | public function test_get_meta_using_meta_method() 38 | { 39 | $value = $this->model->meta('key3'); 40 | $this->assertEquals('test', $value); 41 | $this->assertTrue(is_string($value)); 42 | 43 | $value = $this->model->meta('key1'); 44 | $this->assertEquals(1, $value); 45 | $this->assertTrue(is_int($value)); 46 | 47 | $value = $this->model->meta('key6'); 48 | $this->assertEquals(null, $value); 49 | $this->assertTrue(is_null($value)); 50 | 51 | $value = $this->model->meta('key8'); 52 | $this->assertEquals(['test1', 'test2'], $value); 53 | $this->assertTrue(is_array($value)); 54 | 55 | $value = $this->model->meta('key5'); 56 | $this->assertEquals(collect(['test1', 'test2']), $value); 57 | $this->assertTrue($value instanceof Collection); 58 | 59 | $value = $this->model->meta('key7'); 60 | $this->assertEquals('["test1","test2"]', $value); 61 | $this->assertTrue( 62 | is_string($value) && 63 | (is_object(json_decode($value)) || 64 | is_array(json_decode($value)))); 65 | 66 | $value = $this->model->meta('key4'); 67 | $this->assertEquals(true, $value); 68 | $this->assertTrue(is_bool($value)); 69 | } 70 | 71 | public function test_add_single_meta_with_best_guess_for_type_using_meta_method() 72 | { 73 | $this->metaTruncate(); 74 | //string 75 | 76 | $this->model->meta('test', 'testvalue'); 77 | $this->assertEquals(1, Meta::count()); 78 | $meta = Meta::first(); 79 | $this->assertEqualsMeta($meta, 'test', 'testvalue', MetaFacade::META_TYPE_STRING); 80 | $this->metaTruncate(); 81 | 82 | //array 83 | 84 | $this->model->meta('test', [1, 2, 3]); 85 | $this->assertEquals(1, Meta::count()); 86 | $meta = Meta::first(); 87 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 88 | $this->metaTruncate(); 89 | 90 | //json 91 | 92 | $this->model->meta('test', '[1,2,3]'); 93 | $this->assertEquals(1, Meta::count()); 94 | $meta = Meta::first(); 95 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 96 | $this->metaTruncate(); 97 | 98 | //collection 99 | 100 | $this->model->meta('test', collect([1, 2, 3])); 101 | $this->assertEquals(1, Meta::count()); 102 | $meta = Meta::first(); 103 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 104 | $this->metaTruncate(); 105 | 106 | //integer 107 | 108 | $this->model->meta('test', 123); 109 | $this->assertEquals(1, Meta::count()); 110 | $meta = Meta::first(); 111 | $this->assertEqualsMeta($meta, 'test', '123', MetaFacade::META_TYPE_INTEGER); 112 | $this->metaTruncate(); 113 | 114 | $this->model->meta('test', '123'); 115 | $this->assertEquals(1, Meta::count()); 116 | $meta = Meta::first(); 117 | $this->assertEqualsMeta($meta, 'test', '123', MetaFacade::META_TYPE_INTEGER); 118 | $this->metaTruncate(); 119 | 120 | //integer 121 | 122 | $this->model->meta('test', 123.45); 123 | $this->assertEquals(1, Meta::count()); 124 | $meta = Meta::first(); 125 | $this->assertEqualsMeta($meta, 'test', '123.45', MetaFacade::META_TYPE_FLOAT); 126 | $this->metaTruncate(); 127 | 128 | $this->model->meta('test', '123.45'); 129 | $this->assertEquals(1, Meta::count()); 130 | $meta = Meta::first(); 131 | $this->assertEqualsMeta($meta, 'test', '123.45', MetaFacade::META_TYPE_FLOAT); 132 | $this->metaTruncate(); 133 | 134 | //float 135 | 136 | $this->model->meta('test', 123.45); 137 | $this->assertEquals(1, Meta::count()); 138 | $meta = Meta::first(); 139 | $this->assertEqualsMeta($meta, 'test', '123.45', MetaFacade::META_TYPE_FLOAT); 140 | $this->metaTruncate(); 141 | 142 | $this->model->meta('test', '123.45'); 143 | $this->assertEquals(1, Meta::count()); 144 | $meta = Meta::first(); 145 | $this->assertEqualsMeta($meta, 'test', '123.45', MetaFacade::META_TYPE_FLOAT); 146 | $this->metaTruncate(); 147 | 148 | //boolean 149 | 150 | $this->model->meta('test', false, MetaFacade::META_TYPE_BOOLEAN); 151 | $this->assertEquals(1, Meta::count()); 152 | $meta = Meta::first(); 153 | $this->assertEqualsMeta($meta, 'test', '0', MetaFacade::META_TYPE_BOOLEAN); 154 | $this->metaTruncate(); 155 | 156 | //null 157 | 158 | $this->model->meta('test', 'testvalue', MetaFacade::META_TYPE_NULL); 159 | $this->assertEquals(1, Meta::count()); 160 | $meta = Meta::first(); 161 | $this->assertEqualsMeta($meta, 'test', null, MetaFacade::META_TYPE_NULL); 162 | $this->metaTruncate(); 163 | } 164 | 165 | public function test_update_single_meta_with_best_guess_for_type_using_meta_method() 166 | { 167 | $this->metaTruncate(); 168 | //string 169 | 170 | $this->model->setMeta('test' , 'testvalue'); 171 | $this->model->meta('test', 'testvalue2'); 172 | $this->assertEquals(1, Meta::count()); 173 | $meta = Meta::first(); 174 | $this->assertEqualsMeta($meta, 'test', 'testvalue2', MetaFacade::META_TYPE_STRING); 175 | $this->metaTruncate(); 176 | 177 | //array 178 | 179 | $this->model->setMeta('test' , 'testvalue'); 180 | $this->model->meta('test', [1, 2, 3]); 181 | $this->assertEquals(1, Meta::count()); 182 | $meta = Meta::first(); 183 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 184 | $this->metaTruncate(); 185 | 186 | //json 187 | 188 | $this->model->setMeta('test' , 'testvalue'); 189 | $this->model->meta('test', '[1,2,3]'); 190 | $this->assertEquals(1, Meta::count()); 191 | $meta = Meta::first(); 192 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 193 | $this->metaTruncate(); 194 | 195 | //collection 196 | 197 | $this->model->setMeta('test' , 'testvalue'); 198 | $this->model->meta('test', collect([1, 2, 3])); 199 | $this->assertEquals(1, Meta::count()); 200 | $meta = Meta::first(); 201 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 202 | $this->metaTruncate(); 203 | 204 | //integer 205 | 206 | $this->model->setMeta('test' , 'testvalue'); 207 | $this->model->meta('test', 123); 208 | $this->assertEquals(1, Meta::count()); 209 | $meta = Meta::first(); 210 | $this->assertEqualsMeta($meta, 'test', '123', MetaFacade::META_TYPE_INTEGER); 211 | $this->metaTruncate(); 212 | 213 | $this->model->setMeta('test' , 'testvalue'); 214 | $this->model->meta('test', '123'); 215 | $this->assertEquals(1, Meta::count()); 216 | $meta = Meta::first(); 217 | $this->assertEqualsMeta($meta, 'test', '123', MetaFacade::META_TYPE_INTEGER); 218 | $this->metaTruncate(); 219 | 220 | //float 221 | 222 | $this->model->setMeta('test' , 'testvalue'); 223 | $this->model->meta('test', 123.45); 224 | $this->assertEquals(1, Meta::count()); 225 | $meta = Meta::first(); 226 | $this->assertEqualsMeta($meta, 'test', '123.45', MetaFacade::META_TYPE_FLOAT); 227 | $this->metaTruncate(); 228 | 229 | $this->model->setMeta('test' , 'testvalue'); 230 | $this->model->meta('test', '123.45'); 231 | $this->assertEquals(1, Meta::count()); 232 | $meta = Meta::first(); 233 | $this->assertEqualsMeta($meta, 'test', '123.45', MetaFacade::META_TYPE_FLOAT); 234 | $this->metaTruncate(); 235 | 236 | //boolean 237 | 238 | $this->model->setMeta('test' , 'testvalue'); 239 | $this->model->meta('test', false, MetaFacade::META_TYPE_BOOLEAN); 240 | $this->assertEquals(1, Meta::count()); 241 | $meta = Meta::first(); 242 | $this->assertEqualsMeta($meta, 'test', '0', MetaFacade::META_TYPE_BOOLEAN); 243 | $this->metaTruncate(); 244 | 245 | //null 246 | 247 | $this->model->setMeta('test' , 'testvalue'); 248 | $this->model->meta('test', 'testvalue', MetaFacade::META_TYPE_NULL); 249 | $this->assertEquals(1, Meta::count()); 250 | $meta = Meta::first(); 251 | $this->assertEqualsMeta($meta, 'test', null, MetaFacade::META_TYPE_NULL); 252 | $this->metaTruncate(); 253 | } 254 | } 255 | -------------------------------------------------------------------------------- /tests/SetAndGet/TestSetMetaMethod.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 24 | $this->metaTruncate(); 25 | $this->model = ExampleModel::factory()->create(); 26 | } 27 | 28 | public function test_add_single_meta_using_set_meta_method() 29 | { 30 | $this->model->setMeta('test', 'testvalue'); 31 | $meta = Meta::first(); 32 | $this->assertEqualsMeta($meta); 33 | $this->metaTruncate(); 34 | } 35 | 36 | public function test_add_multiple_meta_using_set_meta_method() 37 | { 38 | $this->model->setMeta([ 39 | 'test1' => 'testvalue1', 40 | 'test2' => 'testvalue2', 41 | 'test3' => 'testvalue3' 42 | ]); 43 | $allMeta = Meta::all(); 44 | foreach ($allMeta as $meta) { 45 | $this->assertEqualsMeta($meta, 'test' . $meta->id, 'testvalue' . $meta->id); 46 | } 47 | $this->metaTruncate(); 48 | } 49 | 50 | public function test_that_set_meta_method_return_false_if_key_is_not_array_or_string() 51 | { 52 | $this->assertFalse($this->model->setMeta(true, 'testvalue')); 53 | $this->metaTruncate(); 54 | } 55 | 56 | public function test_if_meta_with_same_key_already_exists_set_meta_method_update_it() 57 | { 58 | $this->model->setMeta('test', 'testvalue'); 59 | $meta = Meta::first(); 60 | $this->assertEqualsMeta($meta); 61 | $this->assertEquals(1, Meta::all()->count()); 62 | $this->model->setMeta('test', 'testvalue1'); 63 | $meta = Meta::first(); 64 | $this->assertEquals(1, Meta::all()->count()); 65 | $this->assertEqualsMeta($meta, 'test', 'testvalue1'); 66 | $this->metaTruncate(); 67 | } 68 | 69 | public function test_in_multiple_setting_meta_if_one_of_theme_already_exists_will_be_updated() 70 | { 71 | $this->model->setMeta('test1', 'testvalue1000'); 72 | $meta = Meta::first(); 73 | $this->assertEqualsMeta($meta, 'test1', 'testvalue1000'); 74 | 75 | $this->model->setMeta([ 76 | 'test1' => 'testvalue1', 77 | 'test2' => 'testvalue2', 78 | 'test3' => 'testvalue3' 79 | ]); 80 | $allMeta = Meta::all(); 81 | foreach ($allMeta as $meta) { 82 | $this->assertEqualsMeta($meta, 'test' . $meta->id, 'testvalue' . $meta->id); 83 | } 84 | $this->assertEquals(3, $allMeta->count()); 85 | $this->metaTruncate(); 86 | } 87 | 88 | public function test_using_set_meta_with_custom_type_will_force_meta_type_to_convert__null() 89 | { 90 | $meta = $this->fastCreateMeta('test', 'testvalue', MetaFacade::META_TYPE_NULL); 91 | $this->assertEqualsMeta($meta, 'test', null, MetaFacade::META_TYPE_NULL); 92 | 93 | $meta = $this->fastCreateMeta('test', collect([1, 2, 3]), MetaFacade::META_TYPE_NULL); 94 | $this->assertEqualsMeta($meta, 'test', null, MetaFacade::META_TYPE_NULL); 95 | 96 | $meta = $this->fastCreateMeta('test', '[1,2,3]', MetaFacade::META_TYPE_NULL); 97 | $this->assertEqualsMeta($meta, 'test', null, MetaFacade::META_TYPE_NULL); 98 | 99 | $meta = $this->fastCreateMeta('test', 'null', MetaFacade::META_TYPE_NULL); 100 | $this->assertEqualsMeta($meta, 'test', null, MetaFacade::META_TYPE_NULL); 101 | 102 | $meta = $this->fastCreateMeta('test', false, MetaFacade::META_TYPE_NULL); 103 | $this->assertEqualsMeta($meta, 'test', null, MetaFacade::META_TYPE_NULL); 104 | } 105 | 106 | public function test_using_set_meta_with_custom_type_will_force_meta_type_to_convert__json() 107 | { 108 | $meta = $this->fastCreateMeta('test', 'testvalue', MetaFacade::META_TYPE_JSON); 109 | $this->assertEqualsMeta($meta, 'test', '{}', MetaFacade::META_TYPE_JSON); 110 | 111 | $meta = $this->fastCreateMeta('test', '[1,2,3]', MetaFacade::META_TYPE_JSON); 112 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_JSON); 113 | 114 | $meta = $this->fastCreateMeta('test', [1, 2, 3], MetaFacade::META_TYPE_JSON); 115 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_JSON); 116 | 117 | $meta = $this->fastCreateMeta('test', collect([1, 2, 3]), MetaFacade::META_TYPE_JSON); 118 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_JSON); 119 | 120 | $meta = $this->fastCreateMeta('test', false, MetaFacade::META_TYPE_JSON); 121 | $this->assertEqualsMeta($meta, 'test', '{}', MetaFacade::META_TYPE_JSON); 122 | 123 | $meta = $this->fastCreateMeta('test', 44, MetaFacade::META_TYPE_JSON); 124 | $this->assertEqualsMeta($meta, 'test', '{}', MetaFacade::META_TYPE_JSON); 125 | } 126 | 127 | public function test_using_set_meta_with_custom_type_will_force_meta_type_to_convert__array() 128 | { 129 | $meta = $this->fastCreateMeta('test', 'testvalue', MetaFacade::META_TYPE_ARRAY); 130 | $this->assertEqualsMeta($meta, 'test', '{}', MetaFacade::META_TYPE_ARRAY); 131 | 132 | $meta = $this->fastCreateMeta('test', null, MetaFacade::META_TYPE_ARRAY); 133 | $this->assertEqualsMeta($meta, 'test', '{}', MetaFacade::META_TYPE_ARRAY); 134 | 135 | $meta = $this->fastCreateMeta('test', '[1,2,3]', MetaFacade::META_TYPE_ARRAY); 136 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_ARRAY); 137 | 138 | $meta = $this->fastCreateMeta('test', 123, MetaFacade::META_TYPE_ARRAY); 139 | $this->assertEqualsMeta($meta, 'test', '{}', MetaFacade::META_TYPE_ARRAY); 140 | 141 | $meta = $this->fastCreateMeta('test', false, MetaFacade::META_TYPE_ARRAY); 142 | $this->assertEqualsMeta($meta, 'test', '{}', MetaFacade::META_TYPE_ARRAY); 143 | 144 | $meta = $this->fastCreateMeta('test', collect([1, 2, 3]), MetaFacade::META_TYPE_ARRAY); 145 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_ARRAY); 146 | 147 | $meta = $this->fastCreateMeta('test', [1, 2, 3], MetaFacade::META_TYPE_ARRAY); 148 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_ARRAY); 149 | } 150 | 151 | public function test_using_set_meta_with_custom_type_will_force_meta_type_to_convert__collection() 152 | { 153 | $meta = $this->fastCreateMeta('test', 'testvalue', MetaFacade::META_TYPE_COLLECTION); 154 | $this->assertEqualsMeta($meta, 'test', '{}', MetaFacade::META_TYPE_COLLECTION); 155 | 156 | $meta = $this->fastCreateMeta('test', null, MetaFacade::META_TYPE_COLLECTION); 157 | $this->assertEqualsMeta($meta, 'test', '{}', MetaFacade::META_TYPE_COLLECTION); 158 | 159 | $meta = $this->fastCreateMeta('test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 160 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 161 | 162 | $meta = $this->fastCreateMeta('test', 123, MetaFacade::META_TYPE_COLLECTION); 163 | $this->assertEqualsMeta($meta, 'test', '{}', MetaFacade::META_TYPE_COLLECTION); 164 | 165 | $meta = $this->fastCreateMeta('test', false, MetaFacade::META_TYPE_COLLECTION); 166 | $this->assertEqualsMeta($meta, 'test', '{}', MetaFacade::META_TYPE_COLLECTION); 167 | 168 | $meta = $this->fastCreateMeta('test', collect([1, 2, 3]), MetaFacade::META_TYPE_COLLECTION); 169 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 170 | 171 | $meta = $this->fastCreateMeta('test', [1, 2, 3], MetaFacade::META_TYPE_COLLECTION); 172 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 173 | } 174 | 175 | public function test_using_set_meta_with_custom_type_will_force_meta_type_to_convert__integer() 176 | { 177 | $meta = $this->fastCreateMeta('test', 'testvalue', MetaFacade::META_TYPE_INTEGER); 178 | $this->assertEqualsMeta($meta, 'test', 0, MetaFacade::META_TYPE_INTEGER); 179 | 180 | $meta = $this->fastCreateMeta('test', [1, 2, 3], MetaFacade::META_TYPE_INTEGER); 181 | $this->assertEqualsMeta($meta, 'test', 0, MetaFacade::META_TYPE_INTEGER); 182 | 183 | $meta = $this->fastCreateMeta('test', '123', MetaFacade::META_TYPE_INTEGER); 184 | $this->assertEqualsMeta($meta, 'test', 123, MetaFacade::META_TYPE_INTEGER); 185 | 186 | $meta = $this->fastCreateMeta('test', null, MetaFacade::META_TYPE_INTEGER); 187 | $this->assertEqualsMeta($meta, 'test', 0, MetaFacade::META_TYPE_INTEGER); 188 | 189 | $meta = $this->fastCreateMeta('test', false, MetaFacade::META_TYPE_INTEGER); 190 | $this->assertEqualsMeta($meta, 'test', 0, MetaFacade::META_TYPE_INTEGER); 191 | 192 | $meta = $this->fastCreateMeta('test', true, MetaFacade::META_TYPE_INTEGER); 193 | $this->assertEqualsMeta($meta, 'test', 1, MetaFacade::META_TYPE_INTEGER); 194 | 195 | $meta = $this->fastCreateMeta('test', 12.34, MetaFacade::META_TYPE_INTEGER); 196 | $this->assertEqualsMeta($meta, 'test', 12, MetaFacade::META_TYPE_INTEGER); 197 | 198 | $meta = $this->fastCreateMeta('test', '12.34', MetaFacade::META_TYPE_INTEGER); 199 | $this->assertEqualsMeta($meta, 'test', 12, MetaFacade::META_TYPE_INTEGER); 200 | } 201 | 202 | public function test_using_set_meta_with_custom_type_will_force_meta_type_to_convert__float() 203 | { 204 | $meta = $this->fastCreateMeta('test', 'testvalue', MetaFacade::META_TYPE_FLOAT); 205 | $this->assertEqualsMeta($meta, 'test', 0.0, MetaFacade::META_TYPE_FLOAT); 206 | 207 | $meta = $this->fastCreateMeta('test', [1, 2, 3], MetaFacade::META_TYPE_FLOAT); 208 | $this->assertEqualsMeta($meta, 'test', 0.0, MetaFacade::META_TYPE_FLOAT); 209 | 210 | $meta = $this->fastCreateMeta('test', '123', MetaFacade::META_TYPE_FLOAT); 211 | $this->assertEqualsMeta($meta, 'test', 123.0, MetaFacade::META_TYPE_FLOAT); 212 | 213 | $meta = $this->fastCreateMeta('test', null, MetaFacade::META_TYPE_FLOAT); 214 | $this->assertEqualsMeta($meta, 'test', 0.0, MetaFacade::META_TYPE_FLOAT); 215 | 216 | $meta = $this->fastCreateMeta('test', false, MetaFacade::META_TYPE_FLOAT); 217 | $this->assertEqualsMeta($meta, 'test', 0.0, MetaFacade::META_TYPE_FLOAT); 218 | 219 | $meta = $this->fastCreateMeta('test', true, MetaFacade::META_TYPE_FLOAT); 220 | $this->assertEqualsMeta($meta, 'test', 1.0, MetaFacade::META_TYPE_FLOAT); 221 | 222 | $meta = $this->fastCreateMeta('test', 12.34, MetaFacade::META_TYPE_FLOAT); 223 | $this->assertEqualsMeta($meta, 'test', 12.34, MetaFacade::META_TYPE_FLOAT); 224 | 225 | $meta = $this->fastCreateMeta('test', '12.34', MetaFacade::META_TYPE_FLOAT); 226 | $this->assertEqualsMeta($meta, 'test', 12.34, MetaFacade::META_TYPE_FLOAT); 227 | } 228 | 229 | public function test_using_set_meta_with_custom_type_will_force_meta_type_to_convert__boolean() 230 | { 231 | $meta = $this->fastCreateMeta('test', 'testvalue', MetaFacade::META_TYPE_BOOLEAN); 232 | $this->assertEqualsMeta($meta, 'test', 1, MetaFacade::META_TYPE_BOOLEAN); 233 | 234 | $meta = $this->fastCreateMeta('test', [1, 2, 3], MetaFacade::META_TYPE_BOOLEAN); 235 | $this->assertEqualsMeta($meta, 'test', 1, MetaFacade::META_TYPE_BOOLEAN); 236 | 237 | $meta = $this->fastCreateMeta('test', '123', MetaFacade::META_TYPE_BOOLEAN); 238 | $this->assertEqualsMeta($meta, 'test', 1, MetaFacade::META_TYPE_BOOLEAN); 239 | 240 | $meta = $this->fastCreateMeta('test', null, MetaFacade::META_TYPE_BOOLEAN); 241 | $this->assertEqualsMeta($meta, 'test', 0, MetaFacade::META_TYPE_BOOLEAN); 242 | 243 | $meta = $this->fastCreateMeta('test', false, MetaFacade::META_TYPE_BOOLEAN); 244 | $this->assertEqualsMeta($meta, 'test', 0, MetaFacade::META_TYPE_BOOLEAN); 245 | 246 | $meta = $this->fastCreateMeta('test', true, MetaFacade::META_TYPE_BOOLEAN); 247 | $this->assertEqualsMeta($meta, 'test', 1, MetaFacade::META_TYPE_BOOLEAN); 248 | } 249 | 250 | public function test_add_single_meta_with_best_guess_of_type() 251 | { 252 | // string 253 | $meta = $this->fastCreateMeta('test', 'testvalue'); 254 | $this->assertEqualsMeta($meta); 255 | 256 | //boolean 257 | $meta = $this->fastCreateMeta('test', true); 258 | $this->assertEqualsMeta($meta, 'test', '1', MetaFacade::META_TYPE_BOOLEAN); 259 | 260 | $meta = $this->fastCreateMeta('test', false); 261 | $this->assertEqualsMeta($meta, 'test', '0', MetaFacade::META_TYPE_BOOLEAN); 262 | 263 | // array json and collection _> to collection 264 | $meta = $this->fastCreateMeta('test', [1, 2, 3]); 265 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 266 | 267 | $meta = $this->fastCreateMeta('test', collect([1, 2, 3])); 268 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 269 | 270 | $meta = $this->fastCreateMeta('test', '[1,2,3]'); 271 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 272 | 273 | // integer 274 | $meta = $this->fastCreateMeta('test', '123'); 275 | $this->assertEqualsMeta($meta, 'test', '123', MetaFacade::META_TYPE_INTEGER); 276 | 277 | $meta = $this->fastCreateMeta('test', 123); 278 | $this->assertEqualsMeta($meta, 'test', '123', MetaFacade::META_TYPE_INTEGER); 279 | 280 | // float 281 | $meta = $this->fastCreateMeta('test', '123.45'); 282 | $this->assertEqualsMeta($meta, 'test', '123.45', MetaFacade::META_TYPE_FLOAT); 283 | 284 | $meta = $this->fastCreateMeta('test', 123.45); 285 | $this->assertEqualsMeta($meta, 'test', '123.45', MetaFacade::META_TYPE_FLOAT); 286 | 287 | // null 288 | $meta = $this->fastCreateMeta('test', null); 289 | $this->assertEqualsMeta($meta, 'test', null, MetaFacade::META_TYPE_NULL); 290 | } 291 | 292 | public function test_that_false_returned_if_one_of_key_in_multiple_set_meta_was_invalid() 293 | { 294 | $settingMultipleMeta = $this->model->setMeta([ 295 | 'test1' => 'testvalue1', 296 | true => 'testvalue2', 297 | 'test3' => 'testvalue3' 298 | ]); 299 | $this->assertFalse($settingMultipleMeta); 300 | $allMeta = Meta::all(); 301 | $this->assertEquals(2, $allMeta->count()); 302 | $this->metaTruncate(); 303 | } 304 | 305 | public function test_set_multiple_meta_with_custom_type_using_set_meta_method() 306 | { 307 | $this->model->setMeta([ 308 | 'test1' => 'testvalue1', 309 | 'test2' => 'testvalue2', 310 | 'test3' => 'testvalue3' 311 | ], MetaFacade::META_TYPE_NULL); 312 | $allMeta = Meta::all(); 313 | foreach ($allMeta as $meta) { 314 | $this->assertEqualsMeta($meta, 'test' . $meta->id, null, MetaFacade::META_TYPE_NULL); 315 | } 316 | $this->metaTruncate(); 317 | 318 | $this->model->setMeta([ 319 | 'test1' => 'testvalue1', 320 | 'test2' => [1, 2, 3], 321 | 'test3' => 123 322 | ], MetaFacade::META_TYPE_BOOLEAN); 323 | $allMeta = Meta::all(); 324 | foreach ($allMeta as $meta) { 325 | $this->assertEqualsMeta($meta, 'test' . $meta->id, true, MetaFacade::META_TYPE_BOOLEAN); 326 | } 327 | $this->metaTruncate(); 328 | } 329 | 330 | public function test_update_multiple_meta_with_custom_type_using_set_meta_method() 331 | { 332 | $this->model->setMeta([ 333 | 'test1' => 'testvalue1', 334 | 'test2' => false, 335 | 'test3' => [1, 2, 3] 336 | ]); 337 | $allMeta = Meta::all(); 338 | $this->assertEqualsMeta($allMeta[0], 'test' . $allMeta[0]->id, 'testvalue1', MetaFacade::META_TYPE_STRING); 339 | $this->assertEqualsMeta($allMeta[1], 'test' . $allMeta[1]->id, '0', MetaFacade::META_TYPE_BOOLEAN); 340 | $this->assertEqualsMeta($allMeta[2], 'test' . $allMeta[2]->id, '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 341 | 342 | $this->model->setMeta([ 343 | 'test1' => 'testvalue2', 344 | 'test2' => true, 345 | 'test3' => [1, 2, 3, 4, 5] 346 | ], MetaFacade::META_TYPE_BOOLEAN); 347 | $allMeta = Meta::all(); 348 | foreach ($allMeta as $meta) { 349 | $this->assertEqualsMeta($meta, 'test' . $meta->id, true, MetaFacade::META_TYPE_BOOLEAN); 350 | } 351 | $this->metaTruncate(); 352 | } 353 | } 354 | -------------------------------------------------------------------------------- /tests/SetAndGet/TestSetMetaUsingProperty.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 25 | $this->metaTruncate(); 26 | $this->model = ExampleModel::factory()->create(); 27 | } 28 | 29 | public function test_add_single_meta_using_property() 30 | { 31 | $this->model->meta->test = 'testvalue'; 32 | $this->model->meta->save(); 33 | $meta = Meta::first(); 34 | $this->assertEqualsMeta($meta); 35 | $this->metaTruncate(); 36 | } 37 | 38 | public function test_if_meta_with_same_key_already_exists_set_property_update_it() 39 | { 40 | $this->model->meta->test = 'testvalue'; 41 | $this->model->meta->save(); 42 | $meta = Meta::first(); 43 | $this->assertEqualsMeta($meta); 44 | $this->assertEquals(1, Meta::all()->count()); 45 | $this->model->meta->test = 'testvalue1'; 46 | $this->model->meta->save(); 47 | $meta = Meta::first(); 48 | $this->assertEquals(1, Meta::all()->count()); 49 | $this->assertEqualsMeta($meta, 'test', 'testvalue1'); 50 | $this->metaTruncate(); 51 | } 52 | 53 | public function test_add_single_meta_with_best_guess_of_type() 54 | { 55 | $this->metaTruncate(); 56 | 57 | // string 58 | $this->model->meta->test = 'testvalue'; 59 | $this->model->meta->save(); 60 | $meta = Meta::first(); 61 | $this->assertEqualsMeta($meta); 62 | $this->metaTruncate(); 63 | 64 | //boolean 65 | $this->model->meta->test = true; 66 | $this->model->meta->save(); 67 | $meta = Meta::first(); 68 | $this->assertEqualsMeta($meta, 'test', '1', MetaFacade::META_TYPE_BOOLEAN); 69 | $this->metaTruncate(); 70 | 71 | $this->model->meta->test = false; 72 | $this->model->meta->save(); 73 | $meta = Meta::first(); 74 | $this->assertEqualsMeta($meta, 'test', '0', MetaFacade::META_TYPE_BOOLEAN); 75 | $this->metaTruncate(); 76 | 77 | // array json and collection _> to collection 78 | $this->model->meta->test = [1, 2, 3]; 79 | $this->model->meta->save(); 80 | $meta = Meta::first(); 81 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 82 | $this->metaTruncate(); 83 | 84 | $this->model->meta->test = collect([1, 2, 3]); 85 | $this->model->meta->save(); 86 | $meta = Meta::first(); 87 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 88 | $this->metaTruncate(); 89 | 90 | $this->model->meta->test = '[1,2,3]'; 91 | $this->model->meta->save(); 92 | $meta = Meta::first(); 93 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 94 | $this->metaTruncate(); 95 | 96 | // integer 97 | $this->model->meta->test = '123'; 98 | $this->model->meta->save(); 99 | $meta = Meta::first(); 100 | $this->assertEqualsMeta($meta, 'test', '123', MetaFacade::META_TYPE_INTEGER); 101 | $this->metaTruncate(); 102 | 103 | $this->model->meta->test = 123; 104 | $this->model->meta->save(); 105 | $meta = Meta::first(); 106 | $this->assertEqualsMeta($meta, 'test', '123', MetaFacade::META_TYPE_INTEGER); 107 | $this->metaTruncate(); 108 | 109 | // float 110 | $this->model->meta->test = '123.45'; 111 | $this->model->meta->save(); 112 | $meta = Meta::first(); 113 | $this->assertEqualsMeta($meta, 'test', '123.45', MetaFacade::META_TYPE_FLOAT); 114 | $this->metaTruncate(); 115 | 116 | $this->model->meta->test = 123.45; 117 | $this->model->meta->save(); 118 | $meta = Meta::first(); 119 | $this->assertEqualsMeta($meta, 'test', '123.45', MetaFacade::META_TYPE_FLOAT); 120 | $this->metaTruncate(); 121 | 122 | // null 123 | $this->model->meta->test = null; 124 | $this->model->meta->save(); 125 | $meta = Meta::first(); 126 | $this->assertEqualsMeta($meta, 'test', null, MetaFacade::META_TYPE_NULL); 127 | $this->metaTruncate(); 128 | } 129 | 130 | public function test_add_single_meta_using_property_fail_if_save_method_not_called() 131 | { 132 | $this->model->meta->test = 'testvalue'; 133 | $this->assertEquals(0, Meta::count()); 134 | $this->model->createMeta('key1', 'testvalue'); 135 | $this->assertEquals(1, Meta::count()); 136 | $meta = $this->model->meta->save(); 137 | $this->assertFalse($meta); 138 | $this->assertEquals(1, Meta::count()); 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /tests/SetAndGet/TestTruncateMetaMethod.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 24 | $this->metaTruncate(); 25 | $this->model = ExampleModel::factory()->create(); 26 | } 27 | 28 | public function test_trunate_meta_method() 29 | { 30 | $this->model->createMeta([ 31 | 'test' => 'testvalue', 32 | 'test2' => 'testvalue2', 33 | 'test3' => 'testvalue3' 34 | ]); 35 | $this->assertEquals(3, Meta::count()); 36 | 37 | $this->model->truncateMeta(); 38 | 39 | $this->assertEquals(0, $this->model->getLoadedMeta()->count()); 40 | $this->assertEquals(0, Meta::count()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /tests/SetAndGet/TestUpdateMetaMethod.php: -------------------------------------------------------------------------------- 1 | modelTruncate(); 24 | $this->metaTruncate(); 25 | $this->model = ExampleModel::factory()->create(); 26 | } 27 | 28 | public function createFakeMeta() 29 | { 30 | $this->model->setMeta('test', 'testvalue'); 31 | } 32 | 33 | public function test_update_single_meta_using_update_meta_method() 34 | { 35 | $this->createFakeMeta(); 36 | $this->model->updateMeta('test', 'testvalue2'); 37 | $this->assertEquals(1, Meta::count()); 38 | $meta = Meta::first(); 39 | $this->assertEqualsMeta($meta, 'test', 'testvalue2'); 40 | $this->metaTruncate(); 41 | } 42 | 43 | public function test_update_single_meta_with_custom_type_using_update_meta_method() 44 | { 45 | //string 46 | 47 | $this->createFakeMeta(); 48 | $this->model->updateMeta('test', 'testvalue2', MetaFacade::META_TYPE_STRING); 49 | $this->assertEquals(1, Meta::count()); 50 | $meta = Meta::first(); 51 | $this->assertEqualsMeta($meta, 'test', 'testvalue2', MetaFacade::META_TYPE_STRING); 52 | $this->metaTruncate(); 53 | 54 | $this->createFakeMeta(); 55 | $this->model->updateMeta('test', '[1,2,3]', MetaFacade::META_TYPE_STRING); 56 | $this->assertEquals(1, Meta::count()); 57 | $meta = Meta::first(); 58 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_STRING); 59 | $this->metaTruncate(); 60 | 61 | $this->createFakeMeta(); 62 | $this->model->updateMeta('test', true, MetaFacade::META_TYPE_STRING); 63 | $this->assertEquals(1, Meta::count()); 64 | $meta = Meta::first(); 65 | $this->assertEqualsMeta($meta, 'test', 'true', MetaFacade::META_TYPE_STRING); 66 | $this->metaTruncate(); 67 | 68 | $this->createFakeMeta(); 69 | $this->model->updateMeta('test', null, MetaFacade::META_TYPE_STRING); 70 | $this->assertEquals(1, Meta::count()); 71 | $meta = Meta::first(); 72 | $this->assertEqualsMeta($meta, 'test', '', MetaFacade::META_TYPE_STRING); 73 | $this->metaTruncate(); 74 | 75 | //array 76 | 77 | $this->createFakeMeta(); 78 | $this->model->updateMeta('test', [1, 2, 3], MetaFacade::META_TYPE_ARRAY); 79 | $this->assertEquals(1, Meta::count()); 80 | $meta = Meta::first(); 81 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_ARRAY); 82 | $this->metaTruncate(); 83 | 84 | $this->createFakeMeta(); 85 | $this->model->updateMeta('test', collect([1, 2, 3]), MetaFacade::META_TYPE_ARRAY); 86 | $this->assertEquals(1, Meta::count()); 87 | $meta = Meta::first(); 88 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_ARRAY); 89 | $this->metaTruncate(); 90 | 91 | $this->createFakeMeta(); 92 | $this->model->updateMeta('test', '[1,2,3]', MetaFacade::META_TYPE_ARRAY); 93 | $this->assertEquals(1, Meta::count()); 94 | $meta = Meta::first(); 95 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_ARRAY); 96 | $this->metaTruncate(); 97 | 98 | $this->createFakeMeta(); 99 | $this->model->updateMeta('test', true, MetaFacade::META_TYPE_ARRAY); 100 | $this->assertEquals(1, Meta::count()); 101 | $meta = Meta::first(); 102 | $this->assertEqualsMeta($meta, 'test', '{}', MetaFacade::META_TYPE_ARRAY); 103 | $this->metaTruncate(); 104 | 105 | //json 106 | 107 | $this->createFakeMeta(); 108 | $this->model->updateMeta('test', '[1,2,3]', MetaFacade::META_TYPE_JSON); 109 | $this->assertEquals(1, Meta::count()); 110 | $meta = Meta::first(); 111 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_JSON); 112 | $this->metaTruncate(); 113 | 114 | //collection 115 | 116 | $this->createFakeMeta(); 117 | $this->model->updateMeta('test', collect([1, 2, 3]), MetaFacade::META_TYPE_COLLECTION); 118 | $this->assertEquals(1, Meta::count()); 119 | $meta = Meta::first(); 120 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 121 | $this->metaTruncate(); 122 | 123 | $this->createFakeMeta(); 124 | $this->model->updateMeta('test', [1, 2, 3], MetaFacade::META_TYPE_COLLECTION); 125 | $this->assertEquals(1, Meta::count()); 126 | $meta = Meta::first(); 127 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 128 | $this->metaTruncate(); 129 | 130 | //integer 131 | 132 | $this->createFakeMeta(); 133 | $this->model->updateMeta('test', 123, MetaFacade::META_TYPE_INTEGER); 134 | $this->assertEquals(1, Meta::count()); 135 | $meta = Meta::first(); 136 | $this->assertEqualsMeta($meta, 'test', '123', MetaFacade::META_TYPE_INTEGER); 137 | $this->metaTruncate(); 138 | 139 | $this->createFakeMeta(); 140 | $this->model->updateMeta('test', '123', MetaFacade::META_TYPE_INTEGER); 141 | $this->assertEquals(1, Meta::count()); 142 | $meta = Meta::first(); 143 | $this->assertEqualsMeta($meta, 'test', '123', MetaFacade::META_TYPE_INTEGER); 144 | $this->metaTruncate(); 145 | 146 | //float 147 | 148 | $this->createFakeMeta(); 149 | $this->model->updateMeta('test', 123.45, MetaFacade::META_TYPE_FLOAT); 150 | $this->assertEquals(1, Meta::count()); 151 | $meta = Meta::first(); 152 | $this->assertEqualsMeta($meta, 'test', '123.45', MetaFacade::META_TYPE_FLOAT); 153 | $this->metaTruncate(); 154 | 155 | $this->createFakeMeta(); 156 | $this->model->updateMeta('test', '123.45', MetaFacade::META_TYPE_FLOAT); 157 | $this->assertEquals(1, Meta::count()); 158 | $meta = Meta::first(); 159 | $this->assertEqualsMeta($meta, 'test', '123.45', MetaFacade::META_TYPE_FLOAT); 160 | $this->metaTruncate(); 161 | 162 | //boolean 163 | 164 | $this->createFakeMeta(); 165 | $this->model->updateMeta('test', 'false', MetaFacade::META_TYPE_BOOLEAN); 166 | $this->assertEquals(1, Meta::count()); 167 | $meta = Meta::first(); 168 | $this->assertEqualsMeta($meta, 'test', '0', MetaFacade::META_TYPE_BOOLEAN); 169 | $this->metaTruncate(); 170 | 171 | $this->createFakeMeta(); 172 | $this->model->updateMeta('test', 1, MetaFacade::META_TYPE_BOOLEAN); 173 | $this->assertEquals(1, Meta::count()); 174 | $meta = Meta::first(); 175 | $this->assertEqualsMeta($meta, 'test', '1', MetaFacade::META_TYPE_BOOLEAN); 176 | $this->metaTruncate(); 177 | 178 | $this->createFakeMeta(); 179 | $this->model->updateMeta('test', false, MetaFacade::META_TYPE_BOOLEAN); 180 | $this->assertEquals(1, Meta::count()); 181 | $meta = Meta::first(); 182 | $this->assertEqualsMeta($meta, 'test', '0', MetaFacade::META_TYPE_BOOLEAN); 183 | $this->metaTruncate(); 184 | 185 | //null 186 | 187 | $this->createFakeMeta(); 188 | $this->model->updateMeta('test', 'testvalue', MetaFacade::META_TYPE_NULL); 189 | $this->assertEquals(1, Meta::count()); 190 | $meta = Meta::first(); 191 | $this->assertEqualsMeta($meta, 'test', null, MetaFacade::META_TYPE_NULL); 192 | $this->metaTruncate(); 193 | } 194 | 195 | public function test_update_single_meta_with_best_guess_for_type_using_update_meta_method() 196 | { 197 | //string 198 | 199 | $this->createFakeMeta(); 200 | $this->model->updateMeta('test', 'testvalue2'); 201 | $this->assertEquals(1, Meta::count()); 202 | $meta = Meta::first(); 203 | $this->assertEqualsMeta($meta, 'test', 'testvalue2', MetaFacade::META_TYPE_STRING); 204 | $this->metaTruncate(); 205 | 206 | //array 207 | 208 | $this->createFakeMeta(); 209 | $this->model->updateMeta('test', [1, 2, 3]); 210 | $this->assertEquals(1, Meta::count()); 211 | $meta = Meta::first(); 212 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 213 | $this->metaTruncate(); 214 | 215 | //json 216 | 217 | $this->createFakeMeta(); 218 | $this->model->updateMeta('test', '[1,2,3]'); 219 | $this->assertEquals(1, Meta::count()); 220 | $meta = Meta::first(); 221 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 222 | $this->metaTruncate(); 223 | 224 | //collection 225 | 226 | $this->createFakeMeta(); 227 | $this->model->updateMeta('test', collect([1, 2, 3])); 228 | $this->assertEquals(1, Meta::count()); 229 | $meta = Meta::first(); 230 | $this->assertEqualsMeta($meta, 'test', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 231 | $this->metaTruncate(); 232 | 233 | //integer 234 | 235 | $this->createFakeMeta(); 236 | $this->model->updateMeta('test', 123); 237 | $this->assertEquals(1, Meta::count()); 238 | $meta = Meta::first(); 239 | $this->assertEqualsMeta($meta, 'test', '123', MetaFacade::META_TYPE_INTEGER); 240 | $this->metaTruncate(); 241 | 242 | $this->createFakeMeta(); 243 | $this->model->updateMeta('test', '123'); 244 | $this->assertEquals(1, Meta::count()); 245 | $meta = Meta::first(); 246 | $this->assertEqualsMeta($meta, 'test', '123', MetaFacade::META_TYPE_INTEGER); 247 | $this->metaTruncate(); 248 | 249 | //float 250 | 251 | $this->createFakeMeta(); 252 | $this->model->updateMeta('test', 123.45); 253 | $this->assertEquals(1, Meta::count()); 254 | $meta = Meta::first(); 255 | $this->assertEqualsMeta($meta, 'test', '123.45', MetaFacade::META_TYPE_FLOAT); 256 | $this->metaTruncate(); 257 | 258 | $this->createFakeMeta(); 259 | $this->model->updateMeta('test', '123.45'); 260 | $this->assertEquals(1, Meta::count()); 261 | $meta = Meta::first(); 262 | $this->assertEqualsMeta($meta, 'test', '123.45', MetaFacade::META_TYPE_FLOAT); 263 | $this->metaTruncate(); 264 | 265 | //boolean 266 | 267 | $this->createFakeMeta(); 268 | $this->model->updateMeta('test', false, MetaFacade::META_TYPE_BOOLEAN); 269 | $this->assertEquals(1, Meta::count()); 270 | $meta = Meta::first(); 271 | $this->assertEqualsMeta($meta, 'test', '0', MetaFacade::META_TYPE_BOOLEAN); 272 | $this->metaTruncate(); 273 | 274 | //null 275 | 276 | $this->createFakeMeta(); 277 | $this->model->updateMeta('test', 'testvalue', MetaFacade::META_TYPE_NULL); 278 | $this->assertEquals(1, Meta::count()); 279 | $meta = Meta::first(); 280 | $this->assertEqualsMeta($meta, 'test', null, MetaFacade::META_TYPE_NULL); 281 | $this->metaTruncate(); 282 | } 283 | 284 | public function test_update_multiple_meta_using_update_meta_method() 285 | { 286 | $this->model->createMeta('test1', 'test'); 287 | $this->model->createMeta('test2', 'test'); 288 | $this->model->updateMeta([ 289 | 'test1' => 'testvalue1', 290 | 'test2' => 'testvalue2' 291 | ]); 292 | $this->assertEquals(2, Meta::count()); 293 | $meta = Meta::all(); 294 | $this->assertEqualsMeta($meta[0], 'test1', 'testvalue1', MetaFacade::META_TYPE_STRING); 295 | $this->assertEqualsMeta($meta[1], 'test2', 'testvalue2', MetaFacade::META_TYPE_STRING); 296 | $this->metaTruncate(); 297 | } 298 | 299 | public function test_update_multiple_meta_with_custom_type_using_update_meta_method() 300 | { 301 | $this->model->createMeta('test1', 'test'); 302 | $this->model->createMeta('test2', 'test'); 303 | $this->model->createMeta('test3', 'test'); 304 | $this->model->createMeta('test4', 'test'); 305 | 306 | $this->model->updateMeta([ 307 | 'test1' => 'testvalue1', 308 | 'test2' => true, 309 | 'test3' => [1, 2, 3], 310 | 'test4' => 123 311 | ], MetaFacade::META_TYPE_STRING); 312 | $this->assertEquals(4, Meta::count()); 313 | $meta = Meta::all(); 314 | $this->assertEqualsMeta($meta[0], 'test1', 'testvalue1', MetaFacade::META_TYPE_STRING); 315 | $this->assertEqualsMeta($meta[1], 'test2', 'true', MetaFacade::META_TYPE_STRING); 316 | $this->assertEqualsMeta($meta[2], 'test3', '[1,2,3]', MetaFacade::META_TYPE_STRING); 317 | $this->assertEqualsMeta($meta[3], 'test4', '123', MetaFacade::META_TYPE_STRING); 318 | $this->metaTruncate(); 319 | } 320 | 321 | public function test_update_multiple_meta_with_best_guess_using_update_meta_method() 322 | { 323 | $this->model->createMeta('test1', 'test'); 324 | $this->model->createMeta('test2', 'test'); 325 | $this->model->createMeta('test3', 'test'); 326 | $this->model->createMeta('test4', 'test'); 327 | $this->model->createMeta('test5', 'test'); 328 | 329 | $this->model->updateMeta([ 330 | 'test1' => 'testvalue1', 331 | 'test2' => true, 332 | 'test3' => [1, 2, 3], 333 | 'test4' => 123, 334 | 'test5' => 123.45 335 | ]); 336 | $this->assertEquals(5, Meta::count()); 337 | $meta = Meta::all(); 338 | $this->assertEqualsMeta($meta[0], 'test1', 'testvalue1', MetaFacade::META_TYPE_STRING); 339 | $this->assertEqualsMeta($meta[1], 'test2', '1', MetaFacade::META_TYPE_BOOLEAN); 340 | $this->assertEqualsMeta($meta[2], 'test3', '[1,2,3]', MetaFacade::META_TYPE_COLLECTION); 341 | $this->assertEqualsMeta($meta[3], 'test4', '123', MetaFacade::META_TYPE_INTEGER); 342 | $this->assertEqualsMeta($meta[4], 'test5', '123.45', MetaFacade::META_TYPE_FLOAT); 343 | $this->metaTruncate(); 344 | } 345 | 346 | public function test_fail_single_meta_if_key_is_invalid() 347 | { 348 | $meta = $this->model->updateMeta(collect([1, 2, 3]), 'testvalue'); 349 | $this->assertFalse($meta); 350 | $this->assertEquals(0, Meta::count()); 351 | $this->metaTruncate(); 352 | 353 | $meta = $this->model->updateMeta(false, 'testvalue'); 354 | $this->assertFalse($meta); 355 | $this->assertEquals(0, Meta::count()); 356 | $this->metaTruncate(); 357 | } 358 | 359 | public function test_fail_multiple_meta_if_key_is_invalid() 360 | { 361 | $this->model->createMeta('test1', 'test'); 362 | $this->model->createMeta('test2', 'test'); 363 | $this->model->createMeta('test3', 'test'); 364 | 365 | $meta = $this->model->updateMeta([ 366 | 'test1' => 'testvalue1', 367 | true => true, 368 | 'test3' => [1, 2, 3], 369 | 'test4' => 123 370 | ]); 371 | $this->assertFalse($meta); 372 | $this->assertEquals(3, Meta::count()); 373 | $this->metaTruncate(); 374 | } 375 | 376 | public function test_fail_if_meta_already_not_exists_using_update_meta() 377 | { 378 | $meta = $this->model->updateMeta('test', 'testvalue2'); 379 | $this->assertFalse($meta); 380 | $this->assertEquals(0, Meta::count()); 381 | $this->metaTruncate(); 382 | } 383 | 384 | public function test_fail_if_meta_already_exists_using_update_meta_multiple_create() 385 | { 386 | $this->model->createMeta('test1', 'testvalue2'); 387 | $meta = $this->model->updateMeta([ 388 | 'test1' => false, 389 | 'test2' => true, 390 | 'test3' => [1, 2, 3], 391 | 'test4' => 123 392 | ]); 393 | $this->assertFalse($meta); 394 | $this->assertEquals(1, Meta::count()); 395 | $meta = Meta::first(); 396 | $this->assertEqualsMeta($meta, 'test1', 'testvalue2', MetaFacade::META_TYPE_STRING); 397 | $this->metaTruncate(); 398 | } 399 | } 400 | -------------------------------------------------------------------------------- /tests/StaticMethods/TestConvertValueMetaToType.php: -------------------------------------------------------------------------------- 1 | assertEquals('test', Meta::convertMetaValueToType('test', Meta::META_TYPE_STRING)); 16 | } 17 | 18 | public function test_convert_json_to_string() 19 | { 20 | $this->assertEquals('[1,2,3]', Meta::convertMetaValueToType('[1,2,3]', Meta::META_TYPE_STRING)); 21 | } 22 | 23 | public function test_convert_collection_to_string() 24 | { 25 | $this->assertEquals('[1,2,3]', Meta::convertMetaValueToType(collect([1, 2, 3]), Meta::META_TYPE_STRING)); 26 | } 27 | 28 | public function test_convert_array_to_string() 29 | { 30 | $this->assertEquals('[1,2,3]', Meta::convertMetaValueToType([1, 2, 3], Meta::META_TYPE_STRING)); 31 | } 32 | 33 | public function test_convert_boolean_to_string() 34 | { 35 | $this->assertEquals('true', Meta::convertMetaValueToType(true, Meta::META_TYPE_STRING)); 36 | } 37 | 38 | public function test_convert_integer_to_string() 39 | { 40 | $this->assertEquals('44', Meta::convertMetaValueToType(44, Meta::META_TYPE_STRING)); 41 | } 42 | 43 | public function test_convert_null_to_string() 44 | { 45 | $this->assertEquals(null, Meta::convertMetaValueToType(null, Meta::META_TYPE_STRING)); 46 | } 47 | 48 | public function test_convert_string_to_integer() 49 | { 50 | $this->assertEquals(0, Meta::convertMetaValueToType('test', Meta::META_TYPE_INTEGER)); 51 | } 52 | 53 | public function test_convert_json_to_integer() 54 | { 55 | $this->assertEquals(0, Meta::convertMetaValueToType('[1,2,3]', Meta::META_TYPE_INTEGER)); 56 | } 57 | 58 | public function test_convert_collection_to_integer() 59 | { 60 | $this->assertEquals(0, Meta::convertMetaValueToType(collect([1, 2, 3]), Meta::META_TYPE_INTEGER)); 61 | } 62 | 63 | public function test_convert_array_to_integer() 64 | { 65 | $this->assertEquals(0, Meta::convertMetaValueToType([1, 2, 3], Meta::META_TYPE_INTEGER)); 66 | } 67 | 68 | public function test_convert_boolean_to_integer() 69 | { 70 | $this->assertEquals(1, Meta::convertMetaValueToType(true, Meta::META_TYPE_INTEGER)); 71 | $this->assertEquals(0, Meta::convertMetaValueToType(false, Meta::META_TYPE_INTEGER)); 72 | } 73 | 74 | public function test_convert_number_to_integer() 75 | { 76 | $this->assertEquals(44, Meta::convertMetaValueToType(44, Meta::META_TYPE_INTEGER)); 77 | $this->assertEquals(4, Meta::convertMetaValueToType(4.4, Meta::META_TYPE_INTEGER)); 78 | } 79 | 80 | public function test_convert_number_to_float() 81 | { 82 | $this->assertEquals(55.0, Meta::convertMetaValueToType(55, Meta::META_TYPE_FLOAT)); 83 | $this->assertEquals(5.5, Meta::convertMetaValueToType(5.5, Meta::META_TYPE_FLOAT)); 84 | } 85 | 86 | public function test_convert_null_to_integer() 87 | { 88 | $this->assertEquals(0, Meta::convertMetaValueToType(null, Meta::META_TYPE_INTEGER)); 89 | } 90 | 91 | public function test_convert_string_to_collection() 92 | { 93 | $this->assertEquals(collect([]), Meta::convertMetaValueToType('test', Meta::META_TYPE_COLLECTION)); 94 | } 95 | 96 | public function test_convert_json_to_collection() 97 | { 98 | $this->assertEquals(collect([1,2,3]), Meta::convertMetaValueToType('[1,2,3]', Meta::META_TYPE_COLLECTION)); 99 | } 100 | 101 | public function test_convert_collection_to_collection() 102 | { 103 | $this->assertEquals(collect([1,2,3]), Meta::convertMetaValueToType(collect([1, 2, 3]), Meta::META_TYPE_COLLECTION)); 104 | } 105 | 106 | public function test_convert_array_to_collection() 107 | { 108 | $this->assertEquals(collect([1,2,3]), Meta::convertMetaValueToType([1, 2, 3], Meta::META_TYPE_COLLECTION)); 109 | } 110 | 111 | public function test_convert_boolean_to_collection() 112 | { 113 | $this->assertEquals(collect([]), Meta::convertMetaValueToType(true, Meta::META_TYPE_COLLECTION)); 114 | } 115 | 116 | public function test_convert_integer_to_collection() 117 | { 118 | $this->assertEquals(collect([]), Meta::convertMetaValueToType(44, Meta::META_TYPE_COLLECTION)); 119 | } 120 | 121 | public function test_convert_null_to_collection() 122 | { 123 | $this->assertEquals(collect([]), Meta::convertMetaValueToType(null, Meta::META_TYPE_COLLECTION)); 124 | } 125 | 126 | public function test_convert_string_to_json() 127 | { 128 | $this->assertEquals('{}', Meta::convertMetaValueToType('test', Meta::META_TYPE_JSON)); 129 | } 130 | 131 | public function test_convert_json_to_json() 132 | { 133 | $this->assertEquals('[1,2,3]', Meta::convertMetaValueToType('[1,2,3]', Meta::META_TYPE_JSON)); 134 | } 135 | 136 | public function test_convert_collection_to_json() 137 | { 138 | $this->assertEquals('[1,2,3]', Meta::convertMetaValueToType(collect([1, 2, 3]), Meta::META_TYPE_JSON)); 139 | } 140 | 141 | public function test_convert_array_to_json() 142 | { 143 | $this->assertEquals('[1,2,3]', Meta::convertMetaValueToType([1, 2, 3], Meta::META_TYPE_JSON)); 144 | } 145 | 146 | public function test_convert_boolean_to_json() 147 | { 148 | $this->assertEquals('{}', Meta::convertMetaValueToType(true, Meta::META_TYPE_JSON)); 149 | } 150 | 151 | public function test_convert_integer_to_json() 152 | { 153 | $this->assertEquals('{}', Meta::convertMetaValueToType(44, Meta::META_TYPE_JSON)); 154 | } 155 | 156 | public function test_convert_null_to_json() 157 | { 158 | $this->assertEquals('{}', Meta::convertMetaValueToType(null, Meta::META_TYPE_JSON)); 159 | } 160 | 161 | public function test_convert_string_to_array() 162 | { 163 | $this->assertEquals([], Meta::convertMetaValueToType('test', Meta::META_TYPE_ARRAY)); 164 | } 165 | 166 | public function test_convert_json_to_array() 167 | { 168 | $this->assertEquals([1, 2, 3], Meta::convertMetaValueToType('[1,2,3]', Meta::META_TYPE_ARRAY)); 169 | } 170 | 171 | public function test_convert_collection_to_array() 172 | { 173 | $this->assertEquals([1, 2, 3], Meta::convertMetaValueToType(collect([1, 2, 3]), Meta::META_TYPE_ARRAY)); 174 | } 175 | 176 | public function test_convert_array_to_array() 177 | { 178 | $this->assertEquals([1, 2, 3], Meta::convertMetaValueToType([1, 2, 3], Meta::META_TYPE_ARRAY)); 179 | } 180 | 181 | public function test_convert_boolean_to_array() 182 | { 183 | $this->assertEquals([], Meta::convertMetaValueToType(true, Meta::META_TYPE_ARRAY)); 184 | } 185 | 186 | public function test_convert_integer_to_array() 187 | { 188 | $this->assertEquals([], Meta::convertMetaValueToType(44, Meta::META_TYPE_ARRAY)); 189 | } 190 | 191 | public function test_convert_null_to_array() 192 | { 193 | $this->assertEquals([], Meta::convertMetaValueToType(null, Meta::META_TYPE_ARRAY)); 194 | } 195 | 196 | public function test_convert_string_to_boolean() 197 | { 198 | $this->assertEquals(true, Meta::convertMetaValueToType('test', Meta::META_TYPE_BOOLEAN)); 199 | $this->assertEquals(false, Meta::convertMetaValueToType('0', Meta::META_TYPE_BOOLEAN)); 200 | $this->assertEquals(true, Meta::convertMetaValueToType('1', Meta::META_TYPE_BOOLEAN)); 201 | $this->assertEquals(false, Meta::convertMetaValueToType('false', Meta::META_TYPE_BOOLEAN)); 202 | $this->assertEquals(true, Meta::convertMetaValueToType('true', Meta::META_TYPE_BOOLEAN)); 203 | } 204 | 205 | public function test_convert_json_to_boolean() 206 | { 207 | $this->assertEquals(true, Meta::convertMetaValueToType('[1,2,3]', Meta::META_TYPE_BOOLEAN)); 208 | } 209 | 210 | public function test_convert_collection_to_boolean() 211 | { 212 | $this->assertEquals(true, Meta::convertMetaValueToType(collect([1, 2, 3]), Meta::META_TYPE_BOOLEAN)); 213 | } 214 | 215 | public function test_convert_array_to_boolean() 216 | { 217 | $this->assertEquals(true, Meta::convertMetaValueToType([1, 2, 3], Meta::META_TYPE_BOOLEAN)); 218 | } 219 | 220 | public function test_convert_boolean_to_boolean() 221 | { 222 | $this->assertFalse(false, Meta::convertMetaValueToType(false, Meta::META_TYPE_BOOLEAN)); 223 | $this->assertTrue(true, Meta::convertMetaValueToType(true, Meta::META_TYPE_BOOLEAN)); 224 | } 225 | 226 | public function test_convert_integer_to_boolean() 227 | { 228 | $this->assertEquals(true, Meta::convertMetaValueToType(44, Meta::META_TYPE_BOOLEAN)); 229 | } 230 | 231 | public function test_convert_null_to_boolean() 232 | { 233 | $this->assertEquals(false, Meta::convertMetaValueToType(null, Meta::META_TYPE_BOOLEAN)); 234 | } 235 | 236 | public function test_convert_to_null() 237 | { 238 | $this->assertEquals(null, Meta::convertMetaValueToType('null', Meta::META_TYPE_NULL)); 239 | } 240 | } 241 | -------------------------------------------------------------------------------- /tests/StaticMethods/TestGuessType.php: -------------------------------------------------------------------------------- 1 | assertEquals(Meta::META_TYPE_NULL , Meta::guessType(null)); 15 | $this->assertNotEquals(Meta::META_TYPE_NULL , Meta::guessType('null')); 16 | } 17 | 18 | public function test_guess_array() 19 | { 20 | $this->assertEquals(Meta::META_TYPE_ARRAY , Meta::guessType([1,2,3])); 21 | $this->assertEquals(Meta::META_TYPE_ARRAY , Meta::guessType([])); 22 | $this->assertNotEquals(Meta::META_TYPE_ARRAY ,Meta::guessType( collect([1,2,3]))); 23 | $this->assertNotEquals(Meta::META_TYPE_ARRAY , Meta::guessType('[1,2,3]')); 24 | $this->assertNotEquals(Meta::META_TYPE_ARRAY , Meta::guessType('array')); 25 | } 26 | 27 | public function test_guess_string() 28 | { 29 | $this->assertEquals(Meta::META_TYPE_STRING , Meta::guessType('test')); 30 | $this->assertEquals(Meta::META_TYPE_STRING , Meta::guessType('123s4')); 31 | $this->assertEquals(Meta::META_TYPE_STRING , Meta::guessType('')); 32 | $this->assertEquals(Meta::META_TYPE_STRING , Meta::guessType('true')); 33 | $this->assertNotEquals(Meta::META_TYPE_STRING , Meta::guessType('123')); 34 | $this->assertNotEquals(Meta::META_TYPE_STRING , Meta::guessType('[1,2,3]')); 35 | $this->assertNotEquals(Meta::META_TYPE_STRING , Meta::guessType('[]')); 36 | $this->assertNotEquals(Meta::META_TYPE_STRING , Meta::guessType('{}')); 37 | } 38 | 39 | public function test_guess_json() 40 | { 41 | $this->assertEquals(Meta::META_TYPE_JSON , Meta::guessType('[1,2,3]')); 42 | $this->assertEquals(Meta::META_TYPE_JSON , Meta::guessType('[]')); 43 | $this->assertEquals(Meta::META_TYPE_JSON , Meta::guessType('{}')); 44 | $this->assertNotEquals(Meta::META_TYPE_JSON , Meta::guessType('test')); 45 | $this->assertNotEquals(Meta::META_TYPE_JSON , Meta::guessType([1,2,3])); 46 | } 47 | 48 | public function test_guess_collection() 49 | { 50 | $this->assertEquals(Meta::META_TYPE_COLLECTION , Meta::guessType(collect([1,2,3]))); 51 | $this->assertEquals(Meta::META_TYPE_COLLECTION ,Meta::guessType( collect([]))); 52 | $this->assertNotEquals(Meta::META_TYPE_COLLECTION , Meta::guessType([])); 53 | $this->assertNotEquals(Meta::META_TYPE_COLLECTION , Meta::guessType(collect([1,2,3])->toArray())); 54 | } 55 | 56 | public function test_guess_boolean() 57 | { 58 | $this->assertEquals(Meta::META_TYPE_BOOLEAN , Meta::guessType(true)); 59 | $this->assertEquals(Meta::META_TYPE_BOOLEAN , Meta::guessType(false)); 60 | $this->assertNotEquals(Meta::META_TYPE_BOOLEAN , Meta::guessType(1)); 61 | $this->assertNotEquals(Meta::META_TYPE_BOOLEAN , Meta::guessType(0)); 62 | $this->assertNotEquals(Meta::META_TYPE_BOOLEAN , Meta::guessType('true')); 63 | } 64 | 65 | public function test_guess_integer() 66 | { 67 | $this->assertEquals(Meta::META_TYPE_INTEGER , Meta::guessType(123)); 68 | $this->assertEquals(Meta::META_TYPE_INTEGER , Meta::guessType('123')); 69 | $this->assertEquals(Meta::META_TYPE_FLOAT, Meta::guessType(1.8)); 70 | $this->assertEquals(Meta::META_TYPE_FLOAT, Meta::guessType('12.34')); 71 | $this->assertNotEquals(Meta::META_TYPE_FLOAT, Meta::guessType('12.34a')); 72 | $this->assertNotEquals(Meta::META_TYPE_INTEGER , Meta::guessType('123s')); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | "Zoha\Meta\Facades\MetaFacade", 26 | ]; 27 | } 28 | 29 | /** 30 | * define environment configs 31 | * 32 | * @param $app 33 | * @return void 34 | */ 35 | protected function getEnvironmentSetUp($app) 36 | { 37 | $app['config']->set('database.default', 'laravelmeta'); 38 | $app['config']->set('database.connections.laravelmeta', [ 39 | 'driver' => 'sqlite', 40 | 'database' => ':memory:', 41 | 'prefix' => '', 42 | ]); 43 | } 44 | 45 | /** 46 | * setup testing 47 | * 48 | * @return void 49 | */ 50 | protected function setUp(): void 51 | { 52 | parent::setUp(); 53 | $this->app['config']->set('meta', include(__DIR__ . '/../src/Meta/Config/meta.php')); 54 | $this->loadLaravelMigrations(['--database' => 'laravelmeta']); 55 | $this->loadMigrationsFrom(__DIR__ . '/../src/Meta/Database/TestCaseMigrations'); 56 | $this->artisan('migrate', ['--database' => 'laravelmeta']); 57 | } 58 | 59 | //--------------------------------------- Test Methods -----------------------------------------// 60 | 61 | public function test_connection_and_migrations() 62 | { 63 | $tableUsersExists = Schema::hasTable('model'); 64 | $tableMetaExists = Schema::hasTable(config('meta.tables.default', 'meta')); 65 | 66 | $this->assertTrue($tableUsersExists); 67 | $this->assertTrue($tableMetaExists); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /tests/TestCustomMetaTable.php: -------------------------------------------------------------------------------- 1 | assertTrue(Schema::hasTable('tests_meta')); 17 | } 18 | 19 | public function test_tables_are_separated() 20 | { 21 | CustomMetaTableModel::create([ 22 | 'title' => 'custom' 23 | ]); 24 | $this->assertEquals(1, CustomMetaTableModel::count()); 25 | $customTable = CustomMetaTableModel::first(); 26 | 27 | $defaultModel = ExampleModel::factory()->create(); 28 | $defaultModel->setMeta([ 29 | 'key1' => 'value1', 30 | 'key2' => 'value2', 31 | ]); 32 | 33 | $customTable->setMeta('key3', 'value3'); 34 | 35 | $this->assertEquals(2, Meta::count()); 36 | 37 | $this->assertEquals(1, $customTable->meta->count()); 38 | 39 | $customTable->setMeta('key3', 'value4'); 40 | 41 | $this->assertEquals(2, Meta::count()); 42 | $this->assertEquals(1, $customTable->meta->count()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /tests/TestFactories.php: -------------------------------------------------------------------------------- 1 | create([ 14 | 'title' => 'test title' 15 | ]); 16 | $model = ExampleModel::first(); 17 | $this->assertEquals('test title', $model->title); 18 | 19 | $this->truncate(); 20 | } 21 | 22 | public function test_meta_model_factory() 23 | { 24 | Meta::factory()->create([ 25 | 'key' => 'example key', 26 | 'value' => 'example value', 27 | 'owner_type' => 'owner type', 28 | 'owner_id' => 10, 29 | ]); 30 | $meta = Meta::first(); 31 | // assert key and value 32 | $this->assertEquals('example key', $meta->key); 33 | $this->assertEquals('example value', $meta->value); 34 | 35 | // assert type and status 36 | $this->assertEquals(\Zoha\Meta\Helpers\MetaHelper::META_TYPE_STRING, $meta->type); 37 | $this->assertTrue((bool) $meta->status); 38 | 39 | //assert owner type and owner key 40 | $this->assertEquals('owner type', $meta->owner_type); 41 | $this->assertEquals(10, $meta->owner_id); 42 | 43 | $this->truncate(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /tests/TestingHelpers.php: -------------------------------------------------------------------------------- 1 | \Zoha\Meta\Models\ExampleModel::class, 18 | 'meta' => \Zoha\Meta\Models\Meta::class, 19 | ]; 20 | 21 | private $fakeDataMeta = [ 22 | [ 23 | [ 24 | 'key' => 'key1', 25 | 'value' => 1, 26 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_INTEGER, 27 | 'owner_id' => 1, 28 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 29 | ], 30 | [ 31 | 'key' => 'key2', 32 | 'value' => 1, 33 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_INTEGER, 34 | 'owner_id' => 1, 35 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 36 | ], 37 | [ 38 | 'key' => 'key3', 39 | 'value' => 'test', 40 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_STRING, 41 | 'owner_id' => 1, 42 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 43 | ], 44 | [ 45 | 'key' => 'key4', 46 | 'value' => true, 47 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_BOOLEAN, 48 | 'owner_id' => 1, 49 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 50 | ], 51 | [ 52 | 'key' => 'key5', 53 | 'value' => '["test1","test2"]', 54 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_COLLECTION, 55 | 'owner_id' => 1, 56 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 57 | ], 58 | ], 59 | [ 60 | [ 61 | 'key' => 'key1', 62 | 'value' => 2, 63 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_INTEGER, 64 | 'owner_id' => 2, 65 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 66 | ], 67 | [ 68 | 'key' => 'key2', 69 | 'value' => 1, 70 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_INTEGER, 71 | 'owner_id' => 2, 72 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 73 | ], 74 | [ 75 | 'key' => 'key3', 76 | 'value' => 'test', 77 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_STRING, 78 | 'owner_id' => 2, 79 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 80 | ], 81 | [ 82 | 'key' => 'key4', 83 | 'value' => true, 84 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_BOOLEAN, 85 | 'owner_id' => 2, 86 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 87 | ], 88 | [ 89 | 'key' => 'key5', 90 | 'value' => '["test1","test2"]', 91 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_ARRAY, 92 | 'owner_id' => 2, 93 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 94 | ], 95 | ], 96 | [ 97 | [ 98 | 'key' => 'key1', 99 | 'value' => 2, 100 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_INTEGER, 101 | 'owner_id' => 3, 102 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 103 | ], 104 | [ 105 | 'key' => 'key2', 106 | 'value' => 2, 107 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_INTEGER, 108 | 'owner_id' => 3, 109 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 110 | ], 111 | [ 112 | 'key' => 'key3', 113 | 'value' => 'test2', 114 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_STRING, 115 | 'owner_id' => 3, 116 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 117 | ], 118 | [ 119 | 'key' => 'key4', 120 | 'value' => false, 121 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_BOOLEAN, 122 | 'owner_id' => 3, 123 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 124 | ], 125 | [ 126 | 'key' => 'key5', 127 | 'value' => '["test3","test4"]', 128 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_JSON, 129 | 'owner_id' => 3, 130 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 131 | ], 132 | ], 133 | [ 134 | [ 135 | 'key' => 'key1', 136 | 'value' => 3, 137 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_INTEGER, 138 | 'owner_id' => 4, 139 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 140 | ], 141 | [ 142 | 'key' => 'key2', 143 | 'value' => 4, 144 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_INTEGER, 145 | 'owner_id' => 4, 146 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 147 | ], 148 | [ 149 | 'key' => 'key3', 150 | 'value' => null, 151 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_STRING, 152 | 'owner_id' => 4, 153 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 154 | ], 155 | [ 156 | 'key' => 'key4', 157 | 'value' => null, 158 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_BOOLEAN, 159 | 'owner_id' => 4, 160 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 161 | ], 162 | [ 163 | 'key' => 'key5', 164 | 'value' => '["test5","test6"]', 165 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_ARRAY, 166 | 'owner_id' => 4, 167 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 168 | ], 169 | ], 170 | [ 171 | [ 172 | 'key' => 'key1', 173 | 'value' => 1, 174 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_INTEGER, 175 | 'owner_id' => 5, 176 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 177 | ], 178 | [ 179 | 'key' => 'key2', 180 | 'value' => 2, 181 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_INTEGER, 182 | 'owner_id' => 5, 183 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 184 | ], 185 | [ 186 | 'key' => 'key3', 187 | 'value' => 'test', 188 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_STRING, 189 | 'owner_id' => 5, 190 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 191 | ], 192 | [ 193 | 'key' => 'key4', 194 | 'value' => true, 195 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_BOOLEAN, 196 | 'owner_id' => 5, 197 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 198 | ], 199 | [ 200 | 'key' => 'key5', 201 | 'value' => '["test1","test2"]', 202 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_COLLECTION, 203 | 'owner_id' => 5, 204 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 205 | ], 206 | [ 207 | 'key' => 'key6', 208 | 'value' => null, 209 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_NULL, 210 | 'owner_id' => 5, 211 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 212 | ], 213 | [ 214 | 'key' => 'key7', 215 | 'value' => '["test1","test2"]', 216 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_JSON, 217 | 'owner_id' => 5, 218 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 219 | ], 220 | [ 221 | 'key' => 'key8', 222 | 'value' => '["test1","test2"]', 223 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_ARRAY, 224 | 'owner_id' => 5, 225 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 226 | ], 227 | [ 228 | 'key' => 'key9', 229 | 'value' => '{"test100":"test2","test2323":"test4"}', 230 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_ARRAY, 231 | 'owner_id' => 5, 232 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 233 | ], 234 | [ 235 | 'key' => 'key10', 236 | 'value' => 12.34, 237 | 'type' => \Zoha\Meta\Helpers\MetaHelper::META_TYPE_FLOAT, 238 | 'owner_id' => 5, 239 | 'owner_type' => 'Zoha\Meta\Models\ExampleModel' 240 | ], 241 | ], 242 | ]; 243 | 244 | //------------------------------------------ Methods --------------------------------------------// 245 | 246 | /** 247 | * truncate all database tables or specific table 248 | * 249 | * @return void 250 | */ 251 | protected function truncate($class = null) 252 | { 253 | if ($class == null) { 254 | foreach ($this->tablesModels as $class) { 255 | $this->truncate($class); 256 | } 257 | return; 258 | } 259 | 260 | $class::truncate(); 261 | 262 | if ($class = Meta::class) { 263 | if ($this->model != null) { 264 | $this->model->truncateMeta(); 265 | } 266 | } 267 | } 268 | 269 | /** 270 | * create fake data in tables for tests 271 | * 272 | * @return void 273 | */ 274 | protected function seeding($class = null) 275 | { 276 | if ($class == null) { 277 | $this->truncate(); 278 | foreach ($this->tablesModels as $class) { 279 | $this->seeding($class); 280 | } 281 | return; 282 | } 283 | if ($class == $this->tablesModels['model']) { 284 | foreach (range(0, 4) as $index) { 285 | $class::create([ 286 | 'title' => 'model ' . $index 287 | ]); 288 | } 289 | return; 290 | } elseif ($class == $this->tablesModels['meta']) { 291 | $index = 0; 292 | foreach (ExampleModel::all() as $model) { 293 | foreach ($this->fakeDataMeta[$index] as $metaGroup) { 294 | $class::create($metaGroup); 295 | } 296 | $index++; 297 | } 298 | } 299 | } 300 | 301 | /** 302 | * delete all meta data from db 303 | * 304 | * @return void 305 | */ 306 | protected function metaTruncate() 307 | { 308 | $this->truncate(Meta::class); 309 | } 310 | 311 | /** 312 | * delete all model data from db 313 | * 314 | * @return void 315 | */ 316 | protected function modelTruncate() 317 | { 318 | $this->truncate(ExampleModel::class); 319 | } 320 | 321 | /** 322 | * check assert columns of an specific Meta 323 | * 324 | * 325 | */ 326 | protected function assertEqualsMeta( 327 | $meta, 328 | $key = 'test', 329 | $value = 'testvalue', 330 | $type = \Zoha\Meta\Helpers\MetaHelper::META_TYPE_STRING 331 | ) { 332 | $this->assertEquals($key, $meta->key); 333 | $this->assertEquals($value, $meta->value); 334 | $this->assertEquals($type, $meta->type); 335 | $this->assertEquals(get_class($this->model), $meta->owner_type); 336 | $this->assertEquals($this->model->id, $meta->owner_id); 337 | } 338 | 339 | /** 340 | * fast create a meta and return it 341 | * 342 | * @param string $type 343 | * @return Meta 344 | */ 345 | public function fastCreateMeta($key = 'test', $value = 'testvalue', $type = null) 346 | { 347 | $this->truncate(Meta::class); 348 | $this->model->setMeta($key, $value, $type); 349 | return Meta::first(); 350 | } 351 | 352 | //--------------------------------------- Test Methods -----------------------------------------// 353 | 354 | public function test_truncate_method_deletes_all_data_in_all_tables() 355 | { 356 | ExampleModel::factory()->create(); 357 | Meta::factory()->create([ 358 | 'owner_type' => 'test', 359 | 'owner_id' => 1, 360 | ]); 361 | $this->assertNotEquals(0, Meta::all()->count()); 362 | $this->assertNotEquals(0, ExampleModel::all()->count()); 363 | $this->truncate(); 364 | $this->assertEquals(0, Meta::all()->count()); 365 | $this->assertEquals(0, ExampleModel::all()->count()); 366 | } 367 | 368 | public function test_truncate_method_deletes_all_data_in_specific_tables() 369 | { 370 | ExampleModel::factory()->create(); 371 | Meta::factory()->create([ 372 | 'owner_type' => 'test', 373 | 'owner_id' => 1, 374 | ]); 375 | $this->assertNotEquals(0, Meta::all()->count()); 376 | $this->assertNotEquals(0, ExampleModel::all()->count()); 377 | $this->truncate(Meta::class); 378 | $this->assertEquals(0, Meta::all()->count()); 379 | $this->assertNotEquals(0, ExampleModel::all()->count()); 380 | } 381 | 382 | public function test_seed_method_create_new_data_in_all_tables() 383 | { 384 | $this->truncate(); 385 | $this->assertEquals(0, ExampleModel::all()->count()); 386 | $this->assertEquals(0, Meta::all()->count()); 387 | $this->seeding(); 388 | $this->assertEquals(5, ExampleModel::all()->count()); 389 | $this->assertEquals(30, Meta::all()->count()); 390 | $this->assertEquals(Meta::find(10)->key, 'key5'); 391 | $this->assertEquals(Meta::find(25)->key, 'key5'); 392 | $this->assertEquals(Meta::find(6)->value, 2); 393 | $this->truncate(); 394 | } 395 | } 396 | --------------------------------------------------------------------------------