在单元测试中将图像保存到DB不能与CakePHP 2中的二进制文件一起使用

时间:2016-02-17 05:47:17

标签: unit-testing cakephp fixtures cakephp-2.x

这是固定代码:

class UploadfileFixture extends CakeTestFixture {

/**
 * Fields
 *
 * @var array
 */
    public $fields = array(
        'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'key' => 'primary'),
        'data' => array('type' => 'binary', 'null' => false, 'default' => null),
        'path' => array('type' => 'string', 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci',
'charset' => 'utf8'),
...

正如您所看到的那样binaryBLOB data $file['data'] = @file_get_contents($uri); ... $model_file->save($file); PDOException: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'data' at row 1 public class GPSTracker extends Service implements LocationListener { private final Context mContext; // flag for GPS status boolean isGPSEnabled = false; // flag for network status boolean isNetworkEnabled = false; // flag for GPS status boolean canGetLocation = false; Location location; // location double latitude; // latitude double longitude; // longitude // The minimum distance to change Updates in meters private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters // The minimum time between updates in milliseconds private static final long MIN_TIME_BW_UPDATES = 1000 * 60; // 1 minute // Declaring a Location Manager protected LocationManager locationManager; public GPSTracker(Context context) { this.mContext = context; getLocation(); } public Location getLocation() { try { locationManager = (LocationManager) mContext .getSystemService(LOCATION_SERVICE); // getting GPS status isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); // getting network status isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // no network provider is enabled } else { this.canGetLocation = true; // First get location from Network Provider if (isNetworkEnabled) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("Network", "Network"); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } // if GPS Enabled get lat/long using GPS Services if (isGPSEnabled) { if (location == null) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("GPS Enabled", "GPS Enabled"); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } } } catch (Exception e) { e.printStackTrace(); } return location; } /** * Stop using GPS listener * Calling this function will stop using GPS in your app * */ public void stopUsingGPS(){ if(locationManager != null){ locationManager.removeUpdates(GPSTracker.this); } } /** * Function to get latitude * */ public double getLatitude(){ if(location != null){ latitude = location.getLatitude(); } // return latitude return latitude; } /** * Function to get longitude * */ public double getLongitude(){ if(location != null){ longitude = location.getLongitude(); } // return longitude return longitude; } /** * Function to check GPS/wifi enabled * @return boolean * */ public boolean canGetLocation() { return this.canGetLocation; } /** * Function to show settings alert dialog * On pressing Settings button will lauch Settings Options * */ public void showSettingsAlert(){ AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); // Setting Dialog Title alertDialog.setTitle("GPS is settings"); // Setting Dialog Message alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); // On pressing Settings button alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); mContext.startActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); // Showing Alert Message alertDialog.show(); } @Override public void onLocationChanged(Location location) { //CommonUtils.showShortToast(mContext,"On location Chnaged"+location.getLatitude()+""+location.getLongitude()); } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public IBinder onBind(Intent arg0) { return null; } } {/}

保存方法:

{ "type":"record", "name":"root", "namespace":"", "fields":[
{"name":"Key","type":"int","doc":"10,NOT NULL, Max Profiled Length=7, Max Profiled Value=1100367"},
{"name":"SampleCount","type":["int","null"],"doc":"10,NULL"},
{"name":"TestAmount","type":["double","null"] ,"doc":"20,NULL"},
{"name":"SampleAmount","type":["double","null"],"doc":"20,NULL"},
{"name":"Comments","type":["null","string"],"doc":"100,NULL"},
{"name":"ActiveFg","type":"boolean","doc":"5,NOT NULL"},
{"name":"MaxChangedDt","type":"string","doc":"20,NOT NULL,YYYY-MM-DD HH24:MI:SS,Last Record Changed Date."}
]
}

我在尝试保存151K图像时收到此消息:

Error for Sample 1:
Caused by: org.apache.avro.UnresolvedUnionException: Not in union ["int","null"]: 1
    at org.apache.avro.generic.GenericData.resolveUnion(GenericData.java:600)
    at org.apache.avro.generic.GenericDatumWriter.resolveUnion(GenericDatumWriter.java:151)
    at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:71)
    at org.apache.avro.reflect.ReflectDatumWriter.write(ReflectDatumWriter.java:143)
    at org.apache.avro.generic.GenericDatumWriter.writeField(GenericDatumWriter.java:114)
    at org.apache.avro.reflect.ReflectDatumWriter.writeField(ReflectDatumWriter.java:175)
    at org.apache.avro.generic.GenericDatumWriter.writeRecord(GenericDatumWriter.java:104)
    at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:66)
    at org.apache.avro.reflect.ReflectDatumWriter.write(ReflectDatumWriter.java:143)
    at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:58)
    at org.apache.avro.file.DataFileWriter.append(DataFileWriter.java:290)
    ... 29 more
Error for Sample 2:
Caused by: org.apache.avro.UnresolvedUnionException: Not in union ["double","null"]: 46.9158
    at org.apache.avro.generic.GenericData.resolveUnion(GenericData.java:600)
    at org.apache.avro.generic.GenericDatumWriter.resolveUnion(GenericDatumWriter.java:151)
    at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:71)
    at org.apache.avro.reflect.ReflectDatumWriter.write(ReflectDatumWriter.java:143)
    at org.apache.avro.generic.GenericDatumWriter.writeField(GenericDatumWriter.java:114)
    at org.apache.avro.reflect.ReflectDatumWriter.writeField(ReflectDatumWriter.java:175)
    at org.apache.avro.generic.GenericDatumWriter.writeRecord(GenericDatumWriter.java:104)
    at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:66)
    at org.apache.avro.reflect.ReflectDatumWriter.write(ReflectDatumWriter.java:143)
    at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:58)
    at org.apache.avro.file.DataFileWriter.append(DataFileWriter.java:290)
    ... 29 more

1 个答案:

答案 0 :(得分:1)

我认为您使用的是MySQL,这意味着您的BLOB类型列将仅限于

  

L + 2个字节,其中 L < 2 16

<强> http://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html#idm140456796731584

即64k,你的151k远远超过了。

默认情况下,CakePHPs灯具仅支持binary,无论如何都会映射到blob。但是,您可以添加自定义列定义,或通过Mysql::$columns属性修改现有列定义。你可以这样做,例如

使用扩展数据源

应用/型号/数据源/数据库/ AppMysql.php

<?php
App::uses('Mysql', 'Model/Datasource/Database');

class AppMysql extends Mysql
{
    public function __construct($config, $autoConnect)
    {
        $this->columns['mediumbinary'] = array('name' => 'mediumblob');
        parent::__construct($config, $autoConnect);
    }
}

应用/配置/ database.php中

public $test = array(
    'datasource' => 'Database/AppMysql',
    // ...
);

您的灯具

public $fields = array(
    // ...
    'data' => array('type' => 'mediumbinary', 'null' => false, 'default' => null),
    // ...
);

或通过动态修改数据源

例如在那个特定的灯具中,比如

public function init()
{
    $source = ConnectionManager::getDataSource($this->useDbConfig);
    $source->columns['mediumbinary'] = array('name' => 'mediumblob');

    parent::init();
}